chrisgarvey
Technical User
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.
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.