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

Backup Your Microsoft Windows Server®

Backup 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

No matter the version of Windows you are using, you may have some problems with Windows Search running too slow or possibly not running at all. Before jumping into how you can solve this issue, just know there are many other viable alternative deskt…
A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …

863 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now