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

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

"Jean-Guy Marcil" wrote:

> "BJ" wrote:
>
> > 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.
>
> 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
>

Safety Articles | Usenet Groups | Usenet News | Bluegrass