【问题标题】:Clicking/Simulating a Click using a VBA macro使用 VBA 宏单击/模拟单击
【发布时间】:2019-12-29 19:36:31
【问题描述】:

我正在寻找一个 VBA 宏,它将执行或单击功能区中 SAP Analysis for Office plug-in 中的按钮。

目前,我有一个可行的替代方案,使用功能区选项卡的热键和我在 VBA 宏中使用 SendKeys 发送的按钮,但它不够强大,因为不同的用户可能有不同的功能区布局,因此不同的热键组合相同的功能。

这是我想以编程方式单击的按钮。

您能否使用当前推荐的 Office fluent 功能区概念帮助我实现这一目标?

编辑:宏记录器不会记录功能区上的操作。

编辑 2:我相信我掌握了包含分析功能区结构的 XML 文件。这是我需要的按钮结构的屏幕截图。

【问题讨论】:

  • 我将首先打开宏记录器并使用功能区执行您想要的操作,然后停止录制并查看它提供给您的代码。
  • 我在发布问题之前尝试过。不幸的是,宏记录器无法识别功能区上的操作。
  • 根据您的other question,我假设您设法找到了一种使用 UIAutomationClient 库单击功能区按钮的方法。你考虑把它贴在这里吗?我个人很想看看你是怎么做的。
  • @DecimalTurn,是的,你是对的!我确实管理了一个方法,但是我遇到了一个问题。stackoverflow.com/questions/57711269/…
  • @DecimalTurn,我已经添加了代码。我希望它可以对某人有用。

标签: excel vba sap excel-2016


【解决方案1】:

我找到了一种使用 MS 提供的 UIAutomation 框架的方法,该框架可以在您的项目中引用为 UIAutomationClient

代码如下:

'global object to hold the translations
 Dim translations As Object

Public Enum oConditions
  eUIA_NamePropertyId
  eUIA_AutomationIdPropertyId
  eUIA_ClassNamePropertyId
End Enum

'element name translations for English
Function getEnglishTranslations() As Object
  Set Words = CreateObject("Scripting.Dictionary")
  'element, element name
  Words.Add "Analysis", "Analysis"
  Words.Add "Lower Ribbon", "Lower Ribbon"
  Words.Add "Design Panel Group", "Design Panel"
  Words.Add "Design Panel Menu", "Design Panel"
  Words.Add "Display Button", "Display"

  Set getEnglishTranslations = Words
End Function

'element name translations for German
Function getGermanTranslations() As Object
   Set Words = CreateObject("Scripting.Dictionary")

   'element, element name
   Words.Add "Analysis", "Analysis"
   Words.Add "Lower Ribbon", "Unteres Menüband"
   Words.Add "Design Panel Group", "Designbereich"
   Words.Add "Design Panel Menu", "Designbereich"
   Words.Add "Display Button", "Anzeigen"

   Set getGermanTranslations = Words
End Function

Function translate(element As String) As String
    translate = translations(element)
End Function

Sub toggleDisplay()
    Application.ScreenUpdating = False

    Dim oAutomation As New CUIAutomation 'the UI Automation Object

    'references for elements in the UI tree
    Dim root As IUIAutomationElement
    Dim xl As IUIAutomationElement
    Dim analysisTab As IUIAutomationElement
    Dim xlrib As IUIAutomationElement
    Dim dpanel As IUIAutomationElement
    Dim display As IUIAutomationElement

    'pattern objects which allow the execution of different UI elements' 
    functionality
    Dim oTogglePattern As IUIAutomationTogglePattern
    Dim expcolPattern As IUIAutomationExpandCollapsePattern
    Dim selPattern As IUIAutomationSelectionItemPattern

    'multi-clause condition references to locate the UI elements in the UI 
    tree.
    Dim analysisCond As IUIAutomationCondition
    Dim displayCond As IUIAutomationCondition
    Dim disPanelCond As IUIAutomationCondition

   'stores the language code for the current active language
   Dim languageCode As Integer

   'get the active application language
   languageCode = _
   Application.International(XlApplicationInternational.xlCountryCode)

   'choose the language dictionary based on the active language
   If languageCode = 49 Then
       Set translations = getGermanTranslations
   Else
       Set translations = getEnglishTranslations
   End If

   'get a reference to the UI element tree
   Set root = oAutomation.GetRootElement

   'locate the Excel window
   Set xl = root.FindFirst(TreeScope_Descendants, PropCondition(oAutomation, 
   eUIA_NamePropertyId, _
   "test wb for hidden ribbon - new (delete me).xlsm - Excel"))

   'click the Analysis tab
   Set analysisCond = _ 
   oAutomation.CreateAndCondition(PropCondition(oAutomation, 
   eUIA_NamePropertyId, translate("Analysis")), _
   PropCondition(oAutomation, eUIA_ClassNamePropertyId, "NetUIRibbonTab"))
   Set analysisTab = xl.FindFirst(TreeScope_Descendants, analysisCond)
   Set selPattern = _ 
   analysisTab.GetCurrentPattern(UIA_SelectionItemPatternId)
   selPattern.Select

   'locate the Design Panel Group
   Set xlrib = xl.FindFirst(TreeScope_Descendants, 
   PropCondition(oAutomation, eUIA_NamePropertyId, translate("Lower 
   Ribbon")))

   Set dpanel = xlrib.FindFirst(TreeScope_Descendants, 
   PropCondition(oAutomation, eUIA_NamePropertyId, translate("Design Panel 
   Group")))

   'try locating the Display button
   Set displayCond = _ 
   oAutomation.CreateAndCondition(PropCondition(oAutomation, 
   eUIA_NamePropertyId, translate("Display Button")), _
   PropCondition(oAutomation, eUIA_ClassNamePropertyId, 
   "NetUIRibbonButton"))

   Set display = dpanel.FindFirst(TreeScope_Descendants, displayCond)

   'true when the window is shrunk to a point where the display button
   'is part of the dropdown menu under Design Panel
   If display Is Nothing Then

       'expand the Design Panel dropdown first
       Set disPanelCond = _ 
       oAutomation.CreateAndCondition(PropCondition(oAutomation, 
       eUIA_NamePropertyId, translate("Design Panel Menu")), _

       PropCondition(oAutomation, eUIA_ClassNamePropertyId, "NetUIAnchor"))
       Set dpanel = dpanel.FindFirst(TreeScope_Descendants, disPanelCond)
       Set expcolPattern = _ 
       dpanel.GetCurrentPattern(UIA_ExpandCollapsePatternId)
       expcolPattern.Expand

      'attempt to locate the Display button again
      Set display = dpanel.FindFirst(TreeScope_Descendants, displayCond)
   End If

   'Click the Display button programmatically (FINALLY!!!)
   Set oTogglePattern = display.GetCurrentPattern(UIA_TogglePatternId)
   oTogglePattern.Toggle

   Application.ScreenUpdating = True
End Sub

'generate a Condition object with the string to be matched against the 
selected property
Function PropCondition(UiAutomation As CUIAutomation, Prop As oConditions, 
Requirement As String) As IUIAutomationCondition
Select Case Prop
    Case 0
          Set PropCondition = _
          UiAutomation.CreatePropertyCondition(UIA_NamePropertyId, 
          Requirement)
    Case 1
          Set PropCondition = _ 
          UiAutomation.CreatePropertyCondition(UIA_AutomationIdPropertyId, 
          Requirement)
    Case 2
          Set PropCondition = _
          UiAutomation.CreatePropertyCondition(UIA_ClassNamePropertyId, 
          Requirement)
    End Select
End Function

不幸的是,我使用这种方法遇到了another problem,目前我还没有解决方案。

这里有一个video,您也可以观看它来帮助您入门。

【讨论】:

    【解决方案2】:

    sry 还不能发表评论,希望我理解正确。

    您是否尝试为功能区设置 const 值?

    Me.RibbonName = "YourRibbonNameFromUsysRibbons"
    

    您可以在此处找到功能区名称:

    https://www.spreadsheet1.com/office-excel-ribbon-imagemso-icons-gallery-page-01.html

    希望我能帮到你。

    祝你有美好的一天。

    【讨论】:

    • 问题是功能区选项卡是 SAP 开发的名为 Analysis 的 Excel 插件的一部分。我正在努力寻找一种以编程方式执行“显示设计面板”功能的方法。该问题也可以描述为找到一种方法来识别和单击 Excel 中可用的任何功能区控件按钮。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-23
    • 2013-08-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多