Results 1 to 6 of 6
  1. #1
    riggsdp is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Nov 2014
    Posts
    25

    Word file saved from VBA is corrupted

    I have a question related to my previous question - https://www.accessforums.net/program...ble-48599.html



    When the Word doc is saved, if I close it then attempt to open it again, I get the following error message:

    The file <filename>.docx cannot be opened because there are problems with the contents.

    Clicking on Details gets: No error detail available.

    If I do a save as before closing the document, with a new name, it opens correctly.

    Is there something I'm doing incorrectly when naming and saving the file in the VBA code?

  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,549
    That's pretty bizarre. You could try saveas a different version. (earlier) That may help.

  3. #3
    John_G is offline VIP
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2011
    Location
    Ottawa, ON (area)
    Posts
    2,615
    Hi -

    Refer to the code you posted before. The error you are getting with MS Word may be due to the fact that you are not closing the instance of MS Word that your function starts.

    Add these lines to the bottom of the function, just before the end:

    '
    ' Close the word application
    '
    AppWord.Quit
    Set AppWord = Nothing

    That should fix the error.


    John

  4. #4
    ssanfu is offline Master of Nothing
    Windows XP Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    I looked at the code from your previous post and saw:
    Code:
        'open MSWord
        Set appWord = GetObject(, "Word.Application")
        If Err.Number <> 0 Then
            Set appWord = New Word.Application
            appWord.Visible = True
        End If
    but nowhere did I see where you closed Word or released the pointers. In effect, the Word document is still open.

    The automation rule (that I learned) is:
    If you open it, close it. (this applies to recordsets)
    If you create it, destroy it. (this applies to things like Word/Excel objects)

    Note 1:Make sure this is at the end of the Sub/Function.
    Note 2: If you use "Set db = Currentdb"...... do not close Currentdb or set Currentdb = nothing. But you would have a line: "Set db = Nothing"


    I would suggest you should try modifying your code to ensure the objects (Word) are closed before you try to open the document.


    My $0.02........

  5. #5
    riggsdp is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Nov 2014
    Posts
    25
    I added the code at the end of the function to close MSWord and I still get the same error message.

  6. #6
    John_G is offline VIP
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2011
    Location
    Ottawa, ON (area)
    Posts
    2,615
    Hi -

    In your code that opens MS Word :

    'open MSWord
    Set appWord = GetObject(, "Word.Application")
    If Err.Number <> 0 Then
    Set appWord = New Word.Application
    appWord.Visible = True
    End If

    I suggest you change it to just

    Set appWord = New Word.Application
    appWord.Visible = True

    so that you always open a new instance of MS Word, whether there is another one open or not.

    That way MS Access has its own instance to work with, and closing it won't affect anything else.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 4
    Last Post: 06-05-2014, 02:24 PM
  2. Changing File Location in saved imports using VBA
    By akshatagarwal93 in forum Import/Export Data
    Replies: 3
    Last Post: 07-23-2012, 10:47 AM
  3. Access to Word Merge into Separately Saved Documents
    By alpinegroove in forum Programming
    Replies: 6
    Last Post: 01-05-2012, 04:49 PM
  4. Exporting Report to a word file or PDF or whatever
    By AccessDatabaseGuy in forum Access
    Replies: 1
    Last Post: 05-03-2011, 02:03 PM
  5. Creating report from Word file
    By jonny in forum Reports
    Replies: 0
    Last Post: 10-15-2009, 03:10 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums