if Excel Workbook is open then… VBA

后端 未结 3 1857
忘了有多久
忘了有多久 2021-01-19 03:45

How could I write code to say.

    Dim xlApp As Excel.Application
    Dim xlWorkbook As Excel.Workbook
    Dim xlWorksheet As Excel.Worksheet
3条回答
  •  孤城傲影
    2021-01-19 04:11

    First try using getobject: if it throws an error then use createobject:

      Dim xlApp As Excel.Application
    
      On Error Resume Next
      Set xlApp = GetObject(, "Excel.Application")
      On Error GoTo 0
    
      If xlApp Is Nothing Then
        Set xlApp = CreateObject("Excel.Application")
        xlApp.Visible = True
      End If
    

提交回复
热议问题