Consulting

Results 1 to 2 of 2

Thread: Legacy Command Button and VBA

  1. #1

    Legacy Command Button and VBA

    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

  2. #2
    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
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

Posting Permissions

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