VB script to export to pdf in Command Button (ActiveX) in Word 2010

jparnell8839 picture jparnell8839 · May 8, 2013 · Viewed 7.3k times · Source

Ok, so I have this Word 2010 Macro enabled template with my handy dandy forms that people can fill out. I have created a button that says "Convert to PDF" because people dont know how to do it natively. I entered the VB editor of the particular CommandButton that I want to have this functionality. Here's whats in that button:

Private Sub CommandButton1_Click()
Sub Convert_PDF()

 Dim desktoploc As String
 Dim filename As String
 Dim mypath As String

    desktoploc = CreateObject("WScript.Shell").SpecialFolders("Desktop")
    filename = ThisDocument.Name
    mypath = desktoploc & "\" & filename

    ActiveDocument.ExportAsFixedFormat OutputFileName:= _
        mypath, _
        ExportFormat:=wdExportFormatPDF, OpenAfterExport:=True, OptimizeFor:= _
        wdExportOptimizeForPrint, Range:=wdExportAllDocument, From:=1, To:=1, _
        Item:=wdExportDocumentContent, IncludeDocProps:=True, KeepIRM:=True, _
        CreateBookmarks:=wdExportCreateNoBookmarks, DocStructureTags:=True, _
        BitmapMissingFonts:=True, UseISO19005_1:=False
End Sub
End Sub

When I run the code I get..... BAM! Compile error: Expected End Sub

If I take out the Sub Convert_PDF() and its pertaining End Sub, suddenly i dont get the sub error messages, but I get another error message:

The file [file name] cannot be opened beacause there are problems with the contents. Details: The file is corrupt and cannot be opened. Replace [file name] with my file's actual name.

I'll be completely honest, I'm a complete n00b at VB and Google is turning out to not being very helpful thus far :/

Any insight?

Answer

Tim Williams picture Tim Williams · May 8, 2013
Private Sub CommandButton1_Click()
    Convert_PDF
End Sub


Sub Convert_PDF()

 Dim desktoploc As String
 Dim filename As String
 Dim mypath As String

    desktoploc = CreateObject("WScript.Shell").SpecialFolders("Desktop")
    filename = ThisDocument.Name
    mypath = desktoploc & "\" & filename & ".pdf"

    ActiveDocument.ExportAsFixedFormat OutputFileName:= _
        mypath, _
        ExportFormat:=wdExportFormatPDF, OpenAfterExport:=True, OptimizeFor:= _
        wdExportOptimizeForPrint, Range:=wdExportAllDocument, From:=1, To:=1, _
        Item:=wdExportDocumentContent, IncludeDocProps:=True, KeepIRM:=True, _
        CreateBookmarks:=wdExportCreateNoBookmarks, DocStructureTags:=True, _
        BitmapMissingFonts:=True, UseISO19005_1:=False
End Sub