how programatically know excel file has macro file in C#
i have a one 2003 excel file. using ofc.exe file i have converted the 2003 excel file in to 2007 exls fil开发者_运维问答e.
now my problem is before convert the file can i know the excel file is a macro excel file in c#.net ?
Reading up on this answer I found that using Application.VBE.ActiveVBProject.VBComponent
seems to be what you want.
A similar question was asked on the MSDN forums albeit that one concerns Visio not Excel. I'm sure you can adapt the code accordingly.
Further on this SO question asks exactly the same as you do. Albeit this answers does not seem to cover C#...
Over at the eggheadcafe one user suggests using this code snippet to loop through all VB-components:
Sub Test()
If bHasMacros(ActiveWorkbook) Then
MsgBox ActiveWorkbook.Name & " has macros."
End If
End Sub
Function bHasMacros(ByRef wkbBook As Workbook) As Boolean
Dim cmpComponent As VBIDE.VBComponent
For Each cmpComponent In wkbBook.VBProject.VBComponents
If cmpComponent.CodeModule.CountOfLines > 1 Then
bHasMacros = True
Exit Function
End If
Next cmpComponent
End Function
精彩评论