我尝试将Excel中的范围复制到Word中并将其粘贴为图像。
我目前的设置是:
在WORD中粘贴后,我再也无法调用引用的Excel文档中的任何宏。我的代码,它位于WORD VBA宏中:
Dim xl As Object
Set xl = CreateObject("Excel.Application")
xl.Workbooks.Open ("\\path\to\DESKTOP\qrap\xlsHelper.xlsm")
For i = 0 To 10
xl.Run "copy", "\\path\to\DESKTOP\qrap\pieces\2003 2015-2 finance.xls"
Selection.PasteSpecial Link:=False, DataType:=wdPasteEnhancedMetafile, _
Placement:=wdInLine, DisplayAsIcon:=False
Next
xl.ActiveWorkbook.Close (False)
xl.Quit
Set xl = Nothing
在第二次迭代中,行xl.Run ...
抛出Runtime Error 1004: Macro cannot be executed
。第一次运行正常。如果我没有切换到WORD,那就运行得很好。
如何重新聚焦到Excel,以便宏可以正常运行?
此外,Excel文件中的copy
宏是:
Dim oWb As Workbook
Public Sub copy(filename As String)
If Not (oWb Is Nothing) Then
oWb.Close
Set oWb = Nothing
End If
Set oWb = Workbooks.Open(filename)
ActiveCell.CurrentRegion.Select
Selection.copy
End Sub
答案 0 :(得分:2)
我怀疑你正在打开一本工作簿。确保后台没有运行任何Excel实例,然后将代码更改为:
Dim xl As Object
Dim wb As Object
Set xl = CreateObject("Excel.Application")
xl.Workbooks.Open "\\path\to\DESKTOP\qrap\xlsHelper.xlsm"
For i = 0 To 10
xl.Run "xlsHelper.xlsm!copy", "\\path\to\DESKTOP\qrap\stukken\2003 2015-2 financieel.xls"
Selection.PasteSpecial Link:=False, DataType:=wdPasteEnhancedMetafile, _
Placement:=wdInLine, DisplayAsIcon:=False
Next
xl.Activeworkbook.Close False
xl.Quit
并试试。