![]() |
|
![]() |
|
Thread Tools | Display Modes |
|
#1
|
|||
|
|||
![]()
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 |
#2
|
||||
|
||||
![]()
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 |
#3
|
||||
|
||||
![]()
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] |
![]() |
|
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
![]() |
megatronixs | Word VBA | 9 | 09-05-2020 02:29 PM |
![]() |
magpiedave | Word | 2 | 02-02-2019 04:28 AM |
![]() |
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 |
![]() |
simon teddy | Word | 2 | 11-13-2014 01:56 PM |