Search code examples
vbaexcelexcel-2010ms-wordword-2010

.copychart from Excel to Word errors unless both applications are visible


Using .copychart in pasting from Excel to Word errors in my code below. If both applications are visible, as I've had them while programming, then it works well.

Is there a way to hide the applications (let them run in the background) and still use .copychart or do I need a different solution?

I am only showing a small parts of the code, as the entire module is quite large. Here is where I get the error on the .copypicture line:

'Copy charts to Worddocument
Dim cht1 As Chart, cht2 As Chart, cht3 As Chart

With tbl2
    cht1.CopyPicture Appearance:=xlScreen, Format:=xlPicture
    With .cell(4, 1).Tables(1).cell(2, 1).Range
        .Paste 'Paste NetRevenue Graph
        .InlineShapes(1).ScaleWidth = 100
    End With
End With

Solution

  • There are a few options you can try:

    For Word:

    If you have

    Dim wordApp As Word.Application
    Dim wordDoc As Word.Document
    

    1.

    wordApp.Visible = False
    '...copy chart
    wordApp.Visible = True
    

    2.

    wordDoc.Windows(1).WindowState = wdWindowStateMinimize
    '...copy chart
    wordDoc.Windows(1).WindowState = wdWindowStateMaximize   'or wdWindowStateNormal
    

    .

    For Excel:

    1.

    Application.Visible = False
    '...copy chart
    Application.Visible = True
    

    2.

    ActiveWorkbook.Windows(1).WindowState = xlMinimized
    '...copy chart
    ActiveWorkbook.Windows(1).WindowState = xlMaximized
    

    3.

    Application.ScreenUpdating = False
    '...copy chart
    Application.ScreenUpdating = True