+ Reply to Thread
Results 1 to 9 of 9

Closing Userform from VBA when it appears

  1. #1
    Registered User
    Join Date
    11-07-2012
    Location
    Copenhagen, Denmark
    MS-Off Ver
    Office 365
    Posts
    71

    Closing Userform from VBA when it appears

    Hello VBA experts! I have made a Word VBA module to save a copy of a file to specific locations and send the file in an email attachement, all based on different user variables. It all works well, except 1 element.

    The user works in a document (report that changes during the progress of a project, from a table-based template) which then have to be saved and sent dependent on which division and the progress. The user works in one personal file that they can update.

    My macro is initiated by user choice in a User Form. The User Form open on Document_Close(), to avoid people forgetting to send the information off.

    The document is then copied using the Sub below:

    Please Login or Register  to view this content.
    This creates a exact copy of the document that the macro then edits, mostly removing tables, then saving it to a location and sending it to specific people.

    I'd then like to close the file automatically, which I do by using SaveDoc.Close SaveChanges:=wdSaveChanges. Here I run into trouble.

    The "SaveDoc" copy of my file is saved as .docx, but apparently still contains it's own, copied macro, because the original .dotm file is still open. Closing the copy file means a copy of the original User Form pops up. I'd like to avoid this, as it can confuse the users. How can I avoid this happening? Can I prevent the SaveDoc file from opening User Form, or do something else?

    I have difficulties sharing the full code, as it contains a bunch of internal names, projects and addresses. I hope You can help regardless.


    Thank You,

    GregersDK

  2. #2
    Forum Expert
    Join Date
    01-23-2013
    Location
    USA
    MS-Off Ver
    Microsoft 365 aka Office 365
    Posts
    3,863

    Re: Closing Userform from VBA when it appears

    Hi,

    I don't know your details, but a small change in code after using 'SaveCopyAs' may help:
    https://msdn.microsoft.com/en-us/vba...s-method-excel

    Lewis

  3. #3
    Registered User
    Join Date
    11-07-2012
    Location
    Copenhagen, Denmark
    MS-Off Ver
    Office 365
    Posts
    71

    Re: Closing Userform from VBA when it appears

    Quote Originally Posted by LJMetzger View Post
    Hi,

    I don't know your details, but a small change in code after using 'SaveCopyAs' may help:
    https://msdn.microsoft.com/en-us/vba...s-method-excel

    Lewis
    I'm not sure this works Word, seems it only exists for Excel (where you link) and PowerPoint.

    My problem is:
    • The document is copied with the VBA code embedded, despite being a .docx (your solution potentially could fix this)
    • The UserForm OnClose runs in this new document, and I cannot find a method so far to cancel it

    Solving either of these should fix the problem.

    It seems 'SaveCopyAs' does work in Word (though I haven't found any documentation of this so far), but I cannot seem to define it as a Document (Dim Doc As Document). It makes it difficult to reliably refer to it.

  4. #4
    Forum Expert
    Join Date
    01-23-2013
    Location
    USA
    MS-Off Ver
    Microsoft 365 aka Office 365
    Posts
    3,863

    Re: Closing Userform from VBA when it appears

    Sorry for the confusion, I didn't realize it was a Word problem. There are many VBA commands that work that most of us do not know about.

    Another potential solution for your problem is to have a controlling Word document that contains all the VBA. I use this technique successfully in Excel all the time even with data files that contain Macros. If you still do not have success, try making a small sample file containing no sensitive information that demonstrates the problem.

  5. #5
    Registered User
    Join Date
    11-07-2012
    Location
    Copenhagen, Denmark
    MS-Off Ver
    Office 365
    Posts
    71

    Re: Closing Userform from VBA when it appears

    I have found out what the problem really is:

    When making my new document as a copy of the original, using the original document as the template (marked in red below) causes it to also copy the macros over. How do avoid this? The nearest I've come is to instead make a blank document, then transfer all headers/footers and content afterwards, but this is not without problems either. Making a new blank document prevents the macro from being copied though.

    Please Login or Register  to view this content.
    Anyone have a better solution to this?


    GregersDK

  6. #6
    Forum Expert
    Join Date
    01-23-2013
    Location
    USA
    MS-Off Ver
    Microsoft 365 aka Office 365
    Posts
    3,863

    Re: Closing Userform from VBA when it appears

    See the following link: https://stackoverflow.com/questions/...-saving-macros

    The link deletes all the Macros in the destination file. However, in order to do this, in VBA you need to add a reference to the following Library from Word:
    Alt F11 > Tools > References > Microsoft Visual Basic For Applications Extensibility 5.3

  7. #7
    Forum Expert macropod's Avatar
    Join Date
    12-22-2011
    Location
    Canberra, Australia
    MS-Off Ver
    Word, Excel & Powerpoint 2003 & 2010
    Posts
    3,726

    Re: Closing Userform from VBA when it appears

    The first thing to understand is that docx files do not contain macros. Hence, if the file is emailed to someone, they won't see the userform, etc. None of what LJMetzger suggests doing in post #6 is necessary; moreover, doing so requires giving trusted access to the VBA Project model, which is a serious security risk.

    The fact your userform becomes visible when the document is closed is really an issue with how the userform is coded. Evidently, it lacks an appropriate:
    Unload Me
    line - or it's in the wrong place. If you need to unload it with code being run from outside the userform, you'd use something like:
    Unload Userform1
    Cheers,
    Paul Edstein
    [Fmr MS MVP - Word]

  8. #8
    Registered User
    Join Date
    11-07-2012
    Location
    Copenhagen, Denmark
    MS-Off Ver
    Office 365
    Posts
    71

    Red face Re: Closing Userform from VBA when it appears

    I solved it!

    After macropod's post I realised I may had overcomplicated the issue, so I got a little creative.

    Som explanation: The user will use the .docm file with macros to produce, save and send a report multiple times, with changes.
    The problem was that the newly made .docx file also contained the macros. The macros where to run when ActiveDocument.OnClose, so it would repeat the macro running the UserForm.
    When the .docm is closed, the .docx no longer contains macros.

    I realised the defining difference between the documents was the file extension, so I had the initiating macro read it to determine to show UserForm or not. It looks like this:

    Please Login or Register  to view this content.
    Simply, if the Active Document has the file extension of .docx, never show the UserForm.


    Thanks for Your help, I will mark this thread as solved

  9. #9
    Forum Expert macropod's Avatar
    Join Date
    12-22-2011
    Location
    Canberra, Australia
    MS-Off Ver
    Word, Excel & Powerpoint 2003 & 2010
    Posts
    3,726

    Re: Closing Userform from VBA when it appears

    Quote Originally Posted by gregersdk View Post
    The user will use the .docm file with macros to produce, save and send a report multiple times, with changes.
    That's what you should be using a dotm template form not a docm document.
    Quote Originally Posted by gregersdk View Post
    The problem was that the newly made .docx file also contained the macros.
    As I said before a docx file cannot contain macros. Your confusion lies in the fact you haven't understood the relationship between a document and its template (in this case, your mis-used docm file). A docx file always has access to the macros in its template, even though the docx file cannot itself contain any.

    You would have far less problems if you used an actual dotm template for this...

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Workbook Crashes Upon Closing Userform (Userform Updates Pivot Table Source Data)
    By ChrisHook in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-20-2017, 10:28 AM
  2. VBA Password Box Appears After Closing File
    By dmw2014 in forum Excel Programming / VBA / Macros
    Replies: 19
    Last Post: 10-06-2016, 03:04 PM
  3. [SOLVED] MsgBox appears based on certain conditions being met while opening & closing file
    By KK1234 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 01-21-2014, 04:54 PM
  4. Reading the return value from Excel msgbox, that appears while closing
    By Raghavan12 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-09-2011, 07:39 AM
  5. Closing application before MsgBox appears.
    By CJPHX in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-26-2010, 12:45 PM
  6. Keep Modal UserForm Alive after closing 2nd userform using 2007?
    By ShredDude in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-24-2008, 03:12 PM
  7. [SOLVED] Password prompt appears when closing Excel
    By RJ Lohan in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 04-22-2005, 10:06 PM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1