调用Excel中的宏

来源:互联网 发布:linux nfs nobody 编辑:程序博客网 时间:2024/06/03 05:08
    '过程名称:ExcelFl
    '功能描述:调用EXCEL表的宏
    '接收参数:sExcelPath:Excel文件的路径,MacroName:调用宏的名称
    '返回参数:Ture:执行成功,False:执行失败
    '创建人员及日期:zzz@2007-01-15
    '注意事项:暂无
    Public Function ExcelFl(ByVal sExcelPath As StringByVal MacroName As StringAs Boolean
        
Dim oXL As Microsoft.Office.Interop.Excel.Application
        
Dim oWB As Microsoft.Office.Interop.Excel.Workbook
        
Dim oSheet As Microsoft.Office.Interop.Excel.Worksheet
        
Dim oRng As Microsoft.Office.Interop.Excel.Range

        
Try
            oXL 
= CreateObject("Excel.Application")
            oXL.Visible 
= True
            oWB 
= oXL.Workbooks.Open(sExcelPath)
            oXL.Run(MacroName)
            oWB.Close(
True)
            oXL.Quit()

            ExcelFl 
= True
        
Catch ex As Exception
            
Throw New Exception("Error In ExcelFl!!!" & vbCrLf & _
                               
"Source:" & ex.Source.ToString() + " Message:" + ex.Message.ToString())
            ExcelFl 
= False
        
Finally
            oXL 
= Nothing
            oWB 
= Nothing
            oSheet 
= Nothing
            oRng 
= Nothing
        
End Try
    
End Function