Word VBA Macro closes the userform at second run
I have this example very easy to reproduce:
A macro which has a userform with only one button on it "Close Active Document" which only has the code
Code:
ActiveDocument.Close (False)
If I open multiple documents let's say 5 and launch the macro, it will close the first active document when button pressed, stays on, it will close the second active document when button pressed and than closes itself. Which one is abnormal, the fact it stays open after the first document closes or the fact it closes after running the code the second time?
And how can I make it to stay on at all times, so I can close all 5 files one by one?
Thanks in advance for any answers.
Re: Word VBA Macro closes the userform at second run
Quote:
And how can I make it to stay on at all times, so I can close all 5 files one by one?
Thanks in advance for any answers.
Let me see if I have understood you correctly...
There are 5 word documents. On one document, you have this userform and when you click on this button, you want the rest(4) of the documents to close one by one each time you press this button... and in the end close itself... Am I correct?
Re: Word VBA Macro closes the userform at second run
Yes/No
I need the macro to close the active document but still stay on as long as there is another active document. It works for the first run but it doesn't for the second.
Re: Word VBA Macro closes the userform at second run
Quote:
I need the macro to close the active document but still stay on as long as there is another active document. It works for the first run but it doesn't for the second.
It won't stay on because, the document from which you are calling the macro was the active document and you closed it, it stopped the macro as well :)
Why don't you close the rest of the 'open' documents and then in the end close the document which calls the macro?
Re: Word VBA Macro closes the userform at second run
or you can use
Application.Quit
to close all documents at once :)
Re: Word VBA Macro closes the userform at second run
That would be a good idea but how do I process documents which are not active? And how do I make a difference between what I want to process (close in this case) and documents which does not need to?
The macro is actually more complicated, it inserts signature, cordsheet, supervisor name, saves the file, prints it twice and finally closes it.
Having a visual would help the user in seing what happened (I have "OK" feedback on the userform for each automated action) and also not using it if the active file is something else...
Also closing the macro and run it again with each file is not an option because each user has to browse where to save, where is the signature, the cordsheet etc, info I can't save, unfortunately.
Re: Word VBA Macro closes the userform at second run
PS Isn't it strange though, that even if I close the document which I activated the macro from, it still works one more time before shutting itself down?
Re: Word VBA Macro closes the userform at second run
Quote:
PS Isn't it strange though, that even if I close the document which I activated the macro from, it still works one more time before shutting itself down?
If you mean it closes two documents out of 5 before closing down, then it is definitely strange....
When you call the macro, which document is active? What I mean is do you call the macro from the document it is saved in or from a different document?
Re: Word VBA Macro closes the userform at second run
I call the macro from the last open document, the visible document on screen, with a toolbar button (or Alt+F8 / Run).
I tried to run the macro, just show the userform, and before running the code in "Go" button, to change the active document. It didn't work. I won't mind to just hide the docs which were processed and then close them all at once when done, but... how?
Re: Word VBA Macro closes the userform at second run
Quote:
I call the macro from the last open document, ....
Hmm :) I thought so...
Quote:
That would be a good idea but how do I process documents which are not active? And how do I make a difference between what I want to process (close in this case) and documents which does not need to?
The macro is actually more complicated, it inserts signature, cordsheet, supervisor name, saves the file, prints it twice and finally closes it.
Having a visual would help the user in seing what happened (I have "OK" feedback on the userform for each automated action) and also not using it if the active file is something else...
Also closing the macro and run it again with each file is not an option because each user has to browse where to save, where is the signature, the cordsheet etc, info I can't save, unfortunately.
Okay before we get into the coding part, you need to identify "Clearly" what you need to do and then code...
1) What I could understand is that it is not necessary that you run the macro from the document where it is stored...
2) The second thing is to identify the documents where you need to insert the signature, cordsheet, supervisor name and then save the file. Once that is done you want to print it twice as well. Is there a condition or criteria you are using to achieve this?
3) After this is done, what i suggest is that don't try to close the file. Simply save it and move on to the next file. In the end when everything that is supposed to be done is done then use Application.Quit to close all files together.
ps: The only drawback of this method is that if you have " too many (say 10-20)" files opened then it will slow down your system drastically....
Well, I am still guessing on what exactly you are trying to achieve.... There is also another way that I can think of based on my assumptions...
Run the macro from the file it is stored
1) Open the file with which you want to work with
2) do what what you want to do with it
3) close that file and then repeat step one. This way you won't clutter up your memory as well.
Hope this helps....
ps: Going off to sleep. It's already 4:28 AM now :)
Re: Word VBA Macro closes the userform at second run
Thanks for your answers, they give me ideas...
1. No, I don't really need to run the macro from a specific document, I wish I have VB6 installed here so I can make a stand alone program... Oh, the macro is stored in "Normal.dot" (which is hidden?)
2. I could run the macro behind the scenes and process all word files but what if the user has his own notes on a word file open? Basically I open 5 documents and process/close them in an inverse order.
3. That is a good idea but how do I change the active document from within the macro? And there would be anywhere from 1 to 100 open word files at a time...
The other way you suggest is out of way because as I said into a previous post, the user needs first to specify 3 paths and then run the actual code, which would be too time consuming after all...
Thanks a lot, I have to leave work too :) its 4:20 P.M.