none
Why is my file locked by xxx.vshost.exe ? RRS feed

  • Question

  • I stumbled upon the following thread today that I thought would help me, but it seems I have found myself another problem ... 

    The Other Thread ...

    I replied in there, but as the thread is old, I thought I would be better asking the question anew ... I have had some good advice regarding what I want to do (check if a Word file is already open) from Cindy Meister, but this option suddenly looked so simple in comparison ...

    I check if a Word Document exists, if it doesn't I create it anew from a skeleton version & then I open it for editing, regardless of it being previously there or just created. This works fine. All I want to do is check if it is already Open, & if so, then just make it TopMost & continue my editing.

    My code in this attachment, with the code from the above linked thread commented out, works if the file is not open, whether it exists originally or not, so I'm just checking for it being open now ...

     'Dim myFileOpen As Boolean
    
            'myFileOpen = IsFileOpen(myNewsLetter)
    
            'If myFileOpen = True Then
            'MsgBox("The file is already open")
            'Else
            Dim MSWord As New Word.Application
            Dim MSDoc As New Word.Document
            MSDoc = MSWord.Documents.Open(myNewsLetter)
            MSWord.WindowState = Word.WdWindowState.wdWindowStateNormal
            MSWord.Visible = True
            MSWord.Activate()
            'End If
    
    ====================================================================
    
        Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
            Me.Close()
        End Sub
    
        'Function IsFileOpen(ByRef sName As String) As Boolean
        '    Dim fs As FileStream
        '    Try
        '        fs = File.Open(sName, FileMode.Open, FileAccess.Read, FileShare.None)
        '        IsFileOpen = False
        '    Catch ex As Exception
        '        IsFileOpen = True
        '    End Try
        'End Function

    However, when I activate the Code that is commented out I get an error ... why ?!?!?

    Details are as follows ...

    I assume it is something to do with the command ...

    fs = File.Open(sName, FileMode.Open, FileAccess.Read, FileShare.None)

    ... but I am clutching at straws, really !!!

    Any ideas ?!?


    Friday, January 25, 2013 7:08 PM

Answers

  • OK, solved this myself ... woo hoo, go me !!!

    I added the lines ...

    fs.Close()
    fs.Dispose()
    fs = Nothing

    ... after 

    IsFileOpen = False

    I don't know if this is ideal if you are looking to do this in big business applications, but for my purposes it's just fine :-)

    • Marked as answer by vodkasoda Saturday, January 26, 2013 11:30 AM
    Saturday, January 26, 2013 11:30 AM

All replies

  • OK, solved this myself ... woo hoo, go me !!!

    I added the lines ...

    fs.Close()
    fs.Dispose()
    fs = Nothing

    ... after 

    IsFileOpen = False

    I don't know if this is ideal if you are looking to do this in big business applications, but for my purposes it's just fine :-)

    • Marked as answer by vodkasoda Saturday, January 26, 2013 11:30 AM
    Saturday, January 26, 2013 11:30 AM
  • Hi Vodkasoda,

    Thank you for sharing your solution which might be very helpful to other community memebers.

    Best regards,


    Quist Zhang [MSFT]
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, January 28, 2013 7:03 AM
    Moderator