【发布时间】: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