none
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
    Do
    Err.Clear
    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
    Loop
    On Error Goto 0
    oFile.Close
    WScript.Echo "The file is now unlocked"

    Can anyone assist?


    Thanks!

    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? 

    Regards,

    Ashidacchi

    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:

    https://social.technet.microsoft.com/Forums/scriptcenter/en-us/home?forum=ITCG


    Thank you for participating in the forum activities.

    Best Regards,

    Cherry



    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.

    Sorry

     



    Cor


    • 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. 


    Success
    Cor

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

    I need VB Script, not VBA.

    Thanks!

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

    I need VB Script, not VBA.

    Thanks!

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

    Use this forum.

    https://social.technet.microsoft.com/Forums/scriptcenter/en-US/home?forum=ITCG


    Success
    Cor

    Thursday, September 14, 2017 7:58 AM