CreateObjectの部分、起動の失敗の判定の部分はこれでよいのか、ちょっと微妙。
コード
RPAのツールでVBSで機能拡張できるのがあるので、起動済みのBookも取得できると便利かな。Option Explicit Call Main1 '** Subroutine ********************************************' Sub Main1() Dim Excel_app Set Excel_app = CreateExcel If Excel_app Is Nothing Then Msgbox "Excelの起動に失敗しました。" Exit Sub End If Dim wb, ws Const xlWBATWorksheet = -4167 With Excel_app Set wb = .Application.WorkBooks.Add(xlWBATWorksheet) Set ws = wb.Worksheets(1) ws.Name = "テスト" End With End Sub Sub Main2() Dim Excel_app Set Excel_app = GetExcel If Excel_app Is Nothing Then Msgbox "Excelの取得に失敗しました。" Exit Sub End If Dim wb, ws With Excel_app Set wb = .Application.WorkBooks(1) Set ws = wb.Worksheets(1) Msgbox ws.Name End With End Sub '** Function **********************************************' '============================' Function CreateExcel() '============================' Set CreateExcel = Nothing Dim objXL, i Set objXL = CreateObject("Excel.Application") i = 0 Do While objXL Is Nothing i = i + 1 If i > 5 Then Exit Function Else WScript.Sleep(600) End If Loop objXL.Visible = True Set CreateExcel = objXL End Function '============================' Function GetExcel() '============================' Set GetExcel = Nothing Dim objXL Set objXL = GetObject(, "Excel.Application") If objXL Is Nothing Then Exit Function Else objXL.Visible = True Set GetExcel = objXL End If End Function
0 件のコメント:
コメントを投稿