Why does VBA code work after "Save" but then not after Exit?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • haleywj
    New Member
    • Apr 2016
    • 1

    Why does VBA code work after "Save" but then not after Exit?

    I am running Access 2007 on a Windows Vista PC.

    I have VBA code that works fine once the "Save" button is selected in the code editor window and continues to work as expected until I exit Access. When I re-launch Access, the code no longer performs the expected actions until I select "View Code" and "Save" once again. I do not have to make any changes to the code. The expected actions once again are performed until I exit Access. Any ideas as to why this is happening?

    The Access VBA module creates a Word 2007 MailMerge letter from a .dotx template. That code works fine every time so is not a problem. Additional code at the end of the module activates the Word application and document and brings the Word window to the front with focus. This is the part of the code that works when "Saved" until I exit Access. When I re-launch Access and perform this action (i.e., select the button to which the code is attached), the Word window after being created stays hidden behind the Access window. If I then "Save" the code (without change), it starts working properly.
  • zmbd
    Recognized Expert Moderator Expert
    • Mar 2012
    • 5501

    #2
    Ok,
    Let's see if I understand:
    + you have code that creates a word document and then is supposed to bring the word document to the front with focus.
    + when you have the code open in the editor window and compile and save the code, the code executes as expected with the document being brought to the top and receiving focus.
    + when you close and reopen the Access database then re-execute the code, the word document is created; however, the document does not receive focus and either stays minimized or stays behind the other windows.
    +++> HOWEVER, if you re-open the VBE and recompile and save the code, the word object behaves as expected.

    Is this correct?

    Also, please post the sections of your code where you have the DIM statements for the word object, the SET statements for the word object, and finally the last of your code where you mentioned that the word object is brought to the front and focus is set.
    >>> PLEASE remember to format the code using the [CODE/] formatting tool in the post toolbar.

    (BTW: Vista is a beast and may be the root cause; however, let's look at the code first :) )

    -z

    Comment

    Working...