Link to home
Start Free TrialLog in
Avatar of Peborgh
PeborghFlag for United Kingdom of Great Britain and Northern Ireland

asked on

"Password too long" on closing a Word .docx from Access VBA

As it says in the title.
See code. When I run this, not only does it ask me whether to save, which it should do automatically, but when I OK the save, I get this error:

     "The password is too long"
     error 5476 at line 340

When I OK this, it still saves as requested. The .docx is still protected by the password.

 Why do I get this message? It all works fine in Access 2002.

GrahamSkan, if you are listening, you appear to have deal with a similar question in Februay but it has been deleted.

Many thanks.
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]")
60  Set docdoc = docapp.Documents.Open("C:\program files\COUNSELOG 4.2\cg4 docs\" & _
                                       Forms!switchboard!thisuser & "\" & Forms![counselling log].client_on_form & ".docx", _
               , , , password, , , password)

'... all sorts of code to write to the .docx...

340 docdoc.Close wdSaveChanges
350 docapp.Quit

Open in new window

Avatar of Jeffrey Coachman
Jeffrey Coachman
Flag of United States of America image

...how long is the password?
Avatar of Peborgh

ASKER

9 characters.

Thanks,

peter
According to what I have seen the Max password length is 15 characters.

< , , , password, , , password)>
In the above, is: password, the actual password?
If so then it needs to be on double quotes:  "Password",,,"Password"

If it is a variable, then how is it being declared?

Avatar of Peborgh

ASKER

No, password is a variable.

Thanks,

petr
...and as I asked:
<then how is it being declared?>
...then name it as such, so it is easy to identify and avoid confusion:
strPassword
varPassword
...

...based on the datatype...
Avatar of Peborgh

ASKER

It was dim-ed as a string with no size specified.

Thanks,

peter
Is there a reason why you are not posting the full code?

You have 3 "On Error GoTo" lines in the code, which is, in and of itself, ...confusing, but in addition, ...none of the GOTO destinations are shown?

I don't see any declarations, yet at line 20 you are already "Setting" an object equal to something...

JeffCoachman


Avatar of Peborgh

ASKER

Here is the whole thing, I thought the whole code was not relevant but only the bits that went wrong. Sorry.

peter
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, 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]")
60  Set docdoc = docapp.Documents.Open("C:\program files\COUNSELOG 4.2\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

ASKER CERTIFIED SOLUTION
Avatar of Jeffrey Coachman
Jeffrey Coachman
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Peborgh

ASKER

I changed the variable "password" to string. no improvement.

Then used

        dim password as string*16

and it worked!

This was not necessary in Office/Word/Access 2002?! Any comments?

Many thanks,

peter
Avatar of Peborgh

ASKER

So I assume you don't know why this was necessary in 2007 but not 2002?

peter
MS Office Passwords are now more "secure" than in previous versions.
My guess is that strict declarations of password Variables is part of this security.
Avatar of Peborgh

ASKER

Good thinking, boag2000. And thanks.

peter
Avatar of Peborgh

ASKER

Dim-ing the password variable as string was a step in the right direction, but setting it as 16 chars long was the final solution...
Teamwork

;-)
Avatar of Peborgh

ASKER

Amen!