Reputation: 1
I have an Excel Worksheet that opens a blank Word document as the "Master" and a "Temporary" separate Word Document. I'm trying to move to the end of "Master", Copy the entirety of "Temporary" and paste to "Master" and then enter a page break on "Master". I'm getting errors on ".Selection.EndKey unit:=wdStory Extend:=wdMove" , ".Add.Content.Paste" and ".Selection.InsertBreak Type:=7" and I'm not sure why. I'll be iterating multiple "Temporary" documents and adding them to "Master" one by one but don't want to move to the loops until I can do one basic round of copy-paste from "Temporary" to "Master". There's alos a boomark replace subroutine that I'm not showing to reduce complexity from the community, hence the "Fields.Update" on the "Temporary" doc.
Public wb As Excel.Workbook
Public Path As String
Public MasterWordObj
Public MasterCOI
Public TempWordObj
Public TempCOI
Sub COI()
'Initialize Worksheet/Workbook and unprotect worksheet and cells
ActiveSheet.Unprotect
ThisWorkbook.Sheets("COI").Cells.Locked = False
Set wb = ActiveWorkbook
On Error GoTo ErrorHandler
'Path for Master COI Template
Path = "xxx"
'Create a Master Word Session
Set MasterWordObj = CreateObject("Word.Application")
Set MasterCOI = MasterWordObj.Documents.Add
'Define Path for Temporary COI Template
Path = "XXX"
'Create a Temp Word Session
Set TempWordObj = CreateObject("Word.Application")
Set TempCOI = TempWordObj.Documents.Add(Path)
'Activate Temporary Word Template, Update Fields, Copy All
With TempWordObj
.Visible = True
.Selection.WholeStory
.Selection.Fields.Update
.Selection.WholeStory
.ActiveWindow.WindowState = 1
.Activate
.Selection.WholeStory
.Selection.Copy
End With
'Paste and insert Page Break in Master Template
With MasterWordObj
.Visible = True
.ActiveWindow.WindowState = 1
.Selection.EndKey unit:=wdStory, Extend:=wdMove
.Add.Content.Paste
.Selection.InsertBreak Type:=7
.Activate
End With
'Release the Word objects to save memory and exit macro
ErrorExit:
Set MasterWordObj = Nothing
Set TempWordObj = Nothing
Exit Sub
'Error Handling routine
ErrorHandler:
If Err Then
MsgBox "Error No: " & Err.Number & "; There is a problem"
If Not MasterWordObj Is Nothing Then
MasterWordObj.Quit False
End If
If Not TempWordObj Is Nothing Then
TempWordObj.Quit False
End If
Resume ErrorExit
End If
End Sub
Upvotes: 0
Views: 175
Reputation: 166306
If you haven't added a reference to Word in your Excel VBA project then constants like wdStory etc will not be available to your Excel VBA. This is flagged up if you use Option Explicit in your modules (and you absolutely should do that).
You either need to add the reference, or declare the missing constants and their values in your VBA.
Upvotes: 1