在我的一个工作表中,我有另一个工作簿作为嵌入式对象插入,我需要打开它来读取它的一些字段。
我使用OLEobj.Verb xlVerbOpen打开这个文件,并将它分配给一个名为wbR的Workbook变量。因为每次打开它时,它的名称都会改变,所以我认为唯一的方法就是使用ActiveWorkbook。
当我逐行运行代码时,这是可行的。但是,当从一个按钮运行整个宏时,大多数情况下,ActiveWorkbook将被视为与ThisWorkBook相同。我试着用等待和睡眠。
造成这一问题的部分:
For Each OLEobj In ThisWorkbook.Sheets("SheetName").OLEObjects
Set OLEobj = wsr.OLEObjects(OLEobj.Name)
OLEobj.Verb xlVerbOpen
Application.Wait (Now + TimeValue("0:00:05"))
'Sleep 5000
Set wbR = ActiveWorkbook
Exit For
Next OLEobj
发布于 2020-03-05 13:38:02
使用.Verb xlVerbOpen
可以冒险获得“活动”应用程序。
Activate (start/open)
对象并使用OLEFormat.Object.Object
获取工作簿对象。
这就是你想要的吗?
Option Explicit
Sub Sample()
Dim ws As Worksheet
Dim shp As Shape
Dim wb As Object
'~~> Change this to the relevant sheet where the excel file is embedded
Set ws = Sheet1
'~~> Change this to the relevant object name
Set shp = ws.Shapes("Object 1") ' OLEobj.Name? from your code?
'~~> Activate it (Open it)
shp.OLEFormat.Activate
'~~> Get the workbook object
Set wb = shp.OLEFormat.Object.Object
With wb
'
'~~> Work with the workbook object here
'
MsgBox .Worksheets.Count
End With
End Sub
发布于 2020-03-05 13:59:47
按照您的建议,我已经修改了代码,如下所示,它确实很有魅力。非常感谢你的帮助!
For Each OLEobj In ThisWorkbook.Sheets("Ratios").OLEObjects
Set OLEobj = wsr.OLEObjects(OLEobj.Name)
'OLEobj.Verb xlVerbOpen
OLEobj.Activate
**Set wbR = OLEobj.Object** ' New line that solves the issue.
Exit For
Next OLEobj
https://stackoverflow.com/questions/60545993
复制相似问题