• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 848
  • Last Modified:

4198 opening a Word .docx from Access

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

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

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

logInNotes_Error:

    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

0
Peborgh
Asked:
Peborgh
  • 3
  • 3
1 Solution
 
OxonDevCommented:
Trying using a shell command to open the document & then get a reference to the object
0
 
PeborghAuthor Commented:
Interesting. Why do you think this will work? Can you give me an example of the code?

Many thanks,

peter
0
 
rockiroadsCommented:
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")
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
rockiroadsCommented:
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
0
 
PeborghAuthor Commented:
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?

Thanks,

peter
0
 
rockiroadsCommented:
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))

0
 
PeborghAuthor Commented:
Although this did not solve the problem it enabled me to improve my code. Full problem was solved in another EE thread.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now