不能在Excel中的Word CommandButton对象上工作 [英] Can't work on Word CommandButton object from within Excel
问题描述
Set objWord = CreateObject(Word.Application )
设置objDoc = objWord.Documents.Open(strFileName)
objWord.Visible = True
设置cmdSignatureButton = fncGetCommandButtonByName(NameOfCommandButtonImLookingFor,objDoc)
MsgBox h =& cmdSignatureButton.Picture.Handle
'它死在这里,给出错误:
'运行时错误-2147418113(8000ffff)
'自动化错误
'灾难性故障
私有函数fncGetCommandButtonByName(strName As String,objDoc As Word.Document)
Dim obj As Object
Dim i As Integer
对于i = objDoc.InlineShapes.Count到1步-1
与objDoc.InlineShapes(i)
如果.Type = 5然后
如果.OLEFormat.Object.Name = strName然后
设置fncGetCommandButtonByName = .OLEFormat.Object
MsgBox找到命令按钮对象'似乎在这里找到CommandButton对象
退出函数
结束如果
结束如果
结束
下一个
结束函数
不得不知道如何运行公共函数,但这很容易。感谢您的反馈,瑞恩。仍然不知道为什么你的工作和我的没有,但至少我绕过它。
I'm writing an Excel macro that opens up a Word document and looks for a CommandButton object, by Name. When it finds the object, it tries to check if it has a picture associated with it. It seems to be locating the object, but dies a "catastrophic" death when I try to reference the handle of the picture. I've done this before and looking to see if the picture's handle is zero has worked for me. Not sure what's up here, maybe someone else can see what I'm missing?
Set objWord = CreateObject("Word.Application")
Set objDoc = objWord.Documents.Open(strFileName)
objWord.Visible = True
Set cmdSignatureButton = fncGetCommandButtonByName("NameOfCommandButtonImLookingFor", objDoc)
MsgBox "h=" & cmdSignatureButton.Picture.Handle
' It dies here, giving the error:
' Runtime error -2147418113 (8000ffff)
' Automation error
' Catastrophic failure
Private Function fncGetCommandButtonByName(strName As String, objDoc As Word.Document)
Dim obj As Object
Dim i As Integer
For i = objDoc.InlineShapes.Count To 1 Step -1
With objDoc.InlineShapes(i)
If .Type = 5 Then
If .OLEFormat.Object.Name = strName Then
Set fncGetCommandButtonByName = .OLEFormat.Object
MsgBox "Found the Command Button object" ' Seems to find the CommandButton object here
Exit Function
End If
End If
End With
Next
End Function
OK, I think I have an approach, at least. I moved on to my next problem, which is very similar. In this case, I am looking for images within Command Buttons within an Excel spreadsheet, but I'm doing so from Access. Instead of trying to jump through hoops and get Access VBA to interrogate the Excel file, I put a Public Function into the Excel file that Access calls. Excel has no problem checking the button for an image, so it just returns the answer for me.
Had to figure out how to Run Public Functions, but that was easy enough. Thanks for the feedback, Ryan. Still not sure why yours worked and mine didn't, but at least I got around it.
这篇关于不能在Excel中的Word CommandButton对象上工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!