I am working on a VBA macro in Microsoft Word that is supposed to generate a histogram from data in a table within the document Word.
I have written the following code to accomplish this task:
Sub GenerateHistogram()
Dim tbl As Table
Dim rngData As Range
Dim chartObj As Object
' Select the table containing the data
Set tbl = ActiveDocument.Tables(1)
' Define the data range from the table
Set rngData = tbl.Range
' Insert a chart into the document
Set chartObj = ActiveDocument.Shapes.AddChart2(201, xlColumnClustered).Chart
' Add the data to the chart
chartObj.SetSourceData Source:=rngData, PlotBy:=xlColumns
' Adjust the size of the chart
chartObj.Parent.Width = 400
chartObj.Parent.Height = 300
End Sub
However, when I run this macro, I receive an “Automation Error, Unspecified Error” :
I have checked that macros are enabled in Word and that my table is correctly formatted. I am not sure what is causing this error and how to fix it. Can someone help me understand what’s wrong with my code and how to correct it?
Any help would be greatly appreciated. Thanks in advance!
1