PDA

View Full Version : Legacy Command Button and VBA



Rigout
01-22-2016, 11:56 AM
Good evening all,


I've just created a word document form with tables, etc. but I want the person completing the form to be able to save it as a PDF without having to go through the menus, etc.

I've inserted a button using the legacy tools and named it "Save as PDF" but now I need to give it some functionality and I'm thinking that this needs to be done by VBA code.

Can anyone help me on the code I require?

I've not used VBA before so not sure what is required.



Best Regards

Rigout

gmayor
01-23-2016, 08:21 AM
If this is Word 2007 or above, then put the button in design mode from the Developer tab. Right click your button and select View Code.
Note the number of the ComandButton. It will probably be 1
Replace all the code with the following, changing the button number, if necessary, to what it was originally.
Save the document as macro enabled.
Before Word 2007 there was no Word function to create PDF, which requires a third party application to achieve.

Option Explicit

Private Sub CommandButton1_Click()
Dim strDocName As String
Dim strPath As String
Dim intPos As Integer
Start:
'Find position of extension in filename
strDocName = ActiveDocument.Name
strPath = ActiveDocument.Path & "\"
intPos = InStrRev(strDocName, ".")
If intPos = 0 Then
ActiveDocument.Save
GoTo Start
End If
strDocName = Left(strDocName, intPos - 1)
strDocName = strPath & strDocName & ".pdf"

ActiveDocument.ExportAsFixedFormat OutputFilename:=strDocName, _
ExportFormat:=wdExportFormatPDF, _
OpenAfterExport:=False, _
OptimizeFor:=wdExportOptimizeForPrint, _
Range:=wdExportAllDocument, From:=1, to:=1, _
Item:=wdExportDocumentContent, _
IncludeDocProps:=True, _
KeepIRM:=True, _
CreateBookmarks:=wdExportCreateHeadingBookmarks, _
DocStructureTags:=True, _
BitmapMissingFonts:=True, _
UseISO19005_1:=False
lbl_Exit:
Exit Sub
End Sub