J
Jed Harrison
I am trying to create documents that populate bookmarks based on an ADO connection to an Access database. What I want to do is have the original document remain open and unchanged, while each record in the table results in a new document being saved without ever being opened. I think I have the idea of how to do this, what I am missing is the understanding of the properties and methods involved. I have so far tried using activedocument.SaveAs, but what this does is change the original, and leaves the final document open upon finishing.
Bonus points if someone can tell me how to remove the VBA code from the documents that are produced.
This is the code I have so far,
Option Explicit
Sub testBookMarks()
'declarations
Dim theCon As ADODB.Connection
Dim theRecSet As ADODB.Recordset
Dim strDataLocation As String
Dim theSQL As String
'create an ADO connection to the access database
Set theCon = New ADODB.Connection
strDataLocation = "C:\Jed\Parks map reports\testparks.mdb"
theCon.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & strDataLocation
'Open the connection
theCon.Open
theSQL = "SELECT * FROM ParkDesc"
'Create a ADO recordset that will contain data from access table
Set theRecSet = theCon.Execute(theSQL)
Call getData(theRecSet)
'do clean up
'close the ADO connection
theCon.Close
'set objects to nothing
Set theCon = Nothing
Set theRecSet = Nothing
End Sub
Sub getData(theRecSet As ADODB.Recordset)
Dim intFieldCount As Integer
intFieldCount = theRecSet.Fields.Count
Dim strFieldName As String
Dim strUpdateText As String
Dim i As Integer
Dim intTest As Integer 'Delete after testing
Do While Not theRecSet.EOF
For i = 0 To intFieldCount - 1
strFieldName = theRecSet.Fields(i).Name
If Not IsNull(theRecSet.Fields(i).Value) Then
strUpdateText = theRecSet.Fields(i).Value
Else
End If
Call UpdateBookmark(strFieldName, strUpdateText)
Next
intTest = intTest + 1
ActiveDocument.SaveAs "C:\Jed\Parks map reports\Park Report Test\test " & CStr(intTest)
'Here is where I want to save a new version without changing the origianl document
theRecSet.MoveNext
Loop
End Sub
Sub UpdateBookmark(BookmarkToUpdate As String, TextToUse As String)
Dim BMRange As Range
Set BMRange = ActiveDocument.Bookmarks(BookmarkToUpdate).Range
BMRange.Text = TextToUse
ActiveDocument.Bookmarks.Add BookmarkToUpdate, BMRange
End Sub
Bonus points if someone can tell me how to remove the VBA code from the documents that are produced.
This is the code I have so far,
Option Explicit
Sub testBookMarks()
'declarations
Dim theCon As ADODB.Connection
Dim theRecSet As ADODB.Recordset
Dim strDataLocation As String
Dim theSQL As String
'create an ADO connection to the access database
Set theCon = New ADODB.Connection
strDataLocation = "C:\Jed\Parks map reports\testparks.mdb"
theCon.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & strDataLocation
'Open the connection
theCon.Open
theSQL = "SELECT * FROM ParkDesc"
'Create a ADO recordset that will contain data from access table
Set theRecSet = theCon.Execute(theSQL)
Call getData(theRecSet)
'do clean up
'close the ADO connection
theCon.Close
'set objects to nothing
Set theCon = Nothing
Set theRecSet = Nothing
End Sub
Sub getData(theRecSet As ADODB.Recordset)
Dim intFieldCount As Integer
intFieldCount = theRecSet.Fields.Count
Dim strFieldName As String
Dim strUpdateText As String
Dim i As Integer
Dim intTest As Integer 'Delete after testing
Do While Not theRecSet.EOF
For i = 0 To intFieldCount - 1
strFieldName = theRecSet.Fields(i).Name
If Not IsNull(theRecSet.Fields(i).Value) Then
strUpdateText = theRecSet.Fields(i).Value
Else
End If
Call UpdateBookmark(strFieldName, strUpdateText)
Next
intTest = intTest + 1
ActiveDocument.SaveAs "C:\Jed\Parks map reports\Park Report Test\test " & CStr(intTest)
'Here is where I want to save a new version without changing the origianl document
theRecSet.MoveNext
Loop
End Sub
Sub UpdateBookmark(BookmarkToUpdate As String, TextToUse As String)
Dim BMRange As Range
Set BMRange = ActiveDocument.Bookmarks(BookmarkToUpdate).Range
BMRange.Text = TextToUse
ActiveDocument.Bookmarks.Add BookmarkToUpdate, BMRange
End Sub