Open Word document, fill bookmarks, then Insert a Word File into active document

Paul Barrett
Paul Barrett used Ask the Experts™
on
I am slowly putting together a routine that opens a Word doc and fills some bookmarks, all fine so far.  What I would  like to do next is 1.  to insert an Existing word document into the active document using a file location that I can control, so would like it to go after bookmarks and before End With in code below.  2.  Ideally I would also like to set a Word password in the line that sAVES the document, again I am in control of that password which will be sitting in a form field.

Public Sub OutPutToWord()
Dim Wrd As Object
Set Wrd = CreateObject("Word.Application")
Dim strDocPath As String
strDocPath = Forms![frmEmployeeHome]![SelectedDocument]
Wrd.Visible = True
'Open the Document
Wrd.Documents.Open FileName:=strDocPath
       
With Wrd.ActiveDocument.Bookmarks
.Item("CompanyName").Range.Text = Forms![frmEmployeeHome]![CompanyName]
.Item("OrderDate").Range.Text = Forms![frmEmployeeHome]![OrderDate]
.Item("Address").Range.Text = Forms![frmEmployeeHome]![Address]

1.  NEED TO INSERT A WORD FILE IN HERE THAT WILL = Forms![frmEmployeeHome]![InsertDocument]

End With

'Close and release Word pointers
Wrd.ActiveDocument.SaveAs FileName:=Forms![frmEmployeeHome]![FinalDocument]

2.  WOULD LIKE THIS DOCUMENT TO BE SAVED WITH A PASSWORD FROM AN EXISTING FORM FIELD.

Set doc = Nothing
End Sub
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Commented:
Try this:

To insert a word file as a hyperlink (ie ... just list the path that the user can click on), create an additional bookmark (I called mine InsertedDoc), end your With statement after your third item, and do this:

strInsertDocumentPath = Forms![frmEmployeeHome]![InsertDocument]

Wrd.Selection.GoTo What:=wdGoToBookmark, Name:="InsertedDoc"
Wrd.ActiveDocument.Bookmarks.Item("InsertedDoc").Range.Hyperlinks.Add Anchor:=Wrd.Selection.Range, Address:= _
        strInsertDocumentPath,SubAddress:="", ScreenTip:="", TextToDisplay:=strInsertDocumentPath

To actually insert the contents of the file, use this code instead:

Wrd.Selection.GoTo What:=wdGoToBookmark, Name:="InsertedDoc"
Wrd.ActiveDocument.Bookmarks.Item("InsertedDoc").Range.InsertFile FileName:=strInsertDocumentPath

To save the document as a different file name that included a password, do this:

strFinalDocumentName = Forms![frmEmployeeHome]![FinalDocument]
strFinalDocumentPassword = Forms![frmEmployeeHome]![FinalDocumentPassword]

Wrd.ActiveDocument.SaveAs FileName:=strFinalDocumentName, FileFormat:= _
        wdFormatDocument, WritePassword:=strFinalDocumentPassword

This should do exactly what you want.

Author

Commented:
Thanks, I just could'nt get a handle on where the inserted file went, I was just using 'Insert File' thinking it would go in from my last point in the document instead of referencing a point for it to start from.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial