Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Password Protected Document Problem

Status
Not open for further replies.

pluto1415

MIS
Apr 28, 2009
78
US
I have code that opens MS Word documents (as necessary) and merges them from an Access query. It then closes document 1 and moves to document 2, 3, etc... We've come across a reason to password protect all the documents. I've managed to write the code that passes the password and opens the doc, merges it, and saves it to a new doc in a new location without a PW. Unfortunately, when it gets to Doc#2, it doesn't recognize the PW and opens a msgbox asking for the PW. Why won't it recognize it on doc#2?

Code:
Private Sub OL(strDocName1, sStateSelect)
On Error GoTo WordError
    Dim objWord As New Word.Application
    Dim MainObjDoc As Word.Document
    Dim sCaseNo As String
    Dim strTble As String
    Dim strTble2 As String
    strTble = "TblMain"
    strTble2 = "TblOwnerwSigners"
    Dim iOwnerNumber As Integer
    Dim sCarrier As String
    Dim sSeller As String, sSellerAdd As String, sSellerCity As String, sSellerState As String, sSellerZip As String
    Dim sSignerName As String, sSignerTitle As String
    Dim sSalutation As String
    Dim sJuris As String
    Dim sPolicyNo As String
    Dim sCloseDate As String
    Dim sLastName As String
    Dim sMAM As String
        
Set dbs = CurrentDb
    'open recordset for Insureds
    Set rst = dbs.OpenRecordset("SELECT * FROM " & strTble2 & " WHERE Owner_Name IS NOT NULL")
    
    With rst
    iOwnerNumber = 1
    rst.MoveFirst
        Do While Not rst.EOF
        Set MainObjDoc = objWord.Documents.Open(strDocName1, PasswordDocument:="SuperSecurePass")
        sSeller = rst.Fields("Owner_Name")
        sSellerAdd = rst.Fields("Owner_Address")
        sSellerCity = rst.Fields("Owner_city")
        sSellerState = rst.Fields("Owner_state")
        sSellerZip = rst.Fields("Owner_Zip_Code")
        sSalutation = Nz(rst.Fields("SignerName"), rst.Fields("Owner_Name"))
        MainObjDoc.Bookmarks("SellName").Range.InsertAfter Trim((sSeller))
        MainObjDoc.Bookmarks("SellAdd").Range.InsertAfter Trim((sSellerAdd))
        MainObjDoc.Bookmarks("SellCity").Range.InsertAfter Trim((sSellerCity))
        MainObjDoc.Bookmarks("SellState").Range.InsertAfter Trim((sSellerState))
        MainObjDoc.Bookmarks("SellZip").Range.InsertAfter Trim((sSellerZip))
        MainObjDoc.Bookmarks("Salutation").Range.InsertAfter Trim((sSalutation))
            Set rst2 = dbs.OpenRecordset("Select * from " & strTble & " where CaseNo is not null")
            sCaseNo = rst2.Fields("CaseNo")
            sCloseDate = rst2.Fields("Seller_Offer_Expire")
            sCarrier = rst2.Fields("Insurance_Company_Name")
            sPolicyNo = rst2.Fields("Policy_Number")
            sMAM = rst2.Fields("Case_Manager")
            sJuris = rst2.Fields("Jurisdiction")
            sLastName = rst2.Fields("Insured_LName")
            MainObjDoc.Bookmarks("CloseDate").Range.InsertAfter Trim((sCloseDate))
            MainObjDoc.Bookmarks("Carrier").Range.InsertAfter Trim((sCarrier))
            MainObjDoc.Bookmarks("PolicyNo").Range.InsertAfter Trim((sPolicyNo))
            MainObjDoc.Bookmarks("MAM").Range.InsertAfter StrConv(sMAM, vbProperCase)
            MainObjDoc.Bookmarks("Juris").Range.InsertAfter Trim((sJuris))
            MainObjDoc.SaveAs FileName:="R:\Closing Documents\Closing_2\" & Trim(sCaseNo) & " - " & Trim(sLastName) & "\" _
            & "1-" & iOwnerNumber & ".doc", Password:=""
            objWord.ActiveDocument.Close
            objWord.Quit
            Set objWord = Nothing
            iOwnerNumber = iOwnerNumber + 1
    rst.MoveNext
        Loop
    End With
  
Set MainObjDoc = Nothing
rst.Close
Set rst = Nothing
Set rst2 = Nothing

Exit Sub
WordError:
        If Err.Number = 94 Then
            MsgBox "Something's missing, go check all the data ", vbOKOnly, "Document Preparation"
            objWord.Application.Documents.Close wdDoNotSaveChanges
            objWord.Quit
            Me.lblwait.Visible = False
            Me.Box57.Visible = False
            Me.lbl1.Visible = False
            Me.lbl2.Visible = False
            Me.lbl3.Visible = False
            Me.lbl4.Visible = False
            Me.lbl5.Visible = False
            Me.lbl6.Visible = False
           
        ElseIf Err.Number <> 94 Then
            MsgBox "Err #" & Err.Number & "  occurred." & Err.Description, vbOKOnly, "Document Preparation"
            objWord.Application.Documents.Close wdDoNotSaveChanges
            objWord.Quit
            Me.lblwait.Visible = False
            Me.Box57.Visible = False
            Me.lbl1.Visible = False
            Me.lbl2.Visible = False
            Me.lbl3.Visible = False
            Me.lbl4.Visible = False
            Me.lbl5.Visible = False
            Me.lbl6.Visible = False
        End If
    
End Sub

The code is basically identical for doc#2 (and 3, etc.) it may just merge a couple different fields.
 
Slight change to what I said above. It doesn't matter if document #2 is the second document to open or the ONLY document it open - it doesn't recognize the password.

I can't see any difference between the 2 documents that would cause one to work and one to not work - any ideas? (obviously I've double and triple checked the password associated with both docs)
 
OK - I've figured it out, but I don't understand why. Some documents require the following line:

Code:
Set MainObjDoc=objWord.Documents.Open(strDocName2, PasswordDocument:="SecurePassword")

and some require I comment out the above and replace it with:

Code:
objWord.Documents.Open FileName:=strDocName2, PasswordTemplate:="SecurePassword"
Set MainObjDoc=objWord.ActiveDocument

I can't figure out what the difference is between the documents, but it's trial and error as to which documents require the 2 line statement and which will work with the 1 line...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top