Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 299
  • Last Modified:

Using Word From VB to insert text and a graphic

Ok, here's what I need to accomplish: place two lines of text in a word documnet, followed by an image.  Then it needs to repeat until an eof is found.

I've got the db access and loop setup. I need the code to open word, insert the lines and image.  

The code I have now puts in two lines of text, a blank image between the lines of text and repeats.

This is driving me crazy!!  I don't know enough about word's automation to insert even a line of text. I can do excel with the pros, but Word is being a pain. Any assistance would be greatly appreciated!
0
digitalwav
Asked:
digitalwav
  • 3
  • 2
1 Solution
 
DarkoLordCommented:
Hi...

Dim objWord As Word.Application
Set objWord = New Word.Application

objWord.Documents.Add ', , , True
objWord.Selection.Text = "Blah"
objWord.Selection.EndKey unit:=wdStory
objWord.Selection.InlineShapes.AddPicture "c:\temp\image.jpg"
'objWord.Visible = True

'Save document here

Set objWord = nothing
0
 
EDDYKTCommented:
Have you tried to use record the macro from word?

From tools->macro->record the macro.

Do you things

see code to press Alt-f11
0
 
digitalwavIT Infrastructure ManagerAuthor Commented:
I tried that. The macro code for inserting text doesn't work. The inserting of the picture does, but its placement isn't real concrete
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
digitalwavIT Infrastructure ManagerAuthor Commented:
DarkoLord, I tried your code and bascially it's the same problem. No control over where the image goes and all the images show up as blank spaces.

Here's the code I have using your example:

'open db for parts
Set conn = New ADODB.Connection
Set rs = New ADODB.Recordset
conn.Open NewConnectionString

Dim mystream As New ADODB.Stream
mystream.Type = adTypeBinary

Dim objWord As Word.Application
Set objWord = New Word.Application

objWord.Visible = True

Set rs = conn.Execute("SELECT * FROM win_lic ORDER BY jobnumber")

objWord.Documents.Add ', , , True

Do Until rs.EOF = True
objWord.Selection.Text = rs!jobnumber & vbCr & rs!customer
objWord.Selection.EndKey unit:=wdStory

If IsNull(rs!Image) = False Then
mystream.Open
mystream.Write rs!Image
mystream.SaveToFile "c:\\temp\\tmplicimage.jpg", adSaveCreateOverWrite

objWord.Selection.InlineShapes.AddPicture "c:\temp\tmplicimage.jpg"

mystream.Close

Else

End If
DoEvents
rs.MoveNext
Loop
0
 
DarkoLordCommented:
Well that code I gave you adds the picture to the end of the document...
0
 
digitalwavIT Infrastructure ManagerAuthor Commented:
Ok, I tinkered with it and got DarkoLords code working to my satisfaction. Thanks for the input!!
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now