Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 04-05-2022, 07:07 AM
shol shol is offline Help needed to export word document to a pdf and name the pdf a title from the document Windows 10 Help needed to export word document to a pdf and name the pdf a title from the document Office 2016
Novice
Help needed to export word document to a pdf and name the pdf a title from the document
 
Join Date: Apr 2022
Posts: 1
shol is on a distinguished road
Default Help needed to export word document to a pdf and name the pdf a title from the document

I am trying to write a macro that will split a 500 page word document to indivdual pdfs and name the pdf a title from the word document



I have found some code online to split the word document and i have managed to get a string to be the text i want but it crashes whenever I try to use that to name the pdf.
the code i have is below. i highlighted the bit that crashes. if i remove "fName"and replace it with any integer it will work. is there a way i can make a "text" something other then a string? i have attached a screen shot of the document. the title i want is the SLA code



Do you have any Suggestions?


Option Explicit
Sub SaveAsSeparatePDFs()

Dim strDirectory As String, strTemp As String
Dim ipgStart As Integer, ipgEnd As Integer
Dim i As Integer
Dim vMsg As Variant, bError As Boolean
Dim fName As String



1:
strDirectory = InputBox("Directory to save individual PDFs? " & _
vbNewLine & "(ex: C:\Users\Public)")
If strDirectory = "" Then Exit Sub
If Dir(strDirectory, vbDirectory) = "" Then
vMsg = MsgBox("Please enter a valid directory.", vbOKCancel, "Invalid Directory")
If vMsg = 1 Then
GoTo 1


Else
Exit Sub
End If
End If

2:
strTemp = InputBox("Begin saving PDFs starting with page __? " & _
vbNewLine & "(ex: 32)")
bError = bErrorF(strTemp)
If bError = True Then GoTo 2
ipgStart = CInt(strTemp)

3:
strTemp = InputBox("Save PDFs until page __?" & vbNewLine & "(ex: 37)")
bError = bErrorF(strTemp)
If bError = True Then GoTo 3
ipgEnd = CInt(strTemp)



On Error GoTo 4:
For i = ipgStart To ipgEnd

fName = ActiveDocument.Paragraphs(8).Range

ActiveDocument.ExportAsFixedFormat OutputFileName:= _
strDirectory & fName & ".pdf", ExportFormat:=wdExportFormatPDF, _
OpenAfterExport:=False, OptimizeFor:=wdExportOptimizeForPrint, Range:= _
wdExportFromTo, From:=i, To:=i, Item:=wdExportDocumentContent, _
IncludeDocProps:=False, KeepIRM:=False, CreateBookmarks:= _
wdExportCreateHeadingBookmarks, DocStructureTags:=True, _
BitmapMissingFonts:=False, UseISO19005_1:=False
Next i
End
4:
vMsg = MsgBox("Unknown error encountered while creating PDFs." & vbNewLine & vbNewLine & _
"Aborting", vbCritical, "Error Encountered")
End Sub

Private Function bErrorF(strTemp As String) As Boolean
Dim i As Integer, vMsg As Variant
bErrorF = False

If strTemp = "" Then
End
ElseIf IsNumeric(strTemp) = True Then
i = CInt(strTemp)
If i > ActiveDocument.BuiltInDocumentProperties(wdPropert yPages) Or i <= 0 Then
Call msgS(bErrorF)
End If
Else
Call msgS(bErrorF)
End If
End Function

Private Sub msgS(bMsg As Boolean)
Dim vMsg As Variant
vMsg = MsgBox("Please enter a valid integer." & vbNewLine & vbNewLine & _
"Integer must be > 0 and < total pages in the document (" & _
ActiveDocument.BuiltInDocumentProperties(wdPropert yPages) & ")", vbOKCancel, "Invalid Integer")
If vMsg = 1 Then
bMsg = True
Else
End
End If
End Sub
Attached Images
File Type: jpg sla.jpg (128.3 KB, 16 views)
Reply With Quote
  #2  
Old 04-06-2022, 01:40 AM
Guessed's Avatar
Guessed Guessed is offline Help needed to export word document to a pdf and name the pdf a title from the document Windows 10 Help needed to export word document to a pdf and name the pdf a title from the document Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 4,159
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

I'm guessing but I suspect the problem is with what a table cell contains. You are trying to get the text from a table cell which ends with two characters unlikely to be allowed in file names.

Table cells always end with a paragraph mark and an end-of-cell mark. If you trim them off before assigning the rest of the text to the filename you might have more luck
Code:
fName = ActiveDocument.Paragraphs(8).Range
fName = Left(fName,Len(fName)-2)   'add this line
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #3  
Old 04-06-2022, 03:37 PM
macropod's Avatar
macropod macropod is offline Help needed to export word document to a pdf and name the pdf a title from the document Windows 10 Help needed to export word document to a pdf and name the pdf a title from the document Office 2016
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 22,366
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Alternatively:
fName = Split(ActiveDocument.Paragraphs(8).Range.Text, vbCr)(0)
Done that way, it doesn't matter whether the range is in a table.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Help needed to export word document to a pdf and name the pdf a title from the document split word document based on bookmarks with each new document title of the bookmark megatronixs Word VBA 9 09-05-2020 02:29 PM
Help needed to export word document to a pdf and name the pdf a title from the document My document title now has the word (Repaired) at the end magpiedave Word 2 02-02-2019 04:28 AM
Help needed to export word document to a pdf and name the pdf a title from the document How to have the location (path) of the Word document on the title bar? Jamal NUMAN Word 24 09-06-2017 12:18 PM
Need help with my word document - black space before title safire Word 1 03-07-2017 01:46 AM
Help needed to export word document to a pdf and name the pdf a title from the document How To Center The Title On a Word Document simon teddy Word 2 11-13-2014 01:56 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 03:37 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft