Here is a function I use, and it works fine.
You really should define bookmarks in a Word template, like: name,
address, zip code, city etc.
Now, in the form you should call this function by:
Private Sub cmdMergeWithWord_Click()
On Error GoTo Err_FW
Dim strM As String
strM = DLookup("[LetterPat]", "tblFirmaInfo", "[FirmaID] = 1")
MakeAWordDoc strM
Exit_FW:
Exit Sub
Err_FW:
MsgBox Err.Number & " " & "CUSTOMER/FW" & Chr(13) _
& Err.Description, vbOKOnly
Resume Exit_FW
End Sub
Here is the function(In it's own module):
Public Function MakeAWordDoc(ByVal strFile as String)
On Error Resume Next
Dim dbMTW As DAO.Database
Dim rsMTW As DAO.Recordset
Dim strKUNUM, strKUNAVN, strKUADRESS, strPNR, strSTD, strVR As String
Dim Wordobj As Word.Application
strKUNUM = DLookup("[Custnr]", "tblCustomer", "[CustID] = " &
Forms!frmCustomer!CustID)
strKUNAVN = UCase(DLookup("[AName]", "tblCustomer", "[CustID] = " &
Forms!frmCustomer!CustID))
strKUADRESS = UCase(DLookup("[Adress]", "tblustomer", "[CustID] = " &
Forms!frmCustomer!CustID))
strPNR = DLookup("[Postnr]", "tblPostnr", "[PostnrID] = " &
Forms!frmCustomer!PostnrID)
strSTD = DLookup("[Sted]", "tblPostnr", "[PostnrID] = " &
Forms!frmCustomer!PostnrID)
strVR = DLookup("[VaarRef]", "tblFirmaInfo", "[FirmaID] = 1")
Set dbMTW = CurrentDb()
Set rsMTW = dbMTW.OpenRecordset("tblCustomer", dbOpenTable)
rsMTW.Index = "PrimaryKey"
rsMTW.Seek "=", Forms!frmCustomer!CustID
'If rsMTW.NoMatch Then
'MsgBox "Did not find a customer with that name!", vbOKOnly
'Exit Function
'End If
DoCmd.Hourglass True
Set Wordobj = GetObject(, "Word.Application")
If Err.Number <> 0 Then
Set Wordobj = CreateObject("Word.Application")
End If
Wordobj.Visible = True
Wordobj.Documents.Add _
Template:=strFile, _
NewTemplate:=False
Wordobj.Selection.GoTo what:=wdGoToBookmark, name:="KNR"
Wordobj.Selection.TypeText "K.nr.: " & strKUNUM
'UCase(rsMTW![Custnr])
Wordobj.Selection.GoTo what:=wdGoToBookmark, name:="NAVN"
Wordobj.Selection.TypeText strKUNAVN
'UCase(rsMTW![AName])
Wordobj.Selection.GoTo what:=wdGoToBookmark, name:="ADRESS"
Wordobj.Selection.TypeText strKUADRESS
'UCase(rsMTW![Adress])
Wordobj.Selection.GoTo what:=wdGoToBookmark, name:="POSTNR"
Wordobj.Selection.TypeText strPNR
Wordobj.Selection.GoTo what:=wdGoToBookmark, name:="STED"
Wordobj.Selection.TypeText UCase(strSTD)
Wordobj.Selection.GoTo what:=wdGoToBookmark, name:="DR"
Wordobj.Selection.TypeText "Deres ref.: " & UCase(rsMTW![Referanse])
Wordobj.Selection.GoTo what:=wdGoToBookmark, name:="VR"
Wordobj.Selection.TypeText "Vår ref.: " & UCase(strVR)
Wordobj.Selection.GoTo what:=wdGoToBookmark, name:="DT"
Wordobj.Selection.TypeText Date
DoEvents
Wordobj.Activate
Set Wordobj = Nothing
DoCmd.Hourglass False
Exit Function
TemplateError:
Set Wordobj = Nothing
Exit Function
End Function
BerkshireGuy skrev:
I want to open a word document from an Access form and transfer data
from the Access form to the Word Document.
For instance, if a user clicks an option box on my access form, I want
to pass certain data to the next new line of a opened word doc.
Here is what I tried, but doesnt seem to work:
Dim objWord As Object
Dim NewRow As Row
Set objWord = CreateObject("Word.Application")
objWord.Visible = True
objWord.Documents.Open "U:\May 6.doc"
Set NewRow = objWord.Tables(1).Rows.Add
With NewRow
.Cells(1).Range.Text = Format(Now(), "HH:MM") & " " & "HRS PD"
.Cells(2).Range.Text = "Police on property at this time."
End With
Thanks,
Brian