Insert Picture from File using Word VBA

L

Levv

I was trying to use VBA in Word to import about 60 picture files into
word (with the HPGL file format).

I recorded a macro (insert menu, picture -> from file) and modified it
to make it into a for loop. I am running into a problem. The program
works fine for inserting the first few picture files and then gives an
error saying the filename is not recognized. (But I think the
filenames are correct.)


I think the program works for the first few files because I have
already inserted those ones manually using the insert menu in word.


Is there any way to fix this problem or get around it?


Here is part of my code:


Sub Macro1()


Dim busNo(1 To 200)


busNo(1) = 901
busNo(2) = 903
.........


For i = 1 To 62


Selection.InlineShapes.AddPicture FileName:= _
"D:\v29jul06\gstr_" & busNo(i), _
LinkToFile:=False, SaveWithDocument:=True


Next i


End Sub
 
J

Jean-Guy Marcil

Levv was telling us:
Levv nous racontait que :
I was trying to use VBA in Word to import about 60 picture files into
word (with the HPGL file format).

I recorded a macro (insert menu, picture -> from file) and modified it
to make it into a for loop. I am running into a problem. The program
works fine for inserting the first few picture files and then gives an
error saying the filename is not recognized. (But I think the
filenames are correct.)


I think the program works for the first few files because I have
already inserted those ones manually using the insert menu in word.


Is there any way to fix this problem or get around it?

First, it is better to explicitly declare variable instead of letting the
compiler do the conversion for you. Otherwise you might get unexpected
results that can be hard to track down.
Second, try this code to see what is going on:

'_______________________________________
Dim busNo(1 To 200) As String

busNo(1) = 901
busNo(2) = 903
'etc.

For i = 1 To 62
MsgBox "D:\v29jul06\gstr_" & busNo(i)
Selection.InlineShapes.AddPicture FileName:= _
"D:\v29jul06\gstr_" & busNo(i), _
LinkToFile:=False, SaveWithDocument:=True
Next i
'_______________________________________

Before you insert the picture, you will get a message box with he file name:
The first one you get is:
D:\v29jul06\gstr_901
Is there such a file as called "gstr_901" on your system?
Shouldn't there be an extension of some sort, like: "gstr_901.jpg" or
"gstr_901.tif" ???

Finally, try not to use the selection object, it is slower and unreliable.
Play around with something like:

'_______________________________________
Dim busNo(1 To 200) As String
Dim rgeStart As Range

busNo(1) = 901
busNo(2) = 903
'etc.

Set rgeStart = Selection.Range

For i = 1 To 62
With rgeStart
.Collapse wdCollapseEnd
.InlineShapes.AddPicture FileName:= _
"D:\v29jul06\gstr_" & busNo(i) & ".jpg", _
LinkToFile:=False, SaveWithDocument:=True
End With
Next i
'_______________________________________

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
L

Levv

Hi

Thanks for your reply. I guess I should have mentioned that the file
exists and it does not have an extension. (it has no type) It is not a
..jpg or.tiff or .png file.

Normally, I insert it from Insert menu

Insert -> Picture ->from File

then select the file, and then specify that it is HPGL format.

I have tried using this macro for .png files (or .jpg) and it seems to
work for that. The problem may have to do with the file format and
conversion.

===

Also, I have tried to use the range and collapse. But for range, I
don't really have any paragraphs in the document, so not sure how to
specify range. I just want to insert the pictures one after another in
an empty Word document.

I think right now, using range and collapse, the pictures are inserted
but overlapped on each other. (which means you can only see the one on
top, if you insert two pictures)
 
L

Levv

Also, here is sample code that I used. And this is Word 2003.

Set myRange = ActiveDocument.Paragraphs(1).Range
myRange.Collapse Direction:=wdCollapseEnd
ActiveDocument.Shapes.AddPicture _
FileName:="D:\v29jul06\untitled2.png", _
LinkToFile:=True, SaveWithDocument:=True
myRange.MoveEnd Unit:=wdCharacter, Count:=-1


Set myRange = ActiveDocument.Paragraphs(1).Range
myRange.Collapse Direction:=wdCollapseEnd
ActiveDocument.Shapes.AddPicture _
FileName:="D:\v29jul06\untitled.jpg", _
LinkToFile:=True, SaveWithDocument:=True
myRange.MoveEnd Unit:=wdCharacter, Count:=-1
 
J

Jean-Guy Marcil

Levv was telling us:
Levv nous racontait que :
Also, here is sample code that I used. And this is Word 2003.

Set myRange = ActiveDocument.Paragraphs(1).Range
myRange.Collapse Direction:=wdCollapseEnd
ActiveDocument.Shapes.AddPicture _
FileName:="D:\v29jul06\untitled2.png", _
LinkToFile:=True, SaveWithDocument:=True
myRange.MoveEnd Unit:=wdCharacter, Count:=-1


Set myRange = ActiveDocument.Paragraphs(1).Range
myRange.Collapse Direction:=wdCollapseEnd
ActiveDocument.Shapes.AddPicture _
FileName:="D:\v29jul06\untitled.jpg", _
LinkToFile:=True, SaveWithDocument:=True
myRange.MoveEnd Unit:=wdCharacter, Count:=-1

I have just tested this code and it works fine. The picture are inserted one
after the other. My previous code was fine, but the end result was that the
pictures appeared in reverse order in the document. I had forgotten to
include the new picture in the range before collapsing it.

As far as your "typeLess" image file... I am afraid I cannot help you there.
I have never worked with HPGL files and I have never seen data files (images
or text) without a type or an extension.The only extension-free files I have
seen are application specific setting or system files (Norton as some of
those for example).

'_______________________________________
Dim rgeStart As Range

Set rgeStart = ActiveDocument.Range.Paragraphs(1).Range

For i = 1 To 3
With rgeStart
.Collapse wdCollapseEnd
.InlineShapes.AddPicture FileName:= _
"X:\Test\Pix_0" & i & ".jpg", _
LinkToFile:=False, SaveWithDocument:=True
.MoveEnd wdCharacter, 1
End With
Next i
'_______________________________________

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 

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