VB Script to check if file is open and then close it RRS feed

  • Question

  • Hi Team -

    I need to write a vb script that checks if a file is open. If so, save and close the file.

    Here is what I have but it's not closing the file:

    Dim objExcel, objWorkbook 
    Set oFSO = CreateObject("Scripting.FileSystemObject")
    Set objExcel = CreateObject("Excel.Application")
    Set objWorkbook = objExcel.Workbooks.Open("C:\Hyperion_Batch\Client_Hours.csv")
    On Error Resume Next
    Set oFile = oFSO.OpenTextFile(objWorkbook, 8, False)
    if err.number = 0 then Exit Do
    WScript.Echo "File in use"
    WScript.Sleep 1000
    objWorkbook.Close False
    On Error Goto 0
    WScript.Echo "The file is now unlocked"

    Can anyone assist?


    Wednesday, September 13, 2017 7:18 PM

All replies

  • Hi,

    Please make it clear.  Which do you mean as "a file", Excel workbook or other files including CSV file? 



    Thursday, September 14, 2017 12:37 AM
  • I'm sorry, I meant ".xlsx", not .csv.

    Thank you!

    Thursday, September 14, 2017 12:51 AM
  • Hi cdtakacs1,

    Since this forum is discussing and asking questions about the Visual Basic programming language, IDE, libraries, samples, and tools . And your issue is more related to VB Script. This link about VB Script,

    please refer that:


    Thank you for participating in the forum activities.

    Best Regards,


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, September 14, 2017 1:54 AM
  • Ashidacchi

    I marked your reply as abuse (that button has more meanings than offending). 

    It seems to moderators have currently the assignment not to move real Off Topic software to that group. 

    The result, we get more spam of that and less real VB Net Questions.

    After looking again I saw that you gave an VB for Net answer on a question for scripting.




    • Edited by Cor Ligthert Thursday, September 14, 2017 6:19 AM
    Thursday, September 14, 2017 6:19 AM
  • Ah you did both. 

    Helping with VBA is not allowed in this forum. 

    Cherry gave a link, in my perception she should have moved the thread to that forum. 


    Thursday, September 14, 2017 7:34 AM
  • Thank you all.

    I need VB Script, not VBA.


    Thursday, September 14, 2017 7:46 AM
  • Thank you all.

    I need VB Script, not VBA.


    Yea but you are now in a plane factory asking for a repair set for your bicycle.

    Use this forum.



    Thursday, September 14, 2017 7:58 AM