PDA

View Full Version : [SOLVED:] Word Macro for saving PDF files according to the text inside of the file



alkhorsh
02-07-2021, 11:42 PM
Hi Guys, I have a WORD file which includes 100 job offers for employees. Every 2 page in the document belongs to 1 person so I wrote the following code to do so and save as PDF separately. like:
PAGE1.2 = PDF1
PAGE3.4 = PDF2
and so on..

The problem is renaming the files. If you check in the code where I specified "FirstPara" - That's where the naming the file is happening. Right now it will take the 3rd line of the first document only! it will not go to the next loop. I don't know how to connect it to "i" so it would iterate every time and select the 3rd line of next page.
Basically, it's a nested loop I think?

PLEASE HELPPPP!





Sub SaveAsSeparatePDFs()


Dim strDirectory As String, strTemp As String, ipgEnd As Integer
Dim iPDFnum As Integer, i As Integer



1:
strTemp = InputBox("How many pages is included?" & vbNewLine & "(ex: 60)")
ipgEnd = CInt(strTemp)
strDirectory = Environ("USERPROFILE") & "\Desktop"



iPDFnum = 1


For i = 1 To ipgEnd


Set r = ActiveDocument.Range
r.End = r.End - 1
FirstPara = r.Paragraphs(1).Range.Text
FirstPara = Left(FirstPara, Len(FirstPara) - 1)

ActiveDocument.ExportAsFixedFormat OutputFileName:= _
strDirectory & "\JobOffer--" & iPDFnum & FirstPara & ".pdf", ExportFormat:=wdExportFormatPDF, _
OpenAfterExport:=False, OptimizeFor:=wdExportOptimizeForPrint, Range:= _
wdExportFromTo, From:=i, To:=i + 1, Item:=wdExportDocumentContent, _
IncludeDocProps:=False, KeepIRM:=False, CreateBookmarks:= _
wdExportCreateHeadingBookmarks, DocStructureTags:=True, _
BitmapMissingFonts:=False, UseISO19005_1:=False
iPDFnum = iPDFnum + 1

i = i + 1




Next i


End


End Sub

macropod
02-08-2021, 01:07 AM
Since this is most likely the result of a mailmerge, see Split Merged Output to Separate Documents or, better still, Send Mailmerge Output to Individual Files in: Mailmerge Tips & Tricks (msofficeforums.com) (https://www.msofficeforums.com/mail-merge/21803-mailmerge-tips-tricks.html)

gmayor
02-08-2021, 09:42 PM
See also http://www.vbaexpress.com/forum/showthread.php?68396-Merging-two-MACRO-scripts-(WORD)

macropod
02-08-2021, 09:56 PM
The same question was also cross-posted at StackOverflow, but was deleted after it was observed there that the required edits to the code were trivial...