Re: Excel VBA to cycle through Word bookmarks and insert ranges (text, graphs & table
Your homework? tut tut tut
Attached is the Excel workbook with new code in Module 2 and a word template that must live in the same folder. NOTE you must rename Fruit1.doc to Fruit1.dotx. I couldn't see the file with the new extension in the attachment portal and had to rename it to Fruit1.doc
This is slightly different in that it uses a template. Make a copy of the template, it is easy to end up with an open file when your code crashes and does not close the Word file, always manually open and close the Word file after a crash to ensure its closed properly.
You can add error trapping and any other functionality as needed.
You'll note how different my simple code is compared to Mr Plows. He obviously had a need to do what he did, your trying to adapt his code without understanding it caused you grief. That said someone better than me would likely critique my code, we're all learning.
Regards
Richard
The code for those interested:
Public Sub SendToWord()
Dim wdDoc As Word.Document, objDoc As Word.Document, wdApp As Word.Application
Dim VARPersonA As Excel.Range, VARPersonB As Excel.Range, VARPersonC As Excel.Range
Dim VARFruitA As Excel.Range, VARFruitB As Excel.Range, VARFruitC As Excel.Range
Dim VARTotalFruit As Excel.Range
Set wdApp = New Word.Application
'Assign the Word file path and name to variables
FilePath = ThisWorkbook.Path
FileName = "Fruit1.dotx"
Set objDoc = wdApp.Documents.Add(Template:=FilePath & "\" & FileName)
If objDoc Is Nothing Then
MsgBox "Unable to find the Word file.", vbCritical, "File Not Found"
wdApp.Quit
Set appWrd = Nothing
Exit Sub
End If
Set VARPersonA = Range("ARPersonA")
Set VARPersonB = Range("ARPersonB")
Set VARPersonC = Range("ARPersonC")
Set VARFruitA = Range("ARFruitA")
Set VARFruitB = Range("ARFruitB")
Set VARFruitC = Range("ARFruitC")
Set VARTotalFruit = Range("ARTotalFruit")
With objDoc.Bookmarks
.Item("ARPersonA").Range.InsertAfter VARPersonA
.Item("ARPersonB").Range.InsertAfter VARPersonB
.Item("ARPersonC").Range.InsertAfter VARPersonC
.Item("ARTotalFruit").Range.InsertAfter VARTotalFruit
.Item("ARFruitA").Range.InsertAfter VARFruitA & "s" 'add the s to Apple
.Item("ARFruitB").Range.InsertAfter VARFruitB & "s"
.Item("ARFruitC").Range.InsertAfter VARFruitC & "s"
End With
wdApp.Selection.Goto What:=wdGoToBookmark, Name:="ARTableFruit"
ThisWorkbook.Sheets("Sheet1").Range("B3:D7").Copy
wdApp.Selection.Paste
wdApp.Selection.Tables(1).Rows.Alignment = wdAlignRowLeft
wdApp.Selection.Goto What:=wdGoToBookmark, Name:="ARChartFruit"
ThisWorkbook.Sheets("Sheet1").ChartObjects(1).Copy
wdApp.Selection.Paste
wdApp.Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter
'Turn everything back on
Application.ScreenUpdating = True
Application.EnableEvents = True
Application.DisplayAlerts = True
Application.StatusBar = False
'Let the user know the procedure is now complete
Prompt = "Click OK to Review the Fruit Report Document in the Taskbar."
Title = "Procedure Completion"
MsgBox Prompt, vbOKOnly + vbInformation, Title
'Make our Word session visible
wdApp.Visible = True
wdApp.ActiveDocument.SaveAs FilePath & "\" & " Fruit Report.docx"
'Clean up
Set wdApp = Nothing
Set objDoc = Nothing
End Sub
Display More