468,301 Members | 1,531 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,301 developers. It's quick & easy.

Help needed to create a Word document using data from VB6

I am developing a program that gathers inspection visit details and save them in string variables. I need to output this information to a Word template. I have attempted to use Bookmarks, so far without success, the code I have used is as follows:

'printcomplaint
Dim oWord As Word.Application
Dim oDoc As Word.Document
Set oWord = CreateObject("Word.Application")
oWord.Visible = True
oWord.Documents.Add "D:\My Documents\Report Writer\Witness Statement"
'Set oDoc = oWord.Documents.Add
oDoc.Bookmarks("complaint").Range.Text = compnum

This does open the correct template but will not add the bookmark "complaint". When I run the program I get a Runtime error 91 message advising me that Object variable or with Block not set.

Any help would be gratefully received.

Many Thanks
Nov 23 '06 #1
3 5352
albertw
267 100+
I am developing a program that gathers inspection visit details and save them in string variables. I need to output this information to a Word template. I have attempted to use Bookmarks, so far without success, the code I have used is as follows:

'printcomplaint
Dim oWord As Word.Application
Dim oDoc As Word.Document
Set oWord = CreateObject("Word.Application")
oWord.Visible = True
oWord.Documents.Add "D:\My Documents\Report Writer\Witness Statement"
'Set oDoc = oWord.Documents.Add
oDoc.Bookmarks("complaint").Range.Text = compnum

This does open the correct template but will not add the bookmark "complaint". When I run the program I get a Runtime error 91 message advising me that Object variable or with Block not set.

Any help would be gratefully received.

Many Thanks
hi

need to add a bookmark !

Set oDoc = oWord.Documents.Add
oDoc.Bookmarks.Add "Complaint"
oDoc.Bookmarks("Complaint").Range.Text = compnum
Nov 23 '06 #2
Hi,
Thanks to AlbertW for the prompt reply, I added the line of code and am now progressing. The template opens as before and the data (123456) from the VB program is added to the template, unfortunately it adds it at the beginning of the text and not into the bookmark:

123456COMPLAINT NO: {Complaint}

I am convinced that the Range code has something to do with this or perhaps I must do more with the Word template.

My literature is very skimpy on this subject and regretably gives little assistance.

Additional assistance would still be gratefully accepted.

Regards
Nov 24 '06 #3
As a follow up the latest code that I have tried is:

Dim oWord As Word.Application
Dim oDoc As Word.Document
Dim orng As Word.Range
Set oWord = CreateObject("Word.Application")
oWord.Visible = True
Set oDoc = oWord.Documents.Add("D:\My Documents\Report Writer\Witness Statement.doc")

oDoc.Bookmarks.Add "Complaint"
oDoc.Bookmarks.Add "inspqual1"
oDoc.Bookmarks.Add "inspqual2"
oDoc.Bookmarks.Add "inspqual3"
oDoc.Bookmarks.Add "inspqual4"
Set orng = oDoc.Bookmarks("Complaint").Range
oDoc.Bookmarks("Complaint").Range.Text = compnum
Set orng = oDoc.Bookmarks("inspqual1").Range
orng.Text = inspqual(1)
Set orng = oDoc.Bookmarks("inspqual2").Range
orng.Text = inspqual(2)
Set orng = oDoc.Bookmarks("inspqual3").Range
orng.Text = inspqual(3)
Set orng = oDoc.Bookmarks("inspqual4").Range
orng.Text = inspqual(4)

This results in the correct data being placed at the cursor position at the beginning of the document rather than in a Bookmark.
Nov 24 '06 #4

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

6 posts views Thread by naknak | last post: by
6 posts views Thread by cj2 | last post: by
2 posts views Thread by ahmadoubay_20240 | last post: by
reply views Thread by NPC403 | last post: by
reply views Thread by Teichintx | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.