locked
Error - cannot access file because it is being used by another process

    Question

  • Hi,

    I'm writing a little program that cycles through a listbox containing MS Word documents. It saves each file as an HTML file and then opens that file and parses all the needless code that MS Word throws in. I can save it as HTML no problem but when I come to open my newly created HTML file I get the error:

    "The process cannot access the file "myfile" because it is being used by another process"

     

    Here's the subroutine for converting:

    The highlighted row is where it fails...right after my SaveAsHTML() call

     

    Code Snippet

    Private Sub btnConvert_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConvert.Click

    Dim newtext As String = String.Empty

    Dim newfile As String = String.Empty

    Dim filename As String

    Try

    For Each item As Object In lstTo.Items

    filename = txtSelectFiles.Text & "\" & CType(item, String).ToString

    SaveAsHTML(filename)

    filename = Replace(filename, ".doc", ".htm")

    Dim sr As New StreamReader(filename, System.Text.Encoding.Default)

    Dim textstream As String = sr.ReadToEnd()

    sr.Close()

    newtext = CleanHTML(textstream)

    newfile &= newtext

    Next

    Dim newfilename As String = txtSelectFiles.Text & "\" & txtOutputFilename.Text

    Dim fs As New FileStream(newfilename, FileMode.Create, FileAccess.Write)

    Dim sw As New StreamWriter(fs, System.Text.Encoding.Default)

    sw.WriteLine(newfile)

    sw.Close()

    MessageBox.Show("Completed")

    Catch ex As Exception

    MessageBox.Show("An error occurred whil processing these files " & vbCrLf & ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)

    End Try

     

    End Sub

     

    and here's my subroutine for saving as HTML:

     

    Code Snippet

    Private Sub SaveAsHTML(ByVal fname As String)

    Dim missing As Object = System.Reflection.Missing.Value

    Dim app As New Word.Application

    Dim doc As New Word.Document

    Dim filename As String = fname.ToString

    Dim fileToSave As String = Replace(filename, ".doc", ".htm")

    Dim isReadOnly As Object = False

    Dim isVisible As Object = False

    'For filtered HTML Output

    Dim fltDocFormat As Object = 8

     

    doc = app.Documents.Open(filename, missing, isReadOnly, missing, missing, missing, missing, missing, missing, missing, missing, isVisible)

    doc.SaveAs(fileToSave, fltDocFormat, missing, missing, missing, missing, missing, missing, missing, missing, missing)

    app.Quit(missing, missing, missing)

    End Sub

     

    I open the document but don't close it in any way so it must think that the document is still open and won't let me go any further. I tried doc.close at the end if this subroutine but that didn't work.

     

    Does anybody see what the problem is?

     

    Thanks

    Friday, July 20, 2007 6:19 PM

Answers

  • if it is a problem waiting to save then maybe you could open the word doc with a process

    then you save and finally kill the process

    process.kill()

    then use the process exited event to continue on

     

    or maybe you can use a try catch statement in a timer to repeatedly try and open the file

     

    I don't know if any of these would be the best way

    just some thoughts

     

    Friday, July 20, 2007 10:33 PM

All replies

  • Hi,

    Try using some wait till app becomes invalid. It is possible that your programm is trying to access file when the file is in the process of quitting. So wait till that process is complete.

     

    HTH,

    Vallari

    Friday, July 20, 2007 8:36 PM
  • if it is a problem waiting to save then maybe you could open the word doc with a process

    then you save and finally kill the process

    process.kill()

    then use the process exited event to continue on

     

    or maybe you can use a try catch statement in a timer to repeatedly try and open the file

     

    I don't know if any of these would be the best way

    just some thoughts

     

    Friday, July 20, 2007 10:33 PM
  • Hi vallari,

    I'm kinda new to this...do you know any good references on how to do this? I've tried looking up info on processes and I'm having problems incorporating this into my code.

     

    Thanks

    Monday, July 23, 2007 1:48 PM
  • Hi,

     

    You will get more information at http://blogs.msdn.com/vbfaq/archive/2004/05/30/144573.aspx 

    If you still not able to get it. Let me know.

     

    HTH,

    Vallari

    Friday, July 27, 2007 4:15 AM
  • Thanks. I'll try this out.

     

    sensfan

    Monday, July 30, 2007 7:41 PM