Group: microsoft.public.word.vba.general
From: =?Utf-8?B?SmVhbi1HdXkgTWFyY2ls?=
Date: Tuesday, March 18, 2008 11:52 AM
Subject: RE: Word Macro Code Failed when run it thourgh a template

"BJ" wrote:

> 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

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

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

Safety Articles | Usenet Groups | Usenet News | Bluegrass