Consulting

Results 1 to 4 of 4

Thread: Save word document to separate pdf file saved by name on first line

  1. #1

    Save word document to separate pdf file saved by name on first line

    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
    Last edited by macropod; 03-08-2021 at 01:23 PM. Reason: Added code tags

  2. #2
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    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). 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.
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  3. #3
    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

  4. #4
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    Quote Originally Posted by FenBintang View Post
    I saw the links but the code is more complex than I need.
    More complex in what way?
    Quote Originally Posted by FenBintang View Post
    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.
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •