I'm encountering an issue while generating a chart from a VBA macro in Microsoft Word. I have a macro that generates a histogram from data in a specific table in my Word document( Table n°12 ). My goal is to position this chart just below this table 12 from which the data is extracted.
I've tried using the Top property to set the vertical position of the chart, but I keep receiving the following error message: "Wrong number of arguments or invalid property assignment."
Here's the code of my current macro:
Option Explicit
Sub CreateWordChart3()
Dim oChart As Chart, oTable As Table
Dim oSheet As Excel.Worksheet
Dim oShape As Shape
Const START_CELL = "AA1"
Application.ScreenUpdating = False
' Récupérer la table 12 du document
Set oTable = ActiveDocument.Tables(12)
' Ajouter le graphique en tant que forme au document
Set oShape = ActiveDocument.Shapes.AddChart2(-1, xlColumnClustered)
' Définir la position du graphique sous la table 12
With oShape
.Top = oTable.Range.Tables(1).Rows(oTable.Rows.Count).Range.End(wdParagraph).Information(wdVerticalPositionRelativeToPage)
.Left = oTable.Range.Left
End With
' Obtenir le graphique en tant qu'objet Chart
Set oChart = oShape.Chart
' Copier les données de la table dans une feuille Excel temporaire
oTable.Range.Copy
Set oSheet = oChart.ChartData.Workbook.Worksheets(1)
oSheet.Range(START_CELL).PasteSpecial Paste:=xlPasteValuesAndNumberFormats
' Créer le tableau 2D
Call Create2DTable(oSheet, oSheet.Range(START_CELL))
' Fermer le classeur Excel temporaire
oChart.ChartData.Workbook.Close
Application.ScreenUpdating = True
End Sub
I'm seeking advice on how to correctly position the chart below the specified table in my Word document and resolve the error message. Any help or suggestions would be greatly appreciated. Thank you in advance!
InlineShape
and move to the desired locationSub CreateWordChart4()
Dim oChart As Chart, oTable As Table
Dim oSheet As Excel.Worksheet
Dim oShp As Shape, afterTblRange As Range
Const START_CELL = "AA1"
Application.ScreenUpdating = False
Set oTable = ActiveDocument.Tables(2) ' modify as needed
Set oShp = ActiveDocument.Shapes.AddChart
Set oChart = oShp.Chart
Set oSheet = oChart.ChartData.Workbook.Worksheets(1)
oTable.Range.Copy
oSheet.Range(START_CELL).Select
oSheet.Paste
Call Create2DTable(oSheet, oSheet.Range(START_CELL))
oChart.ChartData.Workbook.Close
Set afterTblRange = oTable.Range
afterTblRange.Collapse Direction:=wdCollapseEnd
oShp.ConvertToInlineShape
oShp.Select
Selection.Cut
afterTblRange.Paste
Application.ScreenUpdating = True
End Sub