I have an Excel document with a series of graphs displayed that I would like to copy and paste to a Word document, and then insert a table of contents.
The code works perfectly, except for the line that adds the TOC. It is falling over with the message "Wrong number of arguments or invalid property assignment".
Code as follows
Any help much appreciated.
The code works perfectly, except for the line that adds the TOC. It is falling over with the message "Wrong number of arguments or invalid property assignment".
Code as follows
Code:
Sub MS_Word()
Dim intChartCount As Integer
Dim cht As Chart
Dim strChartTitle As String
'Create a Microsoft Word session
Set wd = CreateObject("word.application")
'Make document visible
wd.Visible = True
'Activate MS Word
AppActivate wd.Name
'Open a new document in Microsoft Word
wd.Documents.Add
wd.Selection.Style = wd.ActiveDocument.Styles("Heading 3")
wd.Selection.TypeParagraph
intChartCount = Worksheets("Chart").ChartObjects.Count
For Counter = 1 To intChartCount
Worksheets("Chart").ChartObjects(Counter).Chart.ChartArea.Copy
strChartTitle = Worksheets("Chart").ChartObjects(Counter).Chart.ChartTitle.Text
With wd
.Selection.Style = wd.ActiveDocument.Styles("Heading 3")
'Insert the Chart title
.Selection.TypeText Text:=strChartTitle
.Selection.TypeParagraph
'Paste the chart
.Selection.PasteSpecial link:=False, DisplayAsIcon:=False, Placement:=wdInLine
' Insert spacing
.Selection.TypeParagraph
.Selection.TypeParagraph
.Selection.TypeParagraph
End With
Next Counter
wd.Selection.WholeStory
wd.Selection.HomeKey
With wd
.TablesOfContents.Add Range:=Selection.Range, RightAlignPageNumbers:= _
True, UseHeadingStyles:=False, IncludePageNumbers:=True, AddedStyles _
:="", UseHyperlinks:=True, HidePageNumbersInWeb:=True
.TablesOfContents(1).TabLeader = wdTabLeaderDots
.TablesOfContents.Format = wdIndexIndent
End With
wd.ActiveDocument.SaveAs Filename:="Charts.doc", FileFormat:=wdFormatDocument, _
LockComments:=False, Password:="", AddToRecentFiles:=True, WritePassword _
:="", ReadOnlyRecommended:=False, EmbedTrueTypeFonts:=False, _
SaveNativePictureFormat:=False, SaveFormsData:=False, SaveAsAOCELetter:= _
False
Application.Worksheets("Chart").Activate
Set wd = Nothing
End Sub
Any help much appreciated.