Insert word file object at bookmark

gruntingmonkey

Active Member
Joined
Mar 6, 2008
Messages
444
Office Version
  1. 365
Platform
  1. Windows
Hello, I am trying to insert a word file at a bookmark in Word (through Excel VBA) but I cant seem to get it right. It keeps pasting the document contents (badly formatted) where I want to just include a copy of the file.

The line I cant get right is Bolded for ease.

Code:
Sub CreateContract()

Dim ConTemp As String
Dim wdApp As Word.Application
Dim wdDoc As Word.Document
Dim AccID As String, Serv As String, clName As String

'''Error Handle
On Error GoTo MyErrorHandler

UserNm = Environ$("Username")
'UserID = Application.UserName
AcNm = Sheets("DataInput").Cells(7, 4).Value
CoE = Sheets("DataInput").Cells(8, 4).Value
Dt = Format(Sheets("DataInput").Cells(9, 4).Value, "yyyy_mm_dd")
'''This is the Contract master template
ConTemp = "C:\Users\" & UserNm & "\The Consultancy\Office Management - Templates\Contract Template.dotx"

SvNm = "JT - " & AcNm & " - " & CoE & " - " & Dt & ""


'''Collects the data from Excel
Workbooks(ThisWorkbook.Name).Activate
AccID = Sheets("DataInput").Cells(7, 4).Value
Serv = Sheets("DataInput").Cells(8, 4).Value
clName = Sheets("DataInput").Cells(10, 4).Value

proposal = Sheets("utilities").Cells(8, 5).Value

    Set wdApp = CreateObject("Word.Application")
'''Opens the template
    Set wdDoc = wdApp.Documents.Open(ConTemp)
   
'''Activates Word
    With wdApp
        .Visible = True
        .Activate
'''Replaces bookmarks in Word with data from Excel
        With wdDoc
            .Bookmarks("AccountID").Range.Text = AccID
            .Bookmarks("ServiceType").Range.Text = Serv
            .Bookmarks("ClientName").Range.Text = clName
            
           
           [B] .Bookmarks("Appendix1").Range.InsertFile Filename:=proposal, Link:=False, Attachment:=True[/B]
            
            .SaveAs2 Filename:="C:\Users\" & UserNm & "\The Consultancy\Sales - Documents\Contracts\" & CoE & "\" & SvNm & ".docx", FileFormat:=Word.WdSaveFormat.wdFormatXMLDocument
            .Close
            
        End With
        
    End With

    wdApp.Quit
    
 MsgBox "Please check through the contract in its entirety. If there are any issues, close the file and change the details in the Excel before recreating contract by starting from Step 1."
 
  Sheets("Utilities").Cells(11, 5).Value = "C:\Users\" & UserNm & "\The Consultancy\Sales - Documents\Contracts\" & CoE & "\" & SvNm & ".docx"
  
    Set wdApp = Nothing
    Set wdDoc = Nothing
    
'''Complete/Incomplete
    Sheets("Utilities").Cells(10, 2).Value = Application.UserName & " " & Now
                Sheets("Utilities").Cells(10, 4).Value = "Complete"

    Exit Sub

MyErrorHandler:
 
 wdApp.Quit SaveChanges:=wdDoNotSaveChanges
                MsgBox "Uh oh - It all went wrong!!! Let Jess know and she will sort it for you....probably....Please tell her the following issue:" & vbNewLine & vbNewLine & Err.Description
 
    Set wdApp = Nothing
    Set wdDoc = Nothing
 
 End Sub
 
Thats it! Fantastic.

As a side note, if I wanted to place the contents of the file instead of the actual file, how would I do this without the formatting being awful? I'm assuming it's something to do with :
Code:
.Bookmarks("Appendix1").Range.FormattedText = wdPropDoc.Range.FormattedText

but I cant find the answer!
 
Upvote 0

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
Yeah, that leaves the formatting of the document in a terrible state which is unusable. From having a look round, I don't think that this can be done reliably time after time without turning each page into a picture, which i'm not keen on doing.

Thanks for the help anyway, I shall add it into the coding bank!
 
Upvote 0
One has to wonder whether you've actually tried it. The .FormattedText method preserves the source formatting. So, if "that leaves the formatting of the document in a terrible state", the reasonable implication is that the fault is already there in the source document. Ultimately, though, the problem is of your own making; if you'd used Styles properly and consistently between the two documents, the problem wouldn't exist.
 
Last edited:
Upvote 0
Macropod, you are being rather rude. I have tried it. This is a learning site. Telling me the fault is mine when you know very little about what I'm doing says more about you then it does about me.
 
Upvote 0
You need to understand the difference between being frank and being rude. Frankly, you need to take time to learn to use Word. I stand by what I said - you wouldn't have this problem if the documents were properly constructed using Styles.
 
Upvote 0

Forum statistics

Threads
1,223,268
Messages
6,171,100
Members
452,379
Latest member
IainTru

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top