Per Andy Davis:
How do i or can I open a word document directly from Access? I would like to
be able for the user to click a button on my form to open a word document
and then also run a macro in Word.
It will involve writing some VBA code.
This isn't exactly what you're describing, but it's probably close enough to the
the idea:
================================================== ==========================
Private Function wordBegin(theModelName) As String
3000 debugStackPush mModuleName & ": wordBegin: "
3001 On Error GoTo wordBegin_err
' PURPOSE: - Start an instance of MS WORD or use an existing instance
' - Open up a model document and saves it under a unique DOS 8.3
name
' - Leave a global object "gWord" pointing to the Word Basic engine
' behind the newly-opened document so the calling procedure can
OLE to it
' ACCEPTS: DOS 8.3 name of model document to be used for letter
' RETURNS: DOS 8.0 name of the newly-opend letter if successful, zero-length
string if failed
'
' NOTES: 1) We do not want to keep opening up new instances of Word every
time this routine
' is called, so we do the "= Nothing" check to see if gWord has
already been set.
' OTHO the user may have closed that instance of Word, leaving
gWord pointing to
' Neverneverland. Experimentation shows that an error 2753 is
generated in this case.
' Hence the error trap and the "userClosedWord" switch.
' 2) In the FileSaveAs, it is important to force the document type
to Word. Otherwise, if
' the models are Word 7 and the user is in Word 8, the document
will default to .RTF
' and paragraph marks will not work (.RTF needs CRLF wheras Word
used just CR)
Dim modelPath As String
Dim LetterPath As String
Dim dosName As String
Dim problemPath As String
Dim userClosedWord As Integer
Const oleError = 2753
3009 modelPath = pathDatDbGet("tblPerson") & "\Models"
3010 LetterPath = pathDatDbGet("tblPerson") & "\Letters"
On Error Resume Next
MkDir LetterPath
On Error GoTo wordBegin_err
3020 dosName = Format$(recordNumberNextGet("LetterNumber"), "00000000") & ".DOC"
wordBegin_loop:
3390 If (gWord Is Nothing) Or (userClosedWord = 1) Then
3391 Set gWord = CreateObject("Word.Application.8")
3392 End If
3395 problemPath = modelPath & "\" & theModelName
3400 gWord.ChangeFileOpenDirectory (modelPath) 'DMN
3401 gWord.Documents.Open (theModelName)
3405 problemPath = LetterPath & "\" & theModelName
3410 gWord.ChangeFileOpenDirectory (LetterPath) 'DMN
3411 gWord.ActiveDocument.SaveAs (dosName) 'DMN
3999 wordBegin = dosName
wordBegin_xit:
debugStackPop
On Error Resume Next
Exit Function
wordBegin_err:
Select Case Err
Case 2763
MsgBox "Microsoft Word is unable to find " & problemPath & ". Please
notify your administrator", 16, "Cannot Print Form Letter"
Resume wordBegin_xit
Case 2772
MsgBox "Unable to locate Microsoft Word program. Please notify your
administrator", 16, "Cannot Print Form Letter"
Resume wordBegin_xit
Case oleError, mRpcServerUnavailable
If userClosedWord = 0 Then
userClosedWord = userClosedWord + 1
Resume wordBegin_loop
Else
bugAlert "Unable to open MS Word. Suspect user may have closed
existing instance."
Resume wordBegin_xit
End If
Case Else
bugAlert ""
Resume wordBegin_xit
End Select
Resume wordBegin_xit 'Shouldn't be needed, but just in case.....
End Function
================================================== ==========================
--
PeteCresswell