Hello, I am very new to VBA so my code may be very ugly.
I have is a form in Access that shows a record in a table and I have a button that loads word, and merges that document. I have is I have is the word document all configured, and I set the recordsource, and the query withen VBA. The first time I run the procedure everything works as expected. Howerver if i close the two word letters (the origional word letter, and the merged letter) and then run the procedure again it only loads the origional letter and does not run the merge part. From what I can tell for some reason the data is not being referess correctly... If I exit completely out of the project and restart access it works fine on the first letter I merge, but there after I get the same problem.
Here is my code I have... Keep in mind it works fine once, but the next time I call it the letter does not get merged, and also when I goto Tools-Mail Merge the Datasource Information is not in their (Even though it is the first time)
--------------------------------------------
Public Function loadword(db As String, CurrentID As String)
'****Opens the files database to retrieve the stored location of the word document
Dim tjDb As DAO.Database
Dim tjRst As DAO.Recordset
Dim sql1 As String
Dim sql2 As String
Dim sql3 As String
Dim sql4 As String
Dim sqlstring As String
Set tjDb = CurrentDb
Set tjRst = tjDb.OpenRecordset("SELECT files.[key], files.[filename] from files where (files.
= '" & db & "')", dbOpenDynaset)
tjRst.MoveFirst
temppath = tjRst![filename]
'*** Loads word
On Error Resume Next
Set wrdApp = GetObject(, "word.application"
If Err.Number <> 0 Then
Set wrdApp = CreateObject("word.application"
End If
wrdApp.Documents.Open temppath
wrdApp.Visible = True
sql1 = "SELECT * FROM [" & db
sql2 = "] where [" & db & "].id"
sql3 = " = "
sql4 = CurrentID & ""
sqlstring = sql1 & sql2 & sql3 & sql4
ActiveDocument.MailMerge.OpenDataSource Name:= _
"C:\Personal Lines Letter Databases\Database.mdb", ConfirmConversions:= _
False, ReadOnly:=False, LinkToSource:=True, AddToRecentFiles:=False, _
PasswordDocument:="", PasswordTemplate:="", WritePasswordDocument:="", _
WritePasswordTemplate:="", Revert:=False, Format:=wdOpenFormatAuto, _
Connection:= _
"DSN=MS Access Database;DBQ=C:\Personal Lines Letter Databases\Database.mdb;DriverId=25;FIL=MS Access;MaxBufferSize=2048;PageTimeout=5;" _
, SQLStatement:=sqlstring, SQLStatement1:=""
With ActiveDocument.MailMerge
.Destination = wdSendToNewDocument
.MailAsAttachment = False
.MailAddressFieldName = ""
.MailSubject = ""
.SuppressBlankLines = True
.Execute Pause:=True
End With
Set wrdApp = Nothing
Set tjDb = Nothing
Set tjRst = Nothing
End Function
I have is a form in Access that shows a record in a table and I have a button that loads word, and merges that document. I have is I have is the word document all configured, and I set the recordsource, and the query withen VBA. The first time I run the procedure everything works as expected. Howerver if i close the two word letters (the origional word letter, and the merged letter) and then run the procedure again it only loads the origional letter and does not run the merge part. From what I can tell for some reason the data is not being referess correctly... If I exit completely out of the project and restart access it works fine on the first letter I merge, but there after I get the same problem.
Here is my code I have... Keep in mind it works fine once, but the next time I call it the letter does not get merged, and also when I goto Tools-Mail Merge the Datasource Information is not in their (Even though it is the first time)
--------------------------------------------
Public Function loadword(db As String, CurrentID As String)
'****Opens the files database to retrieve the stored location of the word document
Dim tjDb As DAO.Database
Dim tjRst As DAO.Recordset
Dim sql1 As String
Dim sql2 As String
Dim sql3 As String
Dim sql4 As String
Dim sqlstring As String
Set tjDb = CurrentDb
Set tjRst = tjDb.OpenRecordset("SELECT files.[key], files.[filename] from files where (files.
tjRst.MoveFirst
temppath = tjRst![filename]
'*** Loads word
On Error Resume Next
Set wrdApp = GetObject(, "word.application"
If Err.Number <> 0 Then
Set wrdApp = CreateObject("word.application"
End If
wrdApp.Documents.Open temppath
wrdApp.Visible = True
sql1 = "SELECT * FROM [" & db
sql2 = "] where [" & db & "].id"
sql3 = " = "
sql4 = CurrentID & ""
sqlstring = sql1 & sql2 & sql3 & sql4
ActiveDocument.MailMerge.OpenDataSource Name:= _
"C:\Personal Lines Letter Databases\Database.mdb", ConfirmConversions:= _
False, ReadOnly:=False, LinkToSource:=True, AddToRecentFiles:=False, _
PasswordDocument:="", PasswordTemplate:="", WritePasswordDocument:="", _
WritePasswordTemplate:="", Revert:=False, Format:=wdOpenFormatAuto, _
Connection:= _
"DSN=MS Access Database;DBQ=C:\Personal Lines Letter Databases\Database.mdb;DriverId=25;FIL=MS Access;MaxBufferSize=2048;PageTimeout=5;" _
, SQLStatement:=sqlstring, SQLStatement1:=""
With ActiveDocument.MailMerge
.Destination = wdSendToNewDocument
.MailAsAttachment = False
.MailAddressFieldName = ""
.MailSubject = ""
.SuppressBlankLines = True
.Execute Pause:=True
End With
Set wrdApp = Nothing
Set tjDb = Nothing
Set tjRst = Nothing
End Function