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?
The code is basically identical for doc#2 (and 3, etc.) it may just merge a couple different fields.
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.