Consulting

Results 1 to 7 of 7

Thread: Word 2013 and exporting to PDF

  1. #1
    VBAX Newbie
    Joined
    May 2017
    Posts
    4
    Location

    Question Word 2013 and exporting to PDF

    Hello,

    I have created a UserForm with a button, that runs the code below. I would like it to export a form, with all the data, to a PDF.

    Application.Dialogs(wdDialogExportAsFixedFormat).Show
    This works, but there is some seemingly odd behaviour where it resets all the form values after export. The data is not in the exported document, nor does it remain on the Word document. It's the same behaviour as the NoReset:=True value when protecting a document, but that doesn't apply here.

    If I manually choose File > Export > Export as PDF, the data comes with it.

    Any idea what I'm doing wrong?

    - Adam.

  2. #2
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,337
    Location
    Is that all the code you have in your userform button? If so the values in the form may not be in the document in the first place.
    Greg

    Visit my website: http://gregmaxey.com

  3. #3
    VBAX Newbie
    Joined
    May 2017
    Posts
    4
    Location
    Hi,

    Thanks for the help so far... much appreciated! There was more code behind the button but I didn't figure it would play a part. I've now made a button with simply that line in it and it seems to have exported with the fields still completed. So, it would suggest my code is at fault.

    Here's the full code:

    Private Sub CommandButton3_Click()
    'Application.ScreenUpdating = False
    If ActiveDocument.ProtectionType <> wdNoProtection Then ActiveDocument.Unprotect Password:="development"
    'ActiveDocument.FormFields("I_OpEncompass").Result = "Yes"
    If ActiveDocument.Variables("DASHAdded") = True Then        ' Do we need to remove the DASH form?
        ActiveDocument.Sections(2).Range.Delete
        ActiveDocument.StoryRanges(wdMainTextStory).Select
        With Selection
            .Find.Text = "If you require this risk assessment, add it using the button on the menu.  It will be removed by the MASH before sending to external agencies, if appropriate."
            .Find.Execute
            .Delete
            .TypeText "A risk assessment was completed, but has been removed as part of Operation Encompass." & (Chr(11)) & (Chr(11))
        End With
    End If
    ' Export the file as a PDF.  Error handler ensures the section is put back if the user cancels the dialog
    On Error Resume Next    ' Stops errors on XP, and retores removed Section G
    Application.Dialogs(wdDialogExportAsFixedFormat).Show
        
    If ActiveDocument.Variables("DASHAdded") = True Then        ' Do we need to put the DASH form back?
        ActiveDocument.Undo
        ActiveDocument.Undo
        ActiveDocument.Undo
        ActiveDocument.Undo
    End If
    'ActiveDocument.Protect Password:="development", NoReset:=True, Type:=wdAllowOnlyFormFields
    Unload Me
    UserForm1.Show vbModeless
    'Application.ScreenUpdating = True
    End Sub
    Basically, I need to export a PDF of a sanitised version of the document. The code should check if a document variable is true, and if so delete the contents of a section and replace a bit of text. In all circumstances, export to PDF, and if that section was removed then to undo those steps to restore the document back to how it was post-export.

    Hope this gives a bit more information...

  4. #4
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,337
    Location
    You need to re-protect the document before publishing. So you may have unprotect and protect twice based on your conditions.
    Greg

    Visit my website: http://gregmaxey.com

  5. #5
    VBAX Newbie
    Joined
    May 2017
    Posts
    4
    Location
    Thanks so much! That's working now.

    I also cleaned up the code a little and changed it to hiding a section rather than deleting content - I found that protecting the document cleared the undo stack, so the old code didn't work any more.

    Private Sub CommandButton4_Click()
    If ActiveDocument.ProtectionType <> wdNoProtection Then ActiveDocument.Unprotect Password:="development"
    ActiveDocument.FormFields("I_OpEncompass").Result = "Yes"
    If ActiveDocument.Variables("DASHAdded") = True Then ActiveDocument.Sections(2).Range.Font.Hidden = True
    ActiveDocument.Protect Password:="development", NoReset:=True, Type:=wdAllowOnlyFormFields
    ' Export the file as a PDF.  Error handler ensures the section is put back if the user cancels the dialog
    On Error Resume Next    ' Stops errors on XP, and retores removed Section G
    Application.Dialogs(wdDialogExportAsFixedFormat).Show
    If ActiveDocument.Variables("DASHAdded") = True Then
        If ActiveDocument.ProtectionType <> wdNoProtection Then ActiveDocument.Unprotect Password:="development"
        ActiveDocument.Sections(2).Range.Font.Hidden = False
        ActiveDocument.Protect Password:="development", NoReset:=True, Type:=wdAllowOnlyFormFields
    End If
    Unload Me
    UserForm1.Show vbModeless
    End Sub

  6. #6
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,337
    Location
    Glad you got it sorted out. You might consider using With statements:

    Private Sub CommandButton4_Click()
      With ActiveDocument
        If .ProtectionType <> wdNoProtection Then .Unprotect Password:="development"
        .FormFields("I_OpEncompass").Result = "Yes"
        If .Variables("DASHAdded") = True Then .Sections(2).Range.Font.Hidden = True
        .Protect Password:="development", NoReset:=True, Type:=wdAllowOnlyFormFields
        ' Export the file as a PDF.  Error handler ensures the section is put back if the user cancels the dialog
        On Error Resume Next ' Stops errors on XP, and retores removed Section G
        Application.Dialogs(wdDialogExportAsFixedFormat).Show
        If .Variables("DASHAdded") = True Then
          If .ProtectionType <> wdNoProtection Then .Unprotect Password:="development"
          .Sections(2).Range.Font.Hidden = False
          .Protect Password:="development", NoReset:=True, Type:=wdAllowOnlyFormFields
        End If
      End With
      Unload Me
      UserForm1.Show vbModeless
    End Sub
    Greg

    Visit my website: http://gregmaxey.com

  7. #7
    VBAX Newbie
    Joined
    May 2017
    Posts
    4
    Location
    Sweet, thanks!

Posting Permissions

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