Home

Application-defined error or object defined error while copying chart using VBA

%3CLINGO-SUB%20id%3D%22lingo-sub-812982%22%20slang%3D%22en-US%22%3EApplication-defined%20error%20or%20object%20defined%20error%20while%20copying%20chart%20using%20VBA%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-812982%22%20slang%3D%22en-US%22%3E%3CP%3EHi%20%2C%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ECan%20you%20please%20help%20me%20figure%20out%20the%20problem%20I%20am%20facing%20with%20Word%20%2C%20VBA%20%2C%20Excel%20%3F%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20am%20trying%20to%20copy%20a%20chart%20present%20in%20an%20Excel%20workbook%20into%20a%20Word%20document%20using%20VBA%26nbsp%3B%20macro%20%2C%20created%20in%20the%20Word%20doc%20(%20Macro%20code%20is%20given%20below%20)%20.%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThe%20problem%20is%20that%20the%20macro%20gives%20following%20error%20%3A%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ERun-time%20error%201004%20%3A%3C%2FP%3E%3CP%3EApplication%20defined%20or%20object%20defined%20error%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ENow%20%2C%20if%20I%20simply%20open%20the%20excel%20workbook%26nbsp%3B%20%2C%20randomly%20click%20on%20some%20cells%20and%20save%26nbsp%3B%20it%26nbsp%3B%20(%26nbsp%3B%20note%20that%20no%20changes%20are%20done%20to%20the%20chart%20in%20question%20)%26nbsp%3B%20and%20re-run%20the%26nbsp%3B%20macro%20%2C%20then%20the%20problem%20goes%20away%20.%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20am%20unable%20to%20solve%20this%20problem%20.%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EMacro%20code%20%3A%20(%20Problem%20line%20is%20in%20bold%20)%26nbsp%3B%3C%2FP%3E%3CP%3E-----------------------------------------------------------------------------------------------%3C%2FP%3E%3CP%3ESub%20copy_chart()%3CBR%20%2F%3E'%3CBR%20%2F%3E'%20copy_chart%20Macro%3CBR%20%2F%3E'%3CBR%20%2F%3E'%3C%2FP%3E%3CP%3ESet%20oExcel%20%3D%20CreateObject(%22Excel.Application%22)%3CBR%20%2F%3EoExcel.Visible%20%3D%20True%3C%2FP%3E%3CP%3ESet%20wb%20%3D%20oExcel.Workbooks.Open(Application.ActiveDocument.Path%20%26amp%3B%20%22%5C%22%20%26amp%3B%20%22Chart_copy_problem_1.xlsx%22%2C%20UpdateLinks%3A%3DFalse)%3C%2FP%3E%3CP%3E%3CSTRONG%3Ewb.WorkSheets(%22Regression%20results%22).ChartObjects(%22Cov_chart%22).Chart.ChartArea.Copy%3C%2FSTRONG%3E%3CBR%20%2F%3E%3CBR%20%2F%3EActiveDocument.Bookmarks(%22Chart_here%22).Select%3CBR%20%2F%3ESelection.Paste%3C%2FP%3E%3CP%3EoExcel.Workbooks(%22Chart_copy_problem_1.xlsx%22).Close%3CBR%20%2F%3E%3CBR%20%2F%3EoExcel.Application.Quit%3C%2FP%3E%3CP%3E%3CBR%20%2F%3EEnd%20Sub%3C%2FP%3E%3CP%3E---------------------------------------------------------------------------------------------%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-812982%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EOffice%20365%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1066253%22%20slang%3D%22en-US%22%3ERe%3A%20Application-defined%20error%20or%20object%20defined%20error%20while%20copying%20chart%20using%20VBA%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1066253%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F395165%22%20target%3D%22_blank%22%3E%40Ravinder2019%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20am%20trying%20to%20figure%20out%20the%20same%20issue%20but%20for%20me%20it%20seems%20to%20not%20work%20when%20the%20chart%20is%20not%20in%20view%20(debug%20retry%20fails%20every%20time)%20and%20when%20I%20scroll%20down%20so%20I%20can%20see%20the%20chart%20and%20retry%20the%20debug%2C%20then%20it%20works.%20I'll%20keep%20investigating%2C%20I%20know%20this%20thread%20is%20old.%3C%2FP%3E%3C%2FLINGO-BODY%3E
Highlighted
Ravinder2019
Occasional Visitor

Hi , 

 

Can you please help me figure out the problem I am facing with Word , VBA , Excel ? 

 

I am trying to copy a chart present in an Excel workbook into a Word document using VBA  macro , created in the Word doc ( Macro code is given below ) . 

 

The problem is that the macro gives following error : 

 

Run-time error 1004 :

Application defined or object defined error 

 

Now , if I simply open the excel workbook  , randomly click on some cells and save  it  (  note that no changes are done to the chart in question )  and re-run the  macro , then the problem goes away . 

 

I am unable to solve this problem . 

 

Macro code : ( Problem line is in bold ) 

-----------------------------------------------------------------------------------------------

Sub copy_chart()
'
' copy_chart Macro
'
'

Set oExcel = CreateObject("Excel.Application")
oExcel.Visible = True

Set wb = oExcel.Workbooks.Open(Application.ActiveDocument.Path & "\" & "Chart_copy_problem_1.xlsx", UpdateLinks:=False)

wb.WorkSheets("Regression results").ChartObjects("Cov_chart").Chart.ChartArea.Copy

ActiveDocument.Bookmarks("Chart_here").Select
Selection.Paste

oExcel.Workbooks("Chart_copy_problem_1.xlsx").Close

oExcel.Application.Quit


End Sub

---------------------------------------------------------------------------------------------

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

1 Reply
Highlighted

@Ravinder2019 

I am trying to figure out the same issue but for me it seems to not work when the chart is not in view (debug retry fails every time) and when I scroll down so I can see the chart and retry the debug, then it works. I'll keep investigating, I know this thread is old.

Related Conversations
MSTeams Groups in outlook with macros disabled
ReadyorNot in Microsoft Teams on
0 Replies
Unknown Login Error
Alex_P3462 in Microsoft Teams on
0 Replies
Restrict access to i.e. Planner
Thomas_Steibl in Admin Center on
4 Replies
Spelling Pop-Up Stuck on SharePoint List
aricornish in SharePoint on
0 Replies
Problem with OneDrive
esholder in OneDrive for Business on
5 Replies