Group: microsoft.public.word.vba.general
From: =?Utf-8?B?Qko=?=
Date: Friday, March 14, 2008 3:31 PM
Subject: RE: Word Macro Code Failed when run it thourgh a template

I tried using 'Document' object instead of using ActiveDocument , but i still
got code failed.

Here is what I tried to do. I saw your suggestions about how to get logo
updates. well, the thing is here, if the file is old file, they still want to
keep the old logo, but user can use the old file for new year process by
changing the enter date, so in this case, they want to see the new logos.

that's why i tried to open the logo file and copy the new logos to the file.
here is my code before i run the loop to close the other windows.

Set oLogoFile = Documents.Open("C:\Program Files\HRForms\logos.doc")
Selection.WholeStory
Selection.Copy
oLogoFile.Close wdDoNotSaveChanges
Windows(1).Activate
Selection.PasteAndFormat (wdPasteDefault)
Selection.TypeBackspace
Windows(1).Close

it didn't close the others if i run the macro instead of stepping through.
it looks like i didn't give the macro enough time to finish one action a
time. but i can't find out where this happend since when i debug, it works
fine.

please help.

thank you so much for your time.

"Jean-Guy Marcil" wrote:

> "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.