PDA

View Full Version : Inserting a Pic into Word doc via VBA.



lowens1967
01-24-2006, 02:34 PM
Ok, this should be relatively simple, but it seems to be eluding me.


There is a Word Document I want to open using VBA, then insert a jpg image on it.

This is how far I got:

Dim oapp As Word.Application
Dim oword As Word.Document

Set oapp = CreateObject("Word.Application")
Set oword = oapp.Documents.Open(FileName:="C:\Temp\rptDailyAccountingARBillableReport_2.rtf")

oword.Select
Documents.Add

Selection.InlineShapes.AddPicture FileName:="C:\CompanyLogo\ITS_Logo.jpg", LinkToFile:=False, SaveWithDocument:=True

oword.Close


----------------------------------------------------------


But this doesn't work. Any ideas?


Larry

mvidas
01-24-2006, 02:51 PM
Hi Larry,

You're on your way with this.. it looks like you're using Word recorded code in there, which will work, as long as you specify to do it in word. Prior to Documents and Selection, add 'oapp':Sub Larry()
Dim oapp As Word.Application
Dim oword As Word.Document

Set oapp = CreateObject("Word.Application")
Set oword = oapp.Documents.Open(FileName:= _
"C:\Temp\rptDailyAccountingARBillableReport_2.rtf")

oword.Select
oapp.Documents.Add 'why add a document?

oapp.Selection.InlineShapes.AddPicture FileName:="C:\CompanyLogo\ITS_Logo.jpg", _
LinkToFile:=False, SaveWithDocument:=True
End SubMatt

mdmackillop
01-24-2006, 03:21 PM
Hi Larry,
Try the following. Only problem is that it doesn't seem to close Winword completely.

Sub Test()
Dim oapp As Word.Application
Dim oword As Word.Document
Set oapp = CreateObject("Word.Application")
Set oword = oapp.Documents.Open("C:\AAA\Daily.rtf")
oword.InlineShapes.AddPicture FileName:="C:\AAA\Logo.jpg", _
LinkToFile:=False, SaveWithDocument:=True
oword.Close True
Set oword = Nothing
Set oapp = Nothing
End Sub

lowens1967
01-25-2006, 05:56 AM
Wonderful!

However, at the risk of pushing my luck... how would I go about specifying the location on the page?

Any ideas there?

By the way, both your answers were sufficient for what I need, but I'm just trying to get a little fancy, is all.


Larry

mvidas
01-25-2006, 06:43 AM
I do not know how you could specify where to put it, as it seems you're not even making the application visible. But if you decide to make it visible, try the suggestions here: http://www.vbaexpress.com/forum/showthread.php?t=6659
I asked a similar question a couple weeks ago

Malcolm,
Wouldnt word close completely if you added this line after oword.close true: oword.Close True
oapp.Quit
Matt

lowens1967
01-25-2006, 06:55 AM
> I asked a similar question a couple weeks ago

Ah... this also confirms something I meant to ask... which is that Access 2000 report generator has difficulty with inserted pics. If I export them as rtf's, it "forgets" to include the inserted image.

... hence my attempt at inserting the image *after* the rtf has already been created.

Any thoughts on this?


Larry

mvidas
01-25-2006, 01:17 PM
I'm not surprised about the RTFs losing the images.. RTF files are just flat-text files that contain keywords the word processor can interpret as formatting, similar to HTML files.

Though I can't help you, there might be a way to insert the pic at the beginning or end of the word doc programatically if you can change the .Selection to be there. I just don't know how, personally.

lowens1967
01-25-2006, 01:29 PM
That's all I was looking for, actually. I just wanted to be absolutely sure I wasn't overlooking something so ridiculously obvious that even Homer Simpson would get it.


Doh!


Thanks.

Larry