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 / December 2006

Tip: Looking for answers? Try searching our database.

Loading pics into a Word document

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Lizardo - 06 Dec 2006 17:14 GMT
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.
Greg Maxey - 06 Dec 2006 18:05 GMT
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.

> I'm sure this has been discussed many times, but so far, I haven't
> found anything that works.
[quoted text clipped - 18 lines]
>
> I'll take any suggestions. I'm pretty new at this.
Jay Freedman - 06 Dec 2006 18:48 GMT
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

Signature

Regards,
Jay Freedman
Microsoft Word MVP        FAQ: http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.

> Lizardo,
>
[quoted text clipped - 40 lines]
>>
>> I'll take any suggestions. I'm pretty new at this.
Greg Maxey - 06 Dec 2006 20:05 GMT
Jay,

Certainly looks better.  Thanks.

> 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
[quoted text clipped - 85 lines]
> >>
> >> I'll take any suggestions. I'm pretty new at this.
 
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.