C#Microsoft Interop Excel 1997-2003 VBA宏:双击单元格中的事件不起作用



我们公司有一个旧的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(@"\serverxybemusterap" + "KV" + txtHv.Text + @"" + "Ap.xls")

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

Microsoft.Office.Core.MsoAutomationSecurity.msoAutomationSecurityLow;

如果不起作用,请尝试同时从c#代码调用启用双击行为的宏:

var xlapp = new Excel.Application();
xlapp.Visible = true;                        
xlapp.AutomationSecurity = Microsoft.Office.Core.MsoAutomationSecurity.msoAutomationSecurityLow;
xlapp.EnableEvents = true;
xlapp.Workbooks.Open(@"\serverxybemusterap" + "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
}

最新更新