Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Bookmarks using word & VB

Status
Not open for further replies.

chrisgarvey

Technical User
Mar 27, 2003
64
0
0
GB
I am working on a program that uses information from an access table to produce a report in word, using a visual basic enviroment.

However I am struggling with regard to inserting bookmarks into word using visual basic to place data in word document.

Using the code below i am able to create bookmark and place the information in the document. However I cannot understand how to create multiple bookmarks on the page:

Dim conn As ADODB.Connection
Dim rs As ADODB.Recordset
'*******For Word Report Generation*******
Dim Y As Word.Application
Function connectDB()
Set conn = New ADODB.Connection
Set rs = New ADODB.Recordset
conn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=False;Data Source= " & App.Path & "\datareport.mdb"
End Function
Function wordreport()
Call connectDB
Set rs = conn.Execute("select * from demo")
Set Y = CreateObject("Word.Application")
With Y
.Documents.Open FileName:=App.Path & "\wordvb.doc"
Call createBookmark

.Selection.GoTo What:=wdGoToBookmark, Name:="Name"
Do Until rs.EOF = True
.Selection.TypeText Text:=rs.Fields(0) & vbTab & rs.Fields(1)
.Selection.TypeParagraph
rs.MoveNext
Loop

.ActiveDocument.Save
.Application.Quit
End With

Set Y = Nothing

End Function

Private Sub Command1_Click()
Call wordreport
End Sub
Function createBookmark()
With ActiveDocument.Bookmarks
.Add Range:=Selection.Range, Name:="Name"
.DefaultSorting = wdSortByName
.ShowHidden = False
End With
End Function

If possible I would like to create a standard document in word with bookmarks already positioned. Then allow Visual basic to import data to the designated bookmark positions.

Is this possible?

Any help would be appreciated,

Chris.

 
I think there is an easier way to do this:
Create a Word MailMerge document.
Use an access query as data source and just drag and drop your bookmarks on the Word document.

Then you can run the mailmerge anytime -> this will pull the data from your db. ;-)

Cheers,
MakeItSo

Andreas Galambos
EDP / Technical Support Specialist
(andreas.galambos@bowneglobal.de)
HP:
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top