试图检测图片是否在CommandButton上

时间:2017-03-15 18:16:02

标签: excel vba access-vba

这类似于Can't work on Word CommandButton object from within Excel,但我现在正尝试从Access VBA获取Excel文件中的命令按钮。具体来说,我试图找出命令按钮是否有与之关联的图片。

好像我可以找到Command Button。我知道如果按钮的图片处理为零,则表示没有图片存在。但我似乎无法进入Command Button的Picture属性。这是我的方法:

Private Sub subGatherFileInfo(fileEFPN As File, wrdDoc As Word.Document)
    Dim cmdButton 'As CommandButton
    Dim wkbEFPN As Workbook
    Dim app As New Excel.Application

    app.Visible = True
    Set wkbEFPN = app.Workbooks.Open(FileName:=fileEFPN.Path)       'These options don't seem to matter->, Editable:=False, ReadOnly:=True)
    '
    ' Validate that the sheet we are looking for exists in the file and is visible
    '
    .
    .
    .
    wkbEFPN.Activate
    wkbEFPN.Sheets("Encounter").Select

    wkbEFPN.Sheets("Encounter").Unprotect strSheetPassword      ' Doesn't seem to matter if I do this
    wkbEFPN.ToggleFormsDesign                                   ' Doesn't seem to matter if I do this
    Set cmdButton = fncgetCommandButton(wkbEFPN.Sheets("Encounter"), "cmdClientSignature")
    '
    ' When I use this, it tells me "CommandButton"
    MsgBox TypeName(cmdButton.Object)
    ' When I use this, it tells me "cmdClientSignature", just like I expect.
    MsgBox cmdButton.Object.Name
    '
    ' THIS FAILS WITH AN ERROR: "Method 'Picture' of object 'ICommandButton' failed"!!!
    MsgBox cmdButton.Object.Picture.Handle

GetOutOfHere:
    MsgBox "Closing " & wkbEFPN.Name
    app.DisplayAlerts = False
    wkbEFPN.Close False
    app.Quit
    Set app = Nothing
    MsgBox "Closed it!"
End Sub

Private Function fncgetCommandButton(sht As Worksheet, strButtonName)
    Dim i As Integer
    Dim shp As Shape

    For i = 1 To sht.Shapes.Count
        Set shp = sht.Shapes(i)
        If shp.OLEFormat.Object.Name = strButtonName Then
            Set fncgetCommandButton = shp.OLEFormat.Object
            MsgBox "Found " & shp.OLEFormat.Object.Name         ' Works OK!
            Exit Function
        End If
    Next
End Function

任何想法都将不胜感激。我想知道是否需要设置一个参数,或者我需要进入的状态,以便我的代码可以访问该对象?谢谢!

0 个答案:

没有答案