"BJ" wrote:
> Hi List,
>
> I have a word template. once user entered a date, the code will check to
> decide wheter need to add new logos to the file. for doing this, the macro
> needs to open another file which contians the new logos, then copy over to
> the file, then close the logo file.
>
> when i ran the macro step though clicking F8. it works fine. but when i
> tried to run it like a user would be, the macro failed becasue the code
> actully didn't close the logo file.
>
> since it works fine when i debugging it, i really can't find the problem.
>
> can someone please help?
>
> here is the code how i close the docs other than the template:
>
> If Windows.Count > 1 Then
> For I = Windows.Count To 1 Step -1
> Windows(I).Activate
> If ActiveDocument.Name <> thisDoc Then
> ActiveDocument.ActiveWindow.Close
> End If
> Next I
> End If
Do not use ActiveDocument when dealing with more than one document in code.
Use a document object for each document involved (2 objects in this case).
This way, you will not need statements such as ".Activate".
For example, all the code you posted above could be replaced with:
If Documents.Count > 1 Then
docLogo.Close wdDoNotSaveChanges
End If
That is, if you used something like this before in the code:
Set docLogo = Documents.Open("C:\myPath\Document with Logo.doc")
Also, if you did open this document, you know for a fact that you have two
documents opened, so the code I wrote above does not need to be incased in an
If block:
docLogo.Close wdDoNotSaveChanges
By the way, if you use this other file solely for getting logos, you could
use the template instead and use AutoText entries instead. In the document,
use AutoText field. This way, when you update the logo in the tempalte, all
document absed on the tempalte will have their logos updated automatically,
no code needed.