gruntingmonkey
Active Member
- Joined
- Mar 6, 2008
- Messages
- 444
- Office Version
- 365
- Platform
- 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.
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