PDA

View Full Version : [SOLVED:] Save word document to separate pdf file saved by name on first line



FenBintang
03-08-2021, 10:30 AM
Hi,

I have a Word document like 100 pages and would like to save as PDF file saved by name as mentioned on first line in word document.
I saw a macro that basically creates a pdf out of each page individually and it works perfectly. I want to to save the document as PDF with the name on the first line as mentioned in word document.

I have tried to change the code but this is not working properly. The second records and the required text will not show in pdf file. can you please help me?
thank you in advance.



Sub SaveToPDF()


Dim I As Long
Dim xdlg As FileDialog
Dim xfolder As Variant
Dim xStart, xEnd As Integer
Dim docmultiple As Document
Dim strNewFileName As String
Dim iCurrentPage As Integer
Dim ipagecount As Integer
Dim rngpage As Range
Dim rngdoc As Range



Application.ScreenUpdating = False

Set docmultiple = ActiveDocument
Set rngpage = docmultiple.Range()

Set rngdoc = ActiveDocument.Content

Set xdlg = Application.FileDialog(msoFileDialogFolderPicker)

If xdlg.Show <> -1 Then Exit Sub

xfolder = xdlg.SelectedItems(1)


iCurrentPage = 1

ipagecount = docmultiple.Content.ComputeStatistics(wdStatisticPages)

Do Until iCurrentPage > ipagecount
If iCurrentPage = ipagecount Then
rngpage.End = ActiveDocument.Range.End
Else

Selection.GoTo wdGoToPage, wdGoToAbsolute, iCurrentPage
rngpage.End = Selection.Start

End If

strNewFileName = Empty

rngpage = iCurrentPage

strNewFileName = strNewFileName & Left(rngdoc.Paragraphs(1), _
Len(rngdoc.Paragraphs(1).Range.Text) - 1)

ActiveDocument.ExportAsFixedFormat OutputFileName:=xfolder & "" & strNewFileName, ExportFormat:=wdFormatPDF, Range:=wdExportCurrentPage

iCurrentPage = iCurrentPage + 1
Selection.GoTo wdGoToPage, wdGoToNext
rngpage.Collapse wdCollapseEnd

Loop


Application.ScreenUpdating = True



Exit Sub

macropod
03-08-2021, 01:23 PM
This kind of thing has been addressed many times.

Such requirements are usually the result of a mailmerge being performed and the user wanting to split the output into separate files. That is best done during the mailmerge, for which see Send Mailmerge Output to Individual Files at: Mailmerge Tips & Tricks (msofficeforums.com) (https://www.msofficeforums.com/mail-merge/21803-mailmerge-tips-tricks.html). If for some reason you're unable to use that approach, you can use the macro under the heading Split Merged Output to Separate Documents in the same link. Both approaches allow the output to be sent to PDFs.

PS: When posting code, please use the code tags, indicated by the # button on the posting menu. Without them, your code loses much of whatever structure it had.

FenBintang
03-08-2021, 02:10 PM
Thx Paul for your advice, I am new here. I saw the links but the code is more complex than I need. Basically I need to split a word document into seperate pdf file. Rightnow my code will work only for the first record, it will not go to the next loop. May be you can advise me?
Thanks in advance, FenBintang

macropod
03-08-2021, 03:56 PM
I saw the links but the code is more complex than I need.
More complex in what way?

Basically I need to split a word document into seperate pdf file.
Which is exactly what the code under the heading Split Merged Output to Separate Documents does... I don't propose to re-write your code when there's perfectly good code available that can already be used.