Code to Insert GIF files into a word document...

K

Kenny

I have written some code to insert two gif files into a
word document. The code seems to work fine during the
first execution, but as soon as it runs again for the
second time i get error 462: remote server does not exist
or can't be found. It happens on the first AddPicture
statement...anyone have any ideas...thanks in advance.
here is the code:

' create the file path for the first GIF file
lsGIFImage = mid(psSourceFilePath, 1, Len
(psSourceFilePath) - 4) & ".gif"

Set lrWord = New Word.Application
lrWord.DisplayAlerts = wdAlertsNone

' create a new document from a template
Set lrDoc = lrWord.Documents.Add("ROMSWordTemplate1")
If lrDoc Is Nothing Then
lrWord.Quit wdDoNotSaveChanges
Set lrWord = Nothing
CreateOutputReport = False
Exit Function
End If

' insert the GIF's (this is where i get the error the
second time to code runs. it works the first time
thru!
Set lrShape = lrDoc.Shapes.AddPicture(lsGIFImage,
False, True, InchesToPoints(1.5), 1, InchesToPoints(6),
InchesToPoints(3.5))
lrDoc.Shapes.AddPicture mid(lsGIFImage, 1, Len
(lsGIFImage) - 4) & "-b.gif", False, True, 1,
lrShape.Height + 10
Set lrShape = Nothing

' save the new document with this file name...
lrDoc.SaveAs mid(psSourceFilePath, 1, Len
(psSourceFilePath) - 4) & ".doc"

' exit and clean up
Set lrDoc = Nothing
lrWord.Quit wdDoNotSaveChanges
Set lrWord = Nothing
 
J

JGM

Kenny said:
I have written some code to insert two gif files into a
word document. The code seems to work fine during the
first execution, but as soon as it runs again for the
second time i get error 462: remote server does not exist
or can't be found. It happens on the first AddPicture
statement...anyone have any ideas...thanks in advance.
here is the code:

' create the file path for the first GIF file
lsGIFImage = mid(psSourceFilePath, 1, Len
(psSourceFilePath) - 4) & ".gif"

Set lrWord = New Word.Application
lrWord.DisplayAlerts = wdAlertsNone

' create a new document from a template
Set lrDoc = lrWord.Documents.Add("ROMSWordTemplate1")
If lrDoc Is Nothing Then
lrWord.Quit wdDoNotSaveChanges
Set lrWord = Nothing
CreateOutputReport = False
Exit Function
End If

' insert the GIF's (this is where i get the error the
second time to code runs. it works the first time
thru!

My guess is that you have to add the application reference to all methods
and functions, regardless of their location in the code (either to the right
or the left of an equal sign for example).
So, in your case, since you are instancing Word form some other app (which
you do not mention by the way), you probably have to add "lrWord" in your
code as follows:

Set lrShape = lrDoc.Shapes.AddPicture(lsGIFImage,
False, True, InchesToPoints(1.5), 1, lrWord.InchesToPoints(6),
lrWord.InchesToPoints(3.5))

See the following KB article:
http://support.microsoft.com/default.aspx?scid=kb;en-us;189618
Set lrShape = lrDoc.Shapes.AddPicture(lsGIFImage,
False, True, InchesToPoints(1.5), 1, InchesToPoints(6),
InchesToPoints(3.5))
lrDoc.Shapes.AddPicture mid(lsGIFImage, 1, Len
(lsGIFImage) - 4) & "-b.gif", False, True, 1,
lrShape.Height + 10
Set lrShape = Nothing

' save the new document with this file name...
lrDoc.SaveAs mid(psSourceFilePath, 1, Len
(psSourceFilePath) - 4) & ".doc"

' exit and clean up
Set lrDoc = Nothing
lrWord.Quit wdDoNotSaveChanges
Set lrWord = Nothing

HTH
Cheers!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top