Solved

4198 opening a Word .docx from Access

Posted on 2010-09-06
7
821 Views
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

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
Comment
Question by:Peborgh
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
7 Comments
 
LVL 6

Expert Comment

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

Author Comment

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

Many thanks,

peter
0
 
LVL 65

Accepted Solution

by:
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")
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 65

Expert Comment

by:rockiroads
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
0
 

Author Comment

by:Peborgh
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?

Thanks,

peter
0
 
LVL 65

Expert Comment

by:rockiroads
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))

0
 

Author Closing Comment

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

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Outlook Free & Paid Tools
It’s the first day of March, the weather is starting to warm up and the excitement of the upcoming St. Patrick’s Day holiday can be felt throughout the world.
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

740 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