MS Office Forum / Word / Programming / March 2008
Word Macro Code Failed when run it thourgh a template
|
|
Thread rating:  |
BJ - 13 Mar 2008 20:23 GMT 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
Thenks
Jean-Guy Marcil - 13 Mar 2008 20:36 GMT > Hi List, > [quoted text clipped - 21 lines] > 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.
BJ - 14 Mar 2008 21:31 GMT 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.
> > Hi List, > > [quoted text clipped - 47 lines] > document absed on the tempalte will have their logos updated automatically, > no code needed. Jean-Guy Marcil - 17 Mar 2008 15:11 GMT > I tried using 'Document' object instead of using ActiveDocument , but i still > got code failed. [quoted text clipped - 20 lines] > time. but i can't find out where this happend since when i debug, it works > fine. As I wrote in my first reply: "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)."
Also, the way you are using your code, you are copying the last paragraph mark from the logo file into the target document. This last ¶ carries a lot of information, it could even change you headers/footers... Try not to select it. From the look of your code, it looks as though you are dealing with inline shapes. So try the code below and then adapt it to your needs.
This code will copy the first inlineshape in the first paragraph from "logos.doc" to the currently selected inlineshape. You may need code to make sure that only the currently selected inlinshape gets replaced, and not the whole paragraph if the wrong type of range is selected... (If Selection.Type...)
Dim oLogoFile As Document Dim rgeCur As Range
Application.ScreenUpdating = False
If Not Selection.Type = wdSelectionInlineShape Then MsgBox "Make sure you select the logo to be replaced, and nothing else.", _ vbExclamation, "Wrong selection" Exit Sub End If
Set rgeCur = Selection.Range Set oLogoFile = Documents.Open("C:\My Documents\logos.doc") rgeCur.FormattedText = oLogoFile.Range.Paragraphs(1) _ .Range.Characters(1).FormattedText oLogoFile.Close wdDoNotSaveChanges
Application.ScreenRefresh Application.ScreenUpdating = True
BJ - 17 Mar 2008 21:40 GMT Jean-Guy,
the logo file contains 2 logos which are jpg file and also has 4 text description lines. I tried to change my codes, but the logo file still can't be closed somehow.
in the original template, the old logo is saved in a document object. that's why my code has to use a loop to close the other documents not only the logo file. all the 'copy' and 'paste' parts work, but only the 'close' doesn't work.
Set regCur = Selection.Range Set oLogoFile = Application.Documents.Open("C:\Program Files\HRForms\logos.doc") pos1 = oLogoFile.Paragraphs(1).Range.Start pos2 = oLogoFile.Paragraphs(4).Range.End Set regCur = oLogoFile.Range(Start:=pos1, End:=pos2) regCur.Copy Application.Documents(thisDoc).Activate Application.Documents(thisDoc).Shapes("Object 8").Select Application.Selection.ShapeRange.Delete Application.Documents(thisDoc).Shapes("Object 2").Select Application.Selection.ShapeRange.ScaleWidth 1.71, msoFalse, msoScaleFromBottomRight Application.Selection.ShapeRange.ScaleWidth 0.94, msoFalse, msoScaleFromTopLeft Application.Selection.ShapeRange.ScaleHeight 0.81, msoFalse, msoScaleFromTopLeft Application.Selection.ShapeRange(1).OLEFormat.DoVerb VerbIndex:=wdOLEVerbPrimary Application.Selection.WholeStory Application.Selection.Delete Unit:=wdCharacter, Count:=1 Application.Selection.PasteSpecial Link:=False, Placement:=wdInLine, DisplayAsIcon:=False Application.Selection.TypeBackspace Set regCur = Nothing Application.Documents(oLogoFile).Close wdDoNotSaveChanges Set oLogoFile = Nothing Application.Documents("Document in " & thisDoc).Close
> > I tried using 'Document' object instead of using ActiveDocument , but i still > > got code failed. [quoted text clipped - 57 lines] > Application.ScreenRefresh > Application.ScreenUpdating = True Jean-Guy Marcil - 18 Mar 2008 14:31 GMT > Jean-Guy, > > the logo file contains 2 logos which are jpg file and also has 4 text > description lines. I tried to change my codes, but the logo file still can't > be closed somehow. You need to organize this logo file so that the information is easily retrieved. I would place the logos and their descriptions in a table. I would make sure that the logos were inline with text (because they are easy to work with).
> in the original template, the old logo is saved in a document object. that's Floating or inline?
> why my code has to use a loop to close the other documents not only the logo > file. What other documents? I thought there were only two documents: The one that needs its logos updated and "logos.doc" ???
> all the 'copy' and 'paste' parts work, but only the 'close' doesn't work. "doesn't work" does not mean anytjing. What happens exactly when you get to the line that causes the error (which line?)?
Why don't you use a simpler line like:
oLogoFile.Close wdDoNotSaveChanges
???
> Set regCur = Selection.Range This line creates a range that refers to the actual selection in the original document.
> Set oLogoFile = Application.Documents.Open("C:\Program > Files\HRForms\logos.doc") > pos1 = oLogoFile.Paragraphs(1).Range.Start > pos2 = oLogoFile.Paragraphs(4).Range.End > Set regCur = oLogoFile.Range(Start:=pos1, End:=pos2) Now, regCur that refered to a range in the original document, is now pointing to the first 4 paragraphs in "logos.doc".
Why set it twice if the first setting is not used at all?
> regCur.Copy > > Application.Documents(thisDoc).Activate > Application.Documents(thisDoc).Shapes("Object 8").Select > Application.Selection.ShapeRange.Delete Can't these three lines be replaced by: thisDoc.Shapes("Object 8").Delete ???
> Application.Documents(thisDoc).Shapes("Object 2").Select > Application.Selection.ShapeRange.ScaleWidth 1.71, msoFalse, [quoted text clipped - 10 lines] > DisplayAsIcon:=False > Application.Selection.TypeBackspace Again, as I wrote twice already, do not use "Activate" when dealing with mutliple documents, it just causes headaches.
For the last part of your code, try something like this:
With thisDoc.Shapes("Object 2") .ScaleWidth 1.71, msoFalse, msoScaleFromBottomRight .ScaleWidth 0.94, msoFalse, msoScaleFromTopLeft .ScaleHeight 0.81, msoFalse, msoScaleFromTopLeft .OLEFormat.Edit Set objectDoc = .OLEFormat.Object End With
With objectDoc With .Range .Delete .PasteSpecial Link:=False, Placement:=wdInLine, DisplayAsIcon:=False End With .Close End With
oLogoFile.Close wdDoNotSaveChanges Set oLogoFile = Nothing Set regCur = Nothing
> Set regCur = Nothing > Application.Documents(oLogoFile).Close wdDoNotSaveChanges > Set oLogoFile = Nothing > Application.Documents("Document in " & thisDoc).Close thisDoc is a document, not a string as expected in the parenthesis. You probabably need "Document in " & thisDoc.Name). But you should use something like I suggest above instead, you have more control that way.
BJ - 18 Mar 2008 16:13 GMT Hi Jean-Guy,
First of all, i want to say thank you so so so much for you spending your time on this issue.
I changed my code based on your suggestions:
******************************* Set oLogoFile = Application.Documents.Open("C:\Program Files\HRForms\logos.doc") oLogoFile.Tables(1).Select Selection.Copy thisDoc.Shapes("Object 8").Delete With thisDoc.Shapes("Object 2") .ScaleWidth 1.71, msoFalse, msoScaleFromBottomRight .ScaleWidth 0.94, msoFalse, msoScaleFromTopLeft .ScaleHeight 0.81, msoFalse, msoScaleFromTopLeft .OLEFormat.Edit Set objectDoc = .OLEFormat.Object End With With objectDoc With .Range .Delete .PasteSpecial link:=False, placement:=wdInLine, DisplayAsIcon:=False End With .Close End With oLogoFile.Close wdDoNotSaveChanges Set oLogoFile = Nothing ******************************
everything works fine except the 'close'. it just doesn't close the logo file. If i put a breakpoint to the sub that calls these code, without debugging through, it runs perfectly. but without breakpoint, i received an error message "4198 Command failed".
i tried to catch where this error happened, it happened after .copy from the logo file. but actually the code was still running. becase the new logos didn't get copied to the doc, but the logo file wasn't closed.
> > Jean-Guy, > > [quoted text clipped - 101 lines] > probabably need "Document in " & thisDoc.Name). But you should use something > like I suggest above instead, you have more control that way. Jean-Guy Marcil - 18 Mar 2008 17:52 GMT > Hi Jean-Guy, > [quoted text clipped - 8 lines] > oLogoFile.Tables(1).Select > Selection.Copy Try not to use the Selection object at all (even in the other code that you use to call this sub). For instance, the above two lines could be:
oLogoFile.Tables(1).Range.Copy
> thisDoc.Shapes("Object 8").Delete > With thisDoc.Shapes("Object 2") [quoted text clipped - 26 lines] > logo file. but actually the code was still running. becase the new logos > didn't get copied to the doc, but the logo file wasn't closed. Have you tried debugging step by step (with F8) from the start of your routine? There must be something in the code you did not post that interferes.
I tried the following sub and it works:
Sub Test()
Dim oLogoFile As Document Dim thisDoc As Document Dim objectDoc As Object
Set thisDoc = ActiveDocument Set oLogoFile = Application.Documents.Open("C:\My Documents\logos.doc")
Application.ScreenUpdating = False
oLogoFile.Tables(1).Range.Copy oLogoFile.Close wdDoNotSaveChanges
thisDoc.Shapes("Object 8").Delete With thisDoc.Shapes("Object 2") .ScaleWidth 1.71, msoFalse, msoScaleFromBottomRight .ScaleWidth 0.94, msoFalse, msoScaleFromTopLeft .ScaleHeight 0.81, msoFalse, msoScaleFromTopLeft .OLEFormat.Edit Set objectDoc = .OLEFormat.Object End With With objectDoc With .Range .Delete .PasteSpecial link:=False, placement:=wdInLine, DisplayAsIcon:=False End With .Close End With Application.ScreenRefresh Application.ScreenUpdating = True
Set oLogoFile = Nothing
End Sub
BJ - 19 Mar 2008 15:15 GMT Hi Jean-Guy Marcil,
I finally got my code works. Here is my code:
*************************************** thisDoc.Shapes("Object 8").Delete With thisDoc.Shapes("Object 2") .ScaleWidth 1.71, msoFalse, msoScaleFromBottomRight .ScaleWidth 0.94, msoFalse, msoScaleFromTopLeft .ScaleHeight 0.81, msoFalse, msoScaleFromTopLeft Set objectDoc = .OLEFormat.Object End With With objectDoc With .Range .InsertFile FileName:="C:\Program Files\HRForms\logos.doc", Range:="", _ ConfirmConversions:=False, link:=False, Attachment:=False End With End With Set objectDoc = Nothing *************************************************
Thank you so much. without your help, i can make this work. really appreciate your time.
> > Hi Jean-Guy, > > [quoted text clipped - 87 lines] > > End Sub Jean-Guy Marcil - 19 Mar 2008 17:29 GMT > Hi Jean-Guy Marcil, > [quoted text clipped - 22 lines] > Thank you so much. without your help, i can make this work. really > appreciate your time. LOL... So you did not need my help? (I believe you meant: "without your help, I can NOT make this work")
Glad you got working like you want.
|
|
|