MDB path same as...CurrentProject.Path & "\test.doc"

  • Thread starter Thread starter ryguy7272
  • Start date Start date
R

ryguy7272

I have been experimenting with a new technique (new for me) all day today. I
was trying to modify some code to send data from a From to a Word.doc with
several Bookmarks. Below is my code:

Option Compare Database

Option Compare Database

Sub PatientForm()

Dim appWord As Word.Application
Dim doc As Word.Document
Dim objWord As Object

On Error Resume Next
err.Clear

Set appWord = GetObject(, "Word.Application")
Set objWord = CreateObject("Word.Application")

If err.Number <> 0 Then

objWord.Documents.Open CurrentProject.Path & "\test.doc"
objWord.Visible = True

End If
Set doc = appWord.Documents.Open("C:\PatientForm.doc", , True)
With doc

..FormFields("LastName").Result = Forms!SearchForm!frmsubClients.Form.LastName
..FormFields("FirstName").Result =
Forms!SearchForm!frmsubClients.Form.FirstName
..FormFields("ConsultDate").Result =
Forms!SearchForm!frmsubClients.Form.ConsultDate

..Visible = True
..Activate
End With
Set doc = Nothing
Set appWord = Nothing
Exit Sub
errHandler:
MsgBox err.Number & ": " & err.Description

End Sub

It works, I am just trying to figure out a way to open the Word.doc and
‘push’ the data from the Form to the Word.doc. As I understand it, this code
will open the Word.doc:

Dim objWord As Object

Set objWord = CreateObject("Word.Application")

'Assumes document in same folder as MDB.
objWord.Documents.Open CurrentProject.Path & "\test.doc"
objWord.Visible = True

This works fine, and I love the fact that the MDB is in the same location as
the Word.doc. I am now trying to change the below line of code to make the
location of the Word.doc relative (and not absolute):
Set doc = appWord.Documents.Open("C:\PatientForm.doc", , True)

How can I modify my code to tell Access that the Word.doc and the MDB are
both in the same directory, without specifically mapping to that directory?
I am trying to prepare this for people who know nothing about VBA.

Regards,
Ryan---
 
Hi Ryan,

the same way ...<smile>

Set doc = appWord.Documents.Open( _
CurrentProject.Path & "\PatientForm.doc", , True)

~~~


there are other issues with your code ...

what is your intention here?

Set appWord = GetObject(, "Word.Application")
Set objWord = CreateObject("Word.Application")

you do not seem to be using the appWord object variable. My guess is
that you planned to test to see if Word was already open and use that
instance -- and use CreateObject to create an instance if it was not.
As it is now, you are always creating a new instance of Word

~~~

also, you are not saving or closing the Word document

~~~
it would be a good idea to add an error handler to the code


'~~~~~~~~~~~~~~
'set up Error Handler - 'put this at the top of your program
' -- right after the procedure declaration
On Error GoTo Proc_Err

'...then come the statements of your procedure ...

'then the exit code and error handler statements at the bottom

Proc_Exit:
On Error Resume Next
'close and release object variables if applicable

Exit Sub ' or Function

Proc_Err:

'NOTE: replace ProcedureName with YOUR procedure name

MsgBox Err.Description, , _
"ERROR " & Err.Number _
& " ProcedureName"

Resume Proc_Exit

'if you want to single-step code to find error, CTRL-Break at MsgBox
'then set this to be the next statement
Resume
'~~~~~~~~~~~~~~

WHERE
ProcedureName is the name of your procedure so you can identify what
code the problem is in when you see the error

The line labels do not matter (Proc_Exit:, Proc_Err:), I like to use the
same ones all the time -- they only have to be unique within a procedure.

if you get an error, press CTRL-BREAK when the message box pops up,
Enter to dismiss the dialog box

this takes you into the code

right-click on the *Resume* statement
from the shortcut menu, choose --> Set Next Statement

then press F8 to resume with the statement that caused the problem --
you can fix it! -- or at least see what the problem is

pressing F8 executes one statement at a time

press F5 to continue execution automatically



Warm Regards,
Crystal

remote programming and training

Access Basics
8-part free tutorial that covers essentials in Access
http://www.AccessMVP.com/strive4peace

*
(: have an awesome day :)
*
 
You know, I could swear I tried that Crystal! Anyway, it's working now.
Thanks so much!
Ryan---
 
Back
Top