this code always worked in 07 but now is crashing in 2010. It should take a range in excel and paste as an image in the corresponding bookmark in Word? It seems like it is running out of memory because it copies everything to the clipboard. Ive tried changing Data Type=:0 and DoEvents any ideas? Thanks!
Sub GenerateBusinessCase()
'
Application.ScreenUpdating = False
Dim pappWord As Object
Dim docWord As Object
Dim wb As Excel.Workbook
Dim xlName As Excel.Name
Dim Path As String
'Dim rangeData As Range
Const wdGoToAbsolute As Integer = 1
Const wdGoToLine As Integer = 3
Set wb = ActiveWorkbook
Path = wb.Path & Application.PathSeparator & "GE Lighting Business Case Template.docx"
On Error GoTo ErrorHandler
'Create a new Word Session
Set pappWord = CreateObject("Word.Application")
On Error GoTo ErrorHandler
'Open document in word
Set docWord = pappWord.Documents.Add(Path)
'Loop through names in the activeworkbook
For Each xlName In wb.Names
'if xlName's name is existing in document then put the value in place of the bookmark
If docWord.Bookmarks.Exists(xlName.Name) Then
docWord.Bookmarks(xlName.Name).Range.Text = Range(xlName.Value)
End If
Next xlName
'Run the table copy code. I am copying/pasting my Excel tables with this code.
docWord.Bookmarks("FirstPillar").Range.Select
Range("FirstPillar1").Copy
pappWord.Selection.PasteSpecial Link:=False, DataType:=3
docWord.Bookmarks("ThirdPillar").Range.Select
Range("ThirdPillar1").Copy
pappWord.Selection.PasteSpecial Link:=False, DataType:=3
docWord.Bookmarks("FourthPillar").Range.Select
Range("FourthPillar1").Copy
pappWord.Selection.PasteSpecial Link:=False, DataType:=3
docWord.Bookmarks("SecondPillar").Range.Select
Range("SecondPillar1").Copy
pappWord.Selection.PasteSpecial Link:=False, DataType:=3
docWord.Bookmarks("WaterfallControls").Range.Select
Range("WaterfallControls1").Copy
pappWord.Selection.PasteSpecial Link:=False, DataType:=3
docWord.Bookmarks("ModelAdjustment").Range.Select
Range("ModelAdjustment1").Copy
pappWord.Selection.PasteSpecial Link:=False, DataType:=3
docWord.Bookmarks("FinancialMetrics").Range.Select
Range("FinancialMetrics1").Copy
pappWord.Selection.PasteSpecial Link:=False, DataType:=3
docWord.Bookmarks("FinancialResults").Range.Select
Range("FinancialResults1").Copy
pappWord.Selection.PasteSpecial Link:=False, DataType:=3
docWord.Bookmarks("SolutionInvestment").Range.Select
Range("SolutionInvestment1").Copy
pappWord.Selection.PasteSpecial Link:=False, DataType:=3
'Turn off copy mode
Application.CutCopyMode = False
'Activate word document
With pappWord
.Visible = True
.ActiveWindow.WindowState = 1
.Activate
' Optional - Display document on page #1
.Selection.GoTo What:=wdGoToLine, Which:=wdGoToAbsolute, Count:=1
End With
'Release the Word object to save memory and exit macro
ErrorExit:
Set pappWord = Nothing
Exit Sub
'Error Handling routine
ErrorHandler:
If Err Then
MsgBox "Error No: " & Err.Number & ";" & vbNewLine & " o Verify calculations on the financial results page do not return errors".
If Not pappWord Is Nothing Then
pappWord.Quit False
End If
Resume ErrorExit
End If
Application.ScreenUpdating = True
End Sub
Sub GenerateBusinessCase()
'
Application.ScreenUpdating = False
Dim pappWord As Object
Dim docWord As Object
Dim wb As Excel.Workbook
Dim xlName As Excel.Name
Dim Path As String
'Dim rangeData As Range
Const wdGoToAbsolute As Integer = 1
Const wdGoToLine As Integer = 3
Set wb = ActiveWorkbook
Path = wb.Path & Application.PathSeparator & "GE Lighting Business Case Template.docx"
On Error GoTo ErrorHandler
'Create a new Word Session
Set pappWord = CreateObject("Word.Application")
On Error GoTo ErrorHandler
'Open document in word
Set docWord = pappWord.Documents.Add(Path)
'Loop through names in the activeworkbook
For Each xlName In wb.Names
'if xlName's name is existing in document then put the value in place of the bookmark
If docWord.Bookmarks.Exists(xlName.Name) Then
docWord.Bookmarks(xlName.Name).Range.Text = Range(xlName.Value)
End If
Next xlName
'Run the table copy code. I am copying/pasting my Excel tables with this code.
docWord.Bookmarks("FirstPillar").Range.Select
Range("FirstPillar1").Copy
pappWord.Selection.PasteSpecial Link:=False, DataType:=3
docWord.Bookmarks("ThirdPillar").Range.Select
Range("ThirdPillar1").Copy
pappWord.Selection.PasteSpecial Link:=False, DataType:=3
docWord.Bookmarks("FourthPillar").Range.Select
Range("FourthPillar1").Copy
pappWord.Selection.PasteSpecial Link:=False, DataType:=3
docWord.Bookmarks("SecondPillar").Range.Select
Range("SecondPillar1").Copy
pappWord.Selection.PasteSpecial Link:=False, DataType:=3
docWord.Bookmarks("WaterfallControls").Range.Select
Range("WaterfallControls1").Copy
pappWord.Selection.PasteSpecial Link:=False, DataType:=3
docWord.Bookmarks("ModelAdjustment").Range.Select
Range("ModelAdjustment1").Copy
pappWord.Selection.PasteSpecial Link:=False, DataType:=3
docWord.Bookmarks("FinancialMetrics").Range.Select
Range("FinancialMetrics1").Copy
pappWord.Selection.PasteSpecial Link:=False, DataType:=3
docWord.Bookmarks("FinancialResults").Range.Select
Range("FinancialResults1").Copy
pappWord.Selection.PasteSpecial Link:=False, DataType:=3
docWord.Bookmarks("SolutionInvestment").Range.Select
Range("SolutionInvestment1").Copy
pappWord.Selection.PasteSpecial Link:=False, DataType:=3
'Turn off copy mode
Application.CutCopyMode = False
'Activate word document
With pappWord
.Visible = True
.ActiveWindow.WindowState = 1
.Activate
' Optional - Display document on page #1
.Selection.GoTo What:=wdGoToLine, Which:=wdGoToAbsolute, Count:=1
End With
'Release the Word object to save memory and exit macro
ErrorExit:
Set pappWord = Nothing
Exit Sub
'Error Handling routine
ErrorHandler:
If Err Then
MsgBox "Error No: " & Err.Number & ";" & vbNewLine & " o Verify calculations on the financial results page do not return errors".
If Not pappWord Is Nothing Then
pappWord.Quit False
End If
Resume ErrorExit
End If
Application.ScreenUpdating = True
End Sub