.copychart 从 Excel 到 Word 错误,除非两个应用程序都可见

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

使用 .copychart 从 Excel 粘贴到我下面代码中的 Word 错误。如果这两个应用程序都可见,就像我在编程时看到的那样,那么它运行良好。

有没有办法隐藏应用程序(让它们 运行 在后台运行)并且仍然使用 .copychart 还是我需要一个不同的解决方案?

我只展示了一小部分代码,因为整个模块很大。这是我在 .copypicture 行收到错误的地方:

'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

您可以尝试以下几种选择:

对于:

如果你有

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

.

对于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