我创建了一个简单的脚本,用于将OLEObject从Excel导出到MS Word:
Sub ExportScreenCaptureToWord()
Dim WordApp As Word.Application
Dim myDoc As Word.Document
Dim embedWord As Excel.OLEObject
Application.ScreenUpdating = False
Application.EnableEvents = False
On Error Resume Next
Set WordApp = GetObject(Class:="Word.Application")
Err.Clear
If WordApp Is Nothing Then Set WordApp = CreateObject(Class:="Word.Application")
If Err.Number = 429 Then
MsgBox "Microsoft Word could not be found, aborting."
GoTo EndRoutine
End If
On Error GoTo 0
Set myDoc = WordApp.Documents.Add
Worksheets("Power View1").Activate
DoEvents
Set embedWord = Sheet2.OLEObjects(1)
DoEvents
embedWord.CopyPicture xlScreen, xlBitmap
DoEvents
Set objSelection = WordApp.Selection
objSelection.Paste
WordApp.Visible = True
WordApp.Activate
EndRoutine:
Application.ScreenUpdating = True
Application.EnableEvents = True
Application.CutCopyMode = False
End Sub
脚本始终执行但不是每次都在Word文档中保存OLEObject。脚本有时执行正常,它将位图图像保存在Word文档中。脚本没有例外。
答案 0 :(得分:0)
不清楚什么方法或prorperty触发异常......你是否尝试至少调试代码?什么行代码触发异常?例外细节?
无论如何,请查看How to Use (OLE) Automation with Word文章,该文章介绍了从其他应用程序自动化Word所需的所有步骤,并提供了示例代码。希望它会有所帮助。