Using Word From VB to insert text and a graphic

Posted on 2005-03-07
Medium Priority
Last Modified: 2008-12-09
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!
Question by:digitalwav
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
LVL 22

Accepted Solution

DarkoLord earned 2000 total points
ID: 13481549

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
LVL 26

Expert Comment

ID: 13485808
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

Author Comment

ID: 13485884
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
Technology Partners: 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!


Author Comment

ID: 13485942
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.Write rs!Image
mystream.SaveToFile "c:\\temp\\tmplicimage.jpg", adSaveCreateOverWrite

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



End If
LVL 22

Expert Comment

ID: 13485976
Well that code I gave you adds the picture to the end of the document...

Author Comment

ID: 13486397
Ok, I tinkered with it and got DarkoLords code working to my satisfaction. Thanks for the input!!

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question