Log in

View Full Version : Open and save a word document in Access.



boneKrusher
02-02-2006, 06:14 PM
Hi all,

I have a form which opens a word template & inserts a bunch of bookmarks. I then want to automaticly save the document to a location on the server using the "activedocument.saveas" function.

The Word document is generated but I keep getting an error saying that there is no active document. Here is the code. What am I doing wrong?

Private Sub Command10_Click()
DoCmd.RunMacro ("refresh")
Dim Path As String
Dim objWord As Object
Pathx = CurrentProject.Path & "\images\MX-110-3.dot"
Path = CurrentProject.Path & "\images\"

Dim DTNow As String
DoCmd.SetWarnings False

DTNow = Format(Date, "mmmm d, yyyy")

If IsNull(Forms![StartNew]![Vender_Setup].Form![res]) Then MsgBox ("You have not set a response date for this Audit. Set a Response date and then try again."): End

'Set word as an application and make it invisible
Set objWord = CreateObject("Word.Application")
objWord.Visible = False 'True is visible

'path and name of the template your are using.
objWord.Documents.Add (Pathx)

'This is for the bookmark that you created in the template
objWord.ActiveDocument.Bookmarks("first").Select
objWord.Selection.Text = Forms![StartNew]![Vender_Info_sub].Form![CONTACT: First Name]


objWord.ActiveDocument.Bookmarks("Last").Select
objWord.Selection.Text = Forms![StartNew]![Vender_Info_sub].Form![CONTACT: Last Name]

objWord.ActiveDocument.Bookmarks("daten").Select
objWord.Selection.Text = DTNow

objWord.ActiveDocument.Bookmarks("year").Select
objWord.Selection.Text = Forms![StartNew]![Vender_Setup].Form![Combo23]

objWord.ActiveDocument.Bookmarks("audnum").Select
objWord.Selection.Text = Forms![StartNew]![Vender_Setup].Form![AuditNumber]

If IsNull(Me.findings) Then MsgBox ("You must enter a finding first"): End

objWord.ActiveDocument.Bookmarks("finding").Select
objWord.Selection.Text = Me.findings

objWord.ActiveDocument.Bookmarks("dateres").Select
objWord.Selection.Text = Forms![StartNew]![Vender_Setup].Form![res]
objWord.Visible = True

Dim FileN As String
Dim both As String
FileN = Me.Year & "-" & Me.aud_number & "-" & Me.FindNum & ".doc"
both = Path & FileN

If MsgBox("Would you like to save this document?", vbYesNo, "Save?") = vbYes Then

ActiveDocument.SaveAs FileName:=both

End If
Set objWord = Nothing

End Sub

matthewspatrick
02-02-2006, 06:44 PM
On the line where it fails, you need to qualify ActiveDocument with a reference to your Word Application object variable (objWord).

Patrick

boneKrusher
02-02-2006, 07:10 PM
Thanks! it works great
objWord.ActiveDocument.SaveAs FileName:=both