【问题标题】:C# Microsoft Interop Excel 1997-2003 VBA Macros: Double Click Event Into Cell Not WorkingC# Microsoft Interop Excel 1997-2003 VBA 宏:双击事件进入单元格不起作用
【发布时间】:2020-11-10 15:16:05
【问题描述】:

我们公司有一个旧的 Excel 1997-2003 VBA 应用程序 (.xls)。如果双击工作表中的特定单元格,则会执行宏。如果我手动打开文件并双击单元格,这完全可以正常工作,然后执行宏。

但是,在我的 C# 代码中,我尝试使用 microsoft.office.interop.excel.application 对象打开 .xls。打开工作正常,但是当我双击单元格时,它们不会执行宏而是跳转到单元格编辑...

这是双击单元格时执行的 VBA Sub:

    Sub DblClickHandle()
   z = ActiveCell.Row
    Select Case Cells(1, 256)
        Case "1009":   'Bearbeitungs-Stati
            Call SchutzAus
            Select Case ActiveCell.Column
                Case 2:   'Position bearbeiten
                    Worksheets("AP-Bearbeitung").Activate
                    Call SchutzAus
                    Cells(z, 4) = Format$(Now, "DD.MM.YYYY")
                    Call SchutzEin
                    Select Case Cells(z, 2).FormulaR1C1
                        Case "Protokoll erzeugen"
                            If Existiert("Deckblatt") = True Then Call ReSet_AP
                            Call AP_Erstellen
                        Case "Export":
                            Call AP_Export
                        Case "Finanzierungsbestätigung"
                            Call CreateFinBest
                        Case "Ausdruck mit Preisen":
                            Call AP_Print(True)
                        Case "Ausdruck ohne Preise":
                            Call AP_Print(False)
                        Case "Ausdruck in PDF mit Preisen"
                            Call AP_PDF(True)
                        Case "Ausdruck in PDF ohne Preise"
                            Call AP_PDF(False)
                        Case "Einzelblattausdruck mit Preisen":
                            Call AP_DruckenEinzeln(True)
                        Case "Ausdruck mit Blattauswahl"
                            Call AP_Mehrfachdrucken
                        Case "Einzelblattausdruck ohne Preise":
                            Call AP_DruckenEinzeln(False)
                        Case "Laufzettel"
                            Call AP_Laufzettel
                        Case "Abtretung und Zahlungsanweisung"
                            Call CreateAbtZahAn
                        Case "Abtretung bei öffentl. Förderung"
                            Call CreateAbtZahAnÖ
                        Case Else
                            On Error GoTo errhandle779
                            Worksheets(Cells(z, 2).FormulaR1C1).Activate
                            On Error GoTo 0
                    End Select
                Case 1, 5:
                    If Cells(z, 10) Then
                        Cells(z, 5).FormulaR1C1 = ""
                      Else
                        Cells(z, 5) = Format$(Now, "DD.MM.YYYY")
                    End If
                Case 4:
                    If Len(Cells(z, 4).FormulaR1C1) > 2 Then
                        Cells(z, 4).FormulaR1C1 = ""
                      Else
                        Cells(z, 4) = Format$(Now, "DD.MM.YYYY")
                    End If
            End Select
            Call SchutzEin
        Case "1099":     'Pakete für Laufzettel
            Call SchutzAus
            If ActiveCell.FormulaR1C1 = "ý" Then
                ActiveCell.FormulaR1C1 = "¨"
                Call SchutzEin
                Exit Sub
            End If
            If ActiveCell.FormulaR1C1 = "¨" Then
                ActiveCell.FormulaR1C1 = "ý"
                Call SchutzEin
                Exit Sub
            End If
            Call SchutzEin
        Case "1999"
            Call BeiTasteEinfg
            Call SchutzAus
            Select Case ActiveCell.value
            Case "Kellerbaufirma":
               Cells(23, 1).value = "Keller fertig am"
            Case "Fa. Bodenplatte"
               Cells(23, 1).value = "Bodenplatte fertig am"
            Case "Keller fertig am"
               Cells(19, 1).value = "Kellerbaufirma"
            Case "Bodenplatte fertig am"
               Cells(19, 1).value = "Fa. Bodenplatte"
            Case Else:
            End Select
            Call SchutzEin
        Case Else:
            Call BeiTasteEinfg
    End Select
    Exit Sub
errhandle779:
    MsgBox Error()
    Resume Next
End Sub

我搜索了所有内容以找到类似的问题,但没有找到任何东西。 您能帮我解决这个问题吗?

#Here is my C# code in which I am opening the .xls macro file with excel interop:

var xlapp = new Excel.Application();
xlapp.Visible = true;                        
xlapp.AutomationSecurity = Microsoft.Office.Core.MsoAutomationSecurity.msoAutomationSecurityByUI;
xlapp.EnableEvents = true;
xlapp.Workbooks.Open(@"\\serverxy\bemuster\ap\" + "KV" + txtHv.Text + @"\" + "Ap.xls")

【问题讨论】:

  • 该文件的 VBA 中是否有“DoubleClick”方法,如果有,它位于何处?像Application.DoubleClick="SomeFunction" 这样的东西在整个项目的 VBA IDE 中只需 CTRL+F “DoubleClick”
  • 它位于一个VBA模块中:Sub EnableRedirections() Application.OnDoubleClick = "DblClickHandle" ' Rufe Auto_Öffnen sendkeys End Sub
  • @jamheadart :这并没有解决问题。仍在编辑单元格,而不是执行宏。
  • 尝试调用激活双击行为的宏,代码sn-p见下面的答案

标签: c# excel vba excel-interop


【解决方案1】:

您可以先尝试将安全级别自动更改为低,而不是让用户定义它:

Microsoft.Office.Core.MsoAutomationSecurity.msoAutomationSecurityLow;

如果这不起作用,请尝试从您的 c# 代码中调用启用 doubleClick 行为的宏:

var xlapp = new Excel.Application();
xlapp.Visible = true;                        
xlapp.AutomationSecurity = Microsoft.Office.Core.MsoAutomationSecurity.msoAutomationSecurityLow;
xlapp.EnableEvents = true;
xlapp.Workbooks.Open(@"\\serverxy\bemuster\ap\" + "KV" + txtHv.Text + @"\" + "Ap.xls")

    string macro = "Module1.EnableRedirections";
    /// replace Module1 with whatever module you found the sub routine in

    try
    {
        xlapp.Run(macro);
    }
    catch (Exception ex)
    {
        // Some error handling
    }

【讨论】:

  • 很遗憾,这个解决方案没有解决问题。如果我双击,它会进入 catch 块。我将 VBA-Subprocedure 添加到我的初始帖子中。这是双击单元格时执行的子程序。
  • 您不想调用DblClickHandle() 宏本身,而是想调用EnableRedirections 宏-因此您在答案中建议的编辑将不起作用。 DblClickHandle() 是一个只应在双击时触发的例程,EnableRedirections 是告诉 Excel 双击时该做什么。
  • 在插入模块的名称而不是子过程的名称后,它起作用了。谢谢!
  • 太棒了,很高兴它成功了。我不确定你为什么要强制 EnableRedirections 运行,因为手动打开时它似乎会自动调用,所以即使通过 Excel.Interop 打开它也应该这样做......但至少我们找到了解决方案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-10
  • 1970-01-01
  • 1970-01-01
  • 2014-10-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多