Consulting

Results 1 to 3 of 3

Thread: Word document won't open after saveas2 - problem with contents

  1. #1
    VBAX Regular Eastwick's Avatar
    Joined
    Mar 2018
    Location
    Newcastle
    Posts
    10
    Location

    Word document won't open after saveas2 - problem with contents

    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.

  2. #2
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    Quote Originally Posted by Eastwick View Post
    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
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  3. #3
    VBAX Regular Eastwick's Avatar
    Joined
    Mar 2018
    Location
    Newcastle
    Posts
    10
    Location
    Oh. As I suspected an embarrassing Newbie error - works exactly as intended - Many thanks Paul.

Posting Permissions

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