2017-08-30 4 views
0

私は1つの.vbsファイル内の2つの機能があります。閉会Excelアプリケーション

Function OpenTarget(ByVal reviewPeriod) 
    ' check if file exists 
    Dim FSO 
    Set FSO = CreateObject("Scripting.FileSystemObject") 

    If fso.FileExists("FILE_" & reviewPeriod) Then 
     ' the target file is created for the first time for the given reviewPeriod, hence open the template 
     Set objExcel_target      = CreateObject("Excel.Application") 
     objExcel_target.Application.Visible  = True 
     objExcel_target.Application.DisplayAlerts = False 
     Set OpenTarget       = objExcel_target.Workbooks.Open("FILE_" & reviewPeriod) 
    Else 
     ' the target file is created for the first time for the given reviewPeriod, hence open the template 
     Set objExcel_target      = CreateObject("Excel.Application") 
     objExcel_target.Application.Visible  = True 
     objExcel_target.Application.DisplayAlerts = False 
     Set OpenTarget       = objExcel_target.Workbooks.Open("FILE_TEMPLATE") 
    End If 
End Function 

Function CloseTarget(target, reviewPeriod) 
    ' Save and close workbook 
    target.SaveAs "Path_To_Another_File" 
    target.Close (False) 

    objExcel_target.Application.Quit 
End Function 

私は2番目に位置する第三の機能、これらの二つの機能を使用したいが.vbsファイル。スクリプトはこの時点で失敗:

objExcel_target.Application.Quit 

私はCloseTarget()objExcel_target知りませんが、どのように私は最終的にもCloseTarget機能を使用してアプリケーションを閉じることができることを理解できますか?

+0

'objExcel_target.Application.Quit'を' target.Application.Quit'に変更するだけではないでしょうか? –

+0

いいえ - 残念ながらそうではありません。 targetはブックです。私はClose()を実行できます。 objExcel_targetは私が単にQuit()したいアプリケーションです – kalinkula

答えて

2

ブックを閉じると、そのプロパティにアクセスできなくなります。それを閉じる前に、ブックからアプリケーションを取得します。

Function CloseTarget(target, reviewPeriod) 
    Set xl = target.Application 

    ' Save and close workbook 
    target.SaveAs "Path_To_Another_File" 
    target.Close (False) 

    xl.Quit 
End Function

それが機能OpenTarget()内のローカル変数のように見えるので、あなたがobjExcel_targetを使用することはできません。

+0

クール!これは仕事です。ありがとう!! – kalinkula

関連する問題