By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,057 Members | 1,346 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,057 IT Pros & Developers. It's quick & easy.

Help needed to create a Word document using data from VB6

P: 3
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
Share this Question
Share on Google+
3 Replies


100+
P: 267
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

P: 3
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

P: 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.