Home VB script to export to pdf in Command Button (ActiveX) in Word 2010
Reply: 0

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

user9151 Published in September 19, 2018, 11:19 am

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?

share|improve this question
  • I think you need to create a PDF object. When I tried to do something like this years ago, they didn't have any free libraries to help with this. As a workaround (until I can see what I can find) you could print to a pdf printer and have your macro call that. – Jeff May 8 '13 at 16:19
  • 1
    @Jeff - Excel 2010 has built-in save-as-PDF: no need for additional libraries... – Tim Williams May 8 '13 at 16:36
  • You have two nested subs there - that's not valid VBA. – Tim Williams May 8 '13 at 16:37

2 Answers 2

active oldest votes
up vote 2 down vote accepted
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.470296 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO