473,320 Members | 2,083 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Opening Word Document and passing information to next new line

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

May 7 '06 #1
2 3342
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


May 8 '06 #2
Thank you for the response!

Works well.

-Brian

May 8 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Novice | last post by:
Hi all, I'm a novice VB.NET and VBA programmer and am wondering if there is a tutorial out there on how to create a word document based on data in a database and then return that document to a web...
0
by: Sivaraman.S | last post by:
Issue : When I am trying to open a Word Document or a Template from a Web Application (ASP.NET Web page - Language Used VB.NET Visual Studio Version 2003) using Word.ApplicationClass (Refering...
3
by: John | last post by:
Hi I am using the code given at the end to open a word document from within vb.net. It works fine the first time but the second time I get the error; Unhandled Exception:...
1
by: ourspt | last post by:
Hi, We are working on an ASP.NET (VB.NET) application and we have a problem opening a word document from our application. When we try to open the word document, we do not get any error...
7
by: mkorch17 | last post by:
Hello, I have a server with a Word document and an Access database on it. I also have an ASP page running on the server. The Word document contains a mail merge with the Access database. What...
3
by: Csaba Gabor | last post by:
If I do not have the indicated Word document open (on my Win XP Pro machine with PHP 5.2), the following will open and display it: <?php $path = "c:\\path\\to\\word\\document\\mydoc.doc"; $doc...
3
by: =?Utf-8?B?ZWQ=?= | last post by:
Windows xp: When I try to open a word document, I get the message that "a dialogue box is open, close the dialogue box and try again." I don't know what to do. I don't have word, but I have...
0
by: AparnaKulkarni | last post by:
Hello, I m working with Templates in MS Word. I have established the user Template locations in Word with ".dot" file But after that I m getting error as "Invalid procedure call or argument...
0
theBond
by: theBond | last post by:
HI! I am trying to open word document on server through c# from client web interface but i am not getting success. can anyone guide me on how can i open doc file which is on server through web...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.