PDA

View Full Version : [SOLVED:] Word document won't open after saveas2 - problem with contents



Eastwick
04-22-2018, 06:26 PM
Hello,

I have code that takes the contents of a userform (textboxes and listbox values) and inserts those values into the bookmarks of my .dotm template. It also inserts text from specified files based upon checkbox selections. All of that works fine but I am stumped by the SaveAs2 code that finalises the project.

The document cannot be opened after save.

The following error message is received;

"We're sorry. We can't open ***.docx because we found a problem with its contents"

it can only be opened if you change the extension to .docm

I have searched and searched to no avail - I think I might be missing something very fundamental. I have also noticed that if you open the resultant .docx document in the VBA editor it has a reference to my template document which contains the code.

This is the current iteration of the SaveAs2 code;


ActiveDocument.SaveAs2 FileName:="CurDir" & "DRAFT_RFQ_" & prName, _
FileFormat:=wdFormatXMLDocument, LockComments:=False, Password:="", _
AddToRecentFiles:=True, WritePassword:="", ReadOnlyRecommended:=False, _
EmbedTrueTypeFonts:=False, SaveNativePictureFormat:=False, SaveFormsData _
:=False, SaveAsAOCELetter:=False, CompatibilityMode:=15


Any help would be greatly appreciated - Eastwick.

macropod
04-22-2018, 09:26 PM
I have code that takes the contents of a userform (textboxes and listbox values) and inserts those values into the bookmarks of my .dotm template. It also inserts text from specified files based upon checkbox selections. All of that works fine but I am stumped by the SaveAs2 code that finalises the project.
Why would you be modifying the template instead of creating a new document from it (via Documents.Add) and updating that?


Unless prName includes the .docx extension, it seems to me all you need is:
ActiveDocument.SaveAs2 FileName:="CurDir" & "DRAFT_RFQ_" & prName & ".docx", FileFormat:=wdFormatXMLDocument, AddToRecentFiles:=False

Eastwick
04-23-2018, 01:39 AM
Oh. As I suspected an embarrassing Newbie error - works exactly as intended - Many thanks Paul.