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!
LVL 1
digitalwavIT Infrastructure ManagerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.