Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / October 2006

Tip: Looking for answers? Try searching our database.

Insert Picture from File using Word VBA

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Levv - 26 Sep 2006 19:13 GMT
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
Jean-Guy Marcil - 27 Sep 2006 01:07 GMT
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).
[quoted text clipped - 9 lines]
>
> 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
'_______________________________________

Signature

Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
jmarcilREMOVE@CAPSsympatico.caTHISTOO
Word MVP site: http://www.word.mvps.org

Levv - 27 Sep 2006 17:58 GMT
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)

> Levv was telling us:
> Levv nous racontait que :
[quoted text clipped - 69 lines]
> jmarcilREMOVE@CAPSsympatico.caTHISTOO
> Word MVP site: http://www.word.mvps.org
Levv - 27 Sep 2006 18:12 GMT
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

> Hi
>
[quoted text clipped - 96 lines]
> > jmarcilREMOVE@CAPSsympatico.caTHISTOO
> > Word MVP site: http://www.word.mvps.org
Jean-Guy Marcil - 27 Sep 2006 19:51 GMT
Levv was telling us:
Levv nous racontait que :

> Also, here is sample code that I used. And this is Word 2003.
>
[quoted text clipped - 38 lines]
>> inserted but overlapped on each other.  (which means you can only
>> see the one on top, if you insert two pictures)

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
'_______________________________________

Signature

Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
jmarcilREMOVE@CAPSsympatico.caTHISTOO
Word MVP site: http://www.word.mvps.org

Russ - 15 Oct 2006 00:44 GMT
Levv,

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

Doing a google search with terms 'word 2003 hpgl' brought up this webpage.
Maybe it has enough info to explain why your inserts don't work.
http://www.mcse.ms/archive138-2005-4-1427655.html
You may have to export them as a different format other than hpgl from the
original application that generated the files, so that Word can insert them.
Or use a conversion application to change them to a more common format.

>>     Set myRange = ActiveDocument.Paragraphs(1).Range
>>    myRange.Collapse Direction:=wdCollapseEnd
[quoted text clipped - 63 lines]
> Next i
> '_______________________________________

Signature

Russ

drsmN0SPAMikleAThotmailD0Tcom.INVALID

Russ - 15 Oct 2006 01:18 GMT
Levv,
Here's another interesting message thread on hpgl:

http://tinyurl.com/yzjznh

> Levv,
>
[quoted text clipped - 77 lines]
>> Next i
>> '_______________________________________

Signature

Russ

drsmN0SPAMikleAThotmailD0Tcom.INVALID

 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.