Auto-record a macro to save Word files as PDF

Yann211 Posted messages 21 Status Member -  
Yann211 Posted messages 21 Status Member -
```html Hello,

I am working on a Word file for which I want to automatically create a PDF file upon closing (for the people for whom this file is intended).

I was then given a macro that automatically prints the file as a PDF when closing the Word file. The problem is that in this file, I have created a sort of table of contents on the first page with hypertext links that directly refer to the chapter page (the document has over 500 pages), and with this method, the links do not work in the PDF file (because they are links with locations in the document). Therefore, I need a similar type of macro, but instead of printing the file as a PDF, it should save the file as a PDF, and that would solve my problem.

I am not familiar with macros, let alone programming, which is why I am seeking your help.

Here is the first macro I had for printing to PDF, which can serve as a basis for creating one that saves as PDF.

Thank you in advance for your help.

PDF Print Macro

Sub AutoClose()
Dim nfichier As String, intpos As Byte
nfichier = ActiveDocument.Name
intpos = InStrRev(nfichier, ".")
nfichier = Left(nfichier, intpos - 1)
ActiveDocument.ExportAsFixedFormat outputFileName:="C:\Users\Yann\OneDrive\ELEVES SB" & "/" & nfichier, exportFormat:=wdExportFormatPDF
End Sub
```

1 answer

  1. m@rina Posted messages 27566 Registration date   Status Moderator Last intervention   11 562
     
    Hello

    First of all, this macro does not create a virtual print, for which you would need a virtual printer and name it.
    The recording in PDF format via macro can be done with three functions:
    - PrintOut => virtual printing
    - SaveAs2
    - ExportAsFixedFormat

    The last two functions yield a similar result. The last one was specifically created for PDF and XPS formats. It indeed allows for PDF-specific settings.

    And I am surprised, because by default this macro puts the links on the headings, as it is a default Word feature to export to PDF with links on headings.

    Try adding to the last line:
     , createbookmarks:=wdExportCreateHeadingBookmarks


    And if that still doesn't work, you can try the SaveAs2 function... :
    ActiveDocument.SaveAs2 FileName:="filename.pdf", fileformat:=wdFormatPDF


    Just for your information, your macro that was created by "on" has lines that allow retrieving the name of the current file. It seems unnecessary, since you hardcode the name...

    m@rina

    --
    I am often called "on"...
    0
    1. Yann211 Posted messages 21 Status Member
       
      Thank you Marina. I didn't understand your sentence: "
      Just for your information, your macro that was created by "on" has lines that allow you to retrieve the name of the current file. Apparently, it doesn't serve any purpose for you, since you hardcode the name... "
      0
    2. Yann211 Posted messages 21 Status Member
       
      The links work on a PDF but my table of contents is made from links that point to bookmarks in the Word document (so locations in the doc). Therefore, they cannot be active on a printed PDF.
      0