vb打开excel文件的相关操作

Dim xlApp As EXCEL.Application
Dim xlBook As EXCEL.Workbook
Dim filename
Dim reportini As String

filename = "\ReportModel\BatchReport.xlsm"
reportini = App.Path + "\report.ini"
On Error Resume Next
      gstrLanguage = getstring(reportini, "database", "Language")
     Call TextIni
    
    Set xlApp = GetObject(, "Excel.Application")     '判断EXCEL是否打开
 
    If Err.Number <> 0 Then
        Set xlApp = CreateObject("Excel.Application") '创建EXCEL对象
        xlApp.Visible = False '设置EXCEL对象不可见
    End If
    Err.Clear
     
    If Dir(App.Path & filename) = "" Then   '判断文件是否存在
        MsgBox Msg_7, vbOKOnly, Title_SysMsg
        Exit Sub
    End If
     
    '判断文件是否打开
 
    For Each xlBook In xlApp.Workbooks
        If xlBook.Name = Right(filename, 16) Then
            MsgBox Msg_6, vbOKOnly, Title_SysMsg
            xlBook.Activate
            xlApp.WindowState = xlMaximized
            Exit Sub
        End If
    Next
    Set xlBook = xlApp.Workbooks.Open(App.Path & filename) '打开工件簿文件
    xlBook.RunAutoMacros (xlAutoOpen) '运行EXCEL启动宏
    'xlBook.RunAutoMacros (xlAutoClose) '运行EXCEL关闭宏
 
    xlApp.Visible = True

你可能感兴趣的:(vb打开excel文件的相关操作)