不能在Excel中的Word CommandButton对象上工作 [英] Can't work on Word CommandButton object from within Excel

查看:379
本文介绍了不能在Excel中的Word CommandButton对象上工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个打开Word文档并通过Name查找CommandButton对象的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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆