Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

4198 opening a Word .docx from Access

Posted on 2010-09-06
7
Medium Priority
?
833 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
  • 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 1500 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
Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

 
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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

Microsoft's Excel has many features that most people will never need nor take advantage of.  Conditional formatting is one feature that you may find a necessity once you start using it.
Cancel future meetings from user mailboxes in Office 365 using Remove-CalendarEvents
Learn how to make your own table of contents in Microsoft Word using paragraph styles and the automatic table of contents tool. We'll be using the paragraph styles in Word’s Home toolbar to help you create a table of contents. Type out your initial …
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

971 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