Loading pics into a Word document

L

Lizardo

I'm sure this has been discussed many times, but so far, I haven't
found anything that works.

We are trying to create 100+ reports with identical layout - but with
different charts per report. Most of the rest of the report is the
same. I'm trying to write a program that will create a Word document,
place the pics at the right place - and change the text as needed.

The biggest problem for me is there's no way to ensure the pic *size*.
I need to be able to resize it as I load it. I've tried a couple things
- set a bookmark, add a picture - but the pic comes up blank.

I could sure use some suggestions. I don't mind discussing at length
because I have a lot of time to do this. Once I solve the pic problem,
the rest should fall neatly into place. I can resize the blank pics -
and I've verified they aren't blank already. I've tried jpegs, gifs,
tiffs, whatever. If I just do a

Bookmark.Range.InlineShapes.AddPicture statement - I *can* see the pic,
but it's huge, and I haven't figured out how to resize it.

I'll take any suggestions. I'm pretty new at this.
 
G

Greg Maxey

Lizardo,

As long as the bookmark spans a range of at least one character (even a
blank space), you might be able to use something like this:

Sub ScratchMacro()
With ActiveDocument.Bookmarks("PicA").Range
.InlineShapes.AddPicture FileName:= _
"C:\Documents and Settings\gregory.maxey\My Documents\My
Pictures\Sample.jpg" _
, LinkToFile:=False, SaveWithDocument:=True,
Range:=ActiveDocument.Bookmarks("PicA").Range
End With
With ActiveDocument.Bookmarks("PicA").Range.InlineShapes(1)
.Height = 20
.Width = 60
End With
End Sub

Pretty crude, but I am off to a meeting and don't have time to refine.
 
J

Jay Freedman

It's better to use the function form of the AddPicture method (that is, put
parentheses around the parameter list, and assign the return value to an
InlineShape object representing the inserted picture). That lets you
manipulate the picture's size and any other properties, without having to
resort to the ActiveDocument.Bookmarks("PicA").Range.InlineShapes(1)
expression. Besides that, you can reassign the bookmark to the InlineShape
object's range at the end, so the original bookmark could be collapsed.

Sub foo()
Dim picRg As Range
Dim picILS As InlineShape
Dim picFile As String
Dim picBookmarkName As String
Dim picDesiredWidth As Single ' in inches
Dim picRatio As Single

picFile = "C:\temp\sample.jpg"

picBookmarkName = "bk1" ' change as needed
picDesiredWidth = 3.2 ' change as needed

Set picRg = ActiveDocument.Bookmarks(picBookmarkName).Range

Set picILS = ActiveDocument.InlineShapes.AddPicture( _
FileName:=picFile, LinkToFile:=False, Range:=picRg)

With picILS
' .LockAspectRatio = msoTrue doesn't work
' so you have to do it yourself
picRatio = CSng(.Height) / CSng(.Width)
.Width = InchesToPoints(picDesiredWidth)
.Height = picRatio * .Width
End With

ActiveDocument.Bookmarks.Add Name:=picBookmarkName, _
Range:=picILS.Range
End Sub

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.
 
G

Greg Maxey

Jay,

Certainly looks better. Thanks.

Jay said:
It's better to use the function form of the AddPicture method (that is, put
parentheses around the parameter list, and assign the return value to an
InlineShape object representing the inserted picture). That lets you
manipulate the picture's size and any other properties, without having to
resort to the ActiveDocument.Bookmarks("PicA").Range.InlineShapes(1)
expression. Besides that, you can reassign the bookmark to the InlineShape
object's range at the end, so the original bookmark could be collapsed.

Sub foo()
Dim picRg As Range
Dim picILS As InlineShape
Dim picFile As String
Dim picBookmarkName As String
Dim picDesiredWidth As Single ' in inches
Dim picRatio As Single

picFile = "C:\temp\sample.jpg"

picBookmarkName = "bk1" ' change as needed
picDesiredWidth = 3.2 ' change as needed

Set picRg = ActiveDocument.Bookmarks(picBookmarkName).Range

Set picILS = ActiveDocument.InlineShapes.AddPicture( _
FileName:=picFile, LinkToFile:=False, Range:=picRg)

With picILS
' .LockAspectRatio = msoTrue doesn't work
' so you have to do it yourself
picRatio = CSng(.Height) / CSng(.Width)
.Width = InchesToPoints(picDesiredWidth)
.Height = picRatio * .Width
End With

ActiveDocument.Bookmarks.Add Name:=picBookmarkName, _
Range:=picILS.Range
End Sub

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.
 

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