4198 opening a Word .docx from Access

Posted on 2010-09-06
Last Modified: 2012-05-10
See code.
Line 60 consistently gives me 4198 error,  "command failed" from Word. The msgbox before tells me the path to the file is correct. And so are the passwords, although they are 9 chars long in a string*16 variable. They have to be this way to make the document open and close without complaints that" the password is too long"...

Any ideas?

Many thanks.
Sub logInNotes(this_form As Form, mode As String, CTYPE As String)
' stick log entry and/or template in Word notes
' "L"   log only
' "T"   template only
' "LT"  both

    Dim blret As Boolean, RTFstring As String

    Dim docapp As Object
    Dim docdoc As Object
    Dim password As String * 16, thislog As String
    Dim junk As Integer
    Dim temppath As String

    ' paragraph and range objects for bookmark/borders code
    Dim rng As Object
    Dim par As Object

    ' open up the file
    On Error GoTo logInNotes_Error

10  On Error GoTo createApp          ' create a Word app
20  Set docapp = GetObject(, "Word.Application")
30  On Error GoTo logInNotes_Error

'40  On Error GoTo notFound
50  password = DLookup("[password readwrite]", "[my details]")

MsgBox Forms!switchboard!currentPath & "\cg4 docs\" & Forms!switchboard!thisuser & "\" & Forms![counselling log].client_on_form & ".docx"
60  Set docdoc = docapp.Documents.Open(Forms!switchboard!currentPath & "\cg4 docs\" & _
                                       Forms!switchboard!thisuser & "\" & Forms![counselling log].client_on_form & ".docx", _
               , , , password, , , password)
70  On Error GoTo logInNotes_Error

    ' timestamp always if we got this far...
90  docapp.Selection.EndKey Unit:=wdStory  ' to the end of text
100 docapp.Selection.Font.size = 11
110 docapp.Selection.TypeText vbCrLf & "___________________________" & vbCrLf   ' timestamp
120 docapp.Selection.TypeText CTYPE & " session on " & _
                              Format(Forms![counselling log]!date_on_form, "MMMM dd, yyyy") & _
                              ", session number " & Forms![counselling log]!sesno & vbCrLf

    ' stick in log entry?
130 If InStr(mode, "L") Then
140     docapp.Selection.Font.size = 13
150     docapp.Selection.TypeText "Log for session: " & vbCrLf
        ' plant starting bookmark

160     docdoc.Bookmarks.Add "start", docapp.Selection.Range

        ' plant text from Access field
171     thislog = Forms![counselling log].[dealt with]      ' convert to string from access field
172     docapp.Selection.TypeText RegExpReplace(thislog, "<(.|\n)*?>") & vbCrLf & vbCrLf

        ' RTF copy
'170     CF_RTF = RegisterClipboardFormat(RTF)
'180     blret = ClipBoard_SetRTFText(Forms![counselling log].[dealt with].rtfText, CF_RTF)    ' from Access...
        'RTFstring = ClipBoard_GetRTFText(CF_RTF)
'190     docapp.Selection.Paste
'200     docapp.Selection.TypeText vbCrLf & vbCrLf

        ' make a range from bookmark to where we are now
        'Set rng = docdoc.Range(docdoc.Bookmarks("start").Range.end, docapp.Selection.end)
210     Set rng = docdoc.Range(docdoc.Bookmarks("start").end, docapp.Selection.end)    ' create range to cover all text imported

        ' enable borders for all paras in this range
220     For Each par In rng.Paragraphs
230         par.Borders.Enable = True
240     Next par
250 End If

    ' stick in template?
260 If InStr(mode, "T") Then
270     temppath = DLookup("[session template path]", "[my details]")
280     If IsNull(temppath) Then
290         Call tellIt(Forms![session log]!Label193, Forms![session log], False)
300     Else
310         docapp.Selection.insertfile temppath
320     End If
330 End If

360 Set rng = Nothing
370 Set par = Nothing

340 docdoc.Close wdSaveChanges
350 docapp.Quit

380 Set docdoc = Nothing
390 Set docapp = Nothing

    'go home

    On Error GoTo 0
    Exit Sub

430 Set docapp = CreateObject("Word.Application")
440 Resume Next

450 Call tellIt(this_form.Label162, this_form, False)
460 Exit Sub


    MsgBox "Error " & err.number & _
         " (" & err.description & _
           ") from >" & err.source & _
           "< in line " & Erl & _
         " in procedure 'logInNotes' of Module 'global'", vbExclamation

End Sub

Open in new window

Question by:Peborgh
  • 3
  • 3

Expert Comment

ID: 33612391
Trying using a shell command to open the document & then get a reference to the object

Author Comment

ID: 33612422
Interesting. Why do you think this will work? Can you give me an example of the code?

Many thanks,

LVL 65

Accepted Solution

rockiroads earned 500 total points
ID: 33612746
GetObject is going to assume word is running as it is trying to find that instance
If you use CreateObject it will create a new word process

Set docapp = CreateObject("Word.Application")
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

LVL 65

Expert Comment

ID: 33612785
Also, to avoid the numerous commas which can be a pain sometimes to remember what arg is what, you can try this

   sFile = Forms!switchboard!currentPath & "\cg4 docs\" & Forms!switchboard!thisuser & "\" & Forms![counselling log].client_on_form & ".docx"
   wordApp.Documents.Open sFile, PasswordDocument:=Password, WritePasswordDocument:=Password

setting your file in a variable is easier to debug and test for

Author Comment

ID: 33615787
rockiroads, your 1st comment: the code deals with absence of existing Word process by going, via on-error, to create one. How is this not optimal?


LVL 65

Expert Comment

ID: 33619331
oh sorry, missed that bit as I was just concentrating on the error at hand
Looking at it more, you might have issues with the password as you have defined it with a length of 16 so it will be padded out to 16. Can u try removing spaces eg

 sFile = Forms!switchboard!currentPath & "\cg4 docs\" & Forms!switchboard!thisuser & "\" & Forms![counselling log].client_on_form & ".docx"
  Set docdoc = docapp.Documents.Open(sFile, PasswordDocument:=Trim$(password), WritePasswordDocument:=Trim$(password2))


Author Closing Comment

ID: 33670710
Although this did not solve the problem it enabled me to improve my code. Full problem was solved in another EE thread.

Featured Post

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
Outlook Free & Paid Tools
The viewer will learn how to simulate a series of sales calls dependent on a single skill level and learn how to simulate a series of sales calls dependent on two skill levels. Simulating Independent Sales Calls: Enter .75 into cell C2 – “skill leve…
The view will learn how to download and install SIMTOOLS and FORMLIST into Excel, how to use SIMTOOLS to generate a Monte Carlo simulation of 30 sales calls, and how to calculate the conditional probability based on the results of the Monte Carlo …

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question