Microsoft Office 2013 Crashes from Vb.net Program

633 Views Asked by At

I have a vb.net program that the user is able to create a word file from a text file, open and edit it, and save their changes back to a web server.

When the user creates the file and uploads it everything works fine. The issue happens when they open any document. When the user is done editing and saves the file then closes out word, Office throw a not responding message and forces them to close the office program, but when I check the document they tried to upload to the server the changes they made are present so my program is completing its process without error, I would assume.

This only happens in Microsoft Office 2013.

Below is the code I am using:

Private Sub oWord_DocumentBeforeClose(ByVal Doc As Microsoft.Office.Interop.Word.Document, ByRef Cancel As Boolean) Handles oWord.DocumentBeforeClose
    'Cancel = True
    Try
        oWord.ActiveDocument.Close()
    Catch ex As Exception
        MsgBox("There was an issue closing word document" & ex.ToString())
    End Try
    'oWord.Quit()
    Try
        oWord.Application.Quit()
    Catch ex As Exception
        MsgBox("There was an issue closing word" & ex.ToString())
    End Try
    Try
        Dim filepath As String
        'Dim url As String = Server & "php/PD026U.php"
        Dim url As String = Server & LibName & "/PD026U.pgm"
        'Dim url As String = "http://192.168.95.1:83/file.php"
        filepath = strCommonAppData & IO.Path.GetFileName(ORGDoc)

        Dim request As HttpWebRequest = DirectCast(HttpWebRequest.Create(url), HttpWebRequest)

        request.PreAuthenticate = True
        request.AllowWriteStreamBuffering = True
        request.KeepAlive = True
        request.ProtocolVersion = HttpVersion.Version10

        Dim boundary As String = System.Guid.NewGuid().ToString()

        request.Accept = "text/html , application/xhtml+xml, */*"
        request.AutomaticDecompression = DecompressionMethods.GZip
        request.ContentType = String.Format("multipart/form-data; boundary={0}", boundary)
        request.Method = "POST"
        request.Credentials = New System.Net.NetworkCredential("ehavermale", "ernie1")

        ' Build Contents for Post
        Dim header As String = String.Format("--{0}", boundary)
        Dim footer As String = header & "--"

        Dim contents As New System.Text.StringBuilder()
        Dim FileHead As New System.Text.StringBuilder()

        ' file
        FileHead.AppendLine(header)
        FileHead.AppendLine(String.Format("Content-Disposition: form-data; name=""upfile""; filename=""{0}""", IO.Path.GetFileName(filepath)))
        FileHead.AppendLine("Content-Type: application/msword")
        FileHead.AppendLine()

        contents.AppendLine(vbCrLf & header)
        contents.AppendLine("Content-Disposition: form-data; name=""task""")
        contents.AppendLine()
        contents.AppendLine("upload")

        contents.AppendLine(header)
        contents.AppendLine("Content-Disposition: form-data; name=""INCOMP""")
        contents.AppendLine()
        contents.AppendLine(INCOMP)

        contents.AppendLine(header)
        contents.AppendLine("Content-Disposition: form-data; name=""ProgLib""")
        contents.AppendLine()
        contents.AppendLine(LibName)

        contents.AppendLine(header)
        contents.AppendLine("Content-Disposition: form-data; name=""INCNUM""")
        contents.AppendLine()
        contents.AppendLine(INNUM)

        contents.AppendLine(header)
        contents.AppendLine("Content-Disposition: form-data; name=""ToPath""")
        contents.AppendLine()
        contents.AppendLine(ToPath)

        ' Footer
        contents.AppendLine(footer)

        ' This is sent to the Post
        Dim bytes As Byte() = System.Text.Encoding.UTF8.GetBytes(contents.ToString())
        Dim FileBytes As Byte() = System.Text.Encoding.UTF8.GetBytes(FileHead.ToString())

        request.ContentLength = bytes.Length + FileHead.Length + New FileInfo(filepath).Length

        Using requestStream As Stream = request.GetRequestStream()
            requestStream.Write(FileBytes, 0, FileBytes.Length)
            Using fileStream As New IO.FileStream(filepath, IO.FileMode.Open, IO.FileAccess.Read)

                Dim buffer(4096) As Byte
                Dim bytesRead As Int32 = fileStream.Read(buffer, 0, buffer.Length)

                Do While (bytesRead > 0)
                    requestStream.Write(buffer, 0, bytesRead)
                    bytesRead = fileStream.Read(buffer, 0, buffer.Length)
                Loop
                fileStream.Close()
            End Using
            requestStream.Write(bytes, 0, bytes.Length)
            requestStream.Flush()
            requestStream.Close()

            Using response As WebResponse = request.GetResponse()
                Using reader As New StreamReader(response.GetResponseStream())
                    Dim strResponseData As String = reader.ReadToEnd()
                    TextBox1.Text = strResponseData
                End Using
            End Using
        End Using
    Catch ex As Exception
        MsgBox("There was an Error on File Upload Please Contact you Administrator for assistance : " & ex.ToString())
    End Try

    Me.Close()
End Sub

I have tried changing how I close my word program, the order I close the document and word and tried to keep word open altogether and nothing I have tried works.

Is there an issue with Office 2013 and beforeclose or programmatically closing a document?

1

There are 1 best solutions below

0
On

I found my problem. There is a COM Add-in called Send to Bluetooth that needed to be disabled now everything is working fine