PDA

View Full Version : Word 2013 and exporting to PDF



QuannyUK
05-03-2017, 04:21 AM
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.

gmaxey
05-03-2017, 06:20 AM
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.

QuannyUK
05-03-2017, 11:57 AM
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...

gmaxey
05-03-2017, 01:09 PM
You need to re-protect the document before publishing. So you may have unprotect and protect twice based on your conditions.

QuannyUK
05-04-2017, 12:51 AM
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

gmaxey
05-04-2017, 03:14 AM
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

QuannyUK
05-04-2017, 11:41 AM
Sweet, thanks!