Question Word Automation Memory Error

Ehaver282

New member
Joined
Dec 23, 2013
Messages
1
Programming Experience
1-3
Hello,

I have created a VB.net program that allows the user to download a word template and some data. The program then uses the macros within that document to fill in the correct locations. Then the user can save the file and close out word. When the user closes word my VB.net program uploads the file back to the server it was downloaded from. Then there is another side of the program that lets the user edit the document they had uploaded.


Everything works fine for most word documents. For some documents when the user creates the original document, changes something in it, saves and then goes back to my website to edit the file that was just uploaded word prompts then with an Insufficient Memory Error. I have tried many ways to upload the file from File stream, to memory stream writing, and everything ends in the same result.


This is the code I am using:


VB.NET:
Imports Microsoft.Office.Interop
Imports System
Imports System.IO
Imports System.Net
Imports System.Web
Imports System.Collections
Imports System.Collections.Specialized
Imports System.Threading
Public Class Form1


    ' Private WithEvents oWord As Word.Application
    Private trd As Thread
    Dim WithEvents oWord As New Microsoft.Office.Interop.Word.Application
    Dim WithEvents wc As New System.Net.WebClient()
    Dim list As New List(Of String)
    Dim GetString As String
    Dim FileWOEX As String
    Dim ORGDoc As String
    Dim MergeVar As String
    Dim Server As String
    Dim DocPath As String
    Dim FileName As String
    Dim ToPath As String
    Dim LibName As String
    Dim INCOMP As String
    Dim INNUM As String
    Dim strCommonAppData As String
    Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load


        Dim list As New List(Of String)
        Dim linef As String
        Dim x As Integer
        Try
            Const ssfCOMMONAPPDATA = &H23


            strCommonAppData = CreateObject("Shell.Application").NameSpace(ssfCOMMONAPPDATA).Self.Path & "\MCCIPRS\"
        Catch ex As Exception
            MsgBox("Data File Path Not Found, an Error occurred during install, please uninstall and reinstall our program!")
            oWord.Quit()
            Me.Close()
        End Try
        Try
            GetString = Command()
            LibName = GetString.Substring(0, 6)
            MergeVar = GetString.Substring(17, 1)
            INCOMP = GetString.Substring(6, 2)
            If (INCOMP.Substring(0, 1) = "0") Then
                INCOMP = INCOMP.Substring(1, 1)
            End If
            INNUM = GetString.Substring(8, 9)
            ORGDoc = GetString.Substring(18)
            Server = ORGDoc.Substring(0, (ORGDoc.IndexOf("/", 9)) + 1)
            DocPath = ORGDoc.Substring((ORGDoc.IndexOf("/", 9)) + 1)
            FileWOEX = ORGDoc.Substring(0, (ORGDoc.Length) - 4)
            FileName = IO.Path.GetFileName(DocPath)
            ToPath = DocPath.Substring(0, (DocPath.Length - FileName.Length))
        Catch ex As Exception
            MsgBox("Data from Web Is Invalid:" & ex.ToString())
            oWord.Quit()
            Me.Close()
        End Try
        Try
            If (System.IO.File.Exists(strCommonAppData & IO.Path.GetFileName(ORGDoc))) Then
                System.IO.File.Delete(strCommonAppData & IO.Path.GetFileName(ORGDoc))
            End If
            My.Computer.Network.DownloadFile(ORGDoc, strCommonAppData & IO.Path.GetFileName(ORGDoc), "", "")
        Catch ex As Exception
            MsgBox("There was a Problem Retrieving the Form from the web Please ensure the file exists or Contact you Administrator" & ex.ToString())
            oWord.Quit()
            Me.Close()
        End Try


        Try
            ' Make Word visible.
            oWord.Visible = True


            oWord.Documents.Open(strCommonAppData & IO.Path.GetFileName(ORGDoc))
        Catch ex As Exception
            MsgBox("There was an error when opening Word please ensure Word was installed correctly" & ex.ToString())
            oWord.Quit()
            Me.Close()
        End Try
        Try
            If MergeVar = "M" Then
                trd = New Thread(Sub()
                                     Using frm As New Form2
                                         Application.Run(frm)
                                     End Using
                                 End Sub)
                trd.Start()
                Try
                    If (System.IO.File.Exists(strCommonAppData & IO.Path.GetFileName(FileWOEX) & ".txt")) Then
                        System.IO.File.Delete(strCommonAppData & IO.Path.GetFileName(FileWOEX) & ".txt")
                    End If
                    My.Computer.Network.DownloadFile(FileWOEX & ".txt", strCommonAppData & IO.Path.GetFileName(FileWOEX) & ".txt", "", "")
                Catch ex As Exception
                    MsgBox("There was an issue downloading the Macro File from Server " & ex.ToString())
                    oWord.Quit()
                    Me.Close()
                End Try
                Using r As StreamReader = New StreamReader(strCommonAppData & IO.Path.GetFileName(FileWOEX) & ".txt")
                    ' Store contents in this String.
                    ' Read first line.
                    linef = Trim(r.ReadLine())
                    x = 0
                    Do While (Not linef Is Nothing)


                        ' Add this line to list.
                        list.Add(linef.Substring(13, linef.IndexOf("(") - 13))


                        list.Add(linef.Substring(linef.IndexOf("(") + 2, (linef.IndexOf(")") - 3) - linef.IndexOf("(")))
                        Try
                            Call oWord.Run(list(x), list(x + 1))
                        Catch ex As Exception
                            trd.Abort()
                            MsgBox("There was an issue filling Macros within this Word file the display may be incorrect")
                            'MsgBox("Macro Issues: " & list(x) & " " & list(x + 1) & " " & ex.ToString())
                        End Try
                        ' Read in the next line.
                        linef = r.ReadLine
                        x = x + 2
                    Loop
                End Using
        trd.Abort()


            Else
        x = 0
            End If
        Catch ex As Exception
            trd.Abort()
            MsgBox("There was an issue filling Macros within this Word file the display may be incorrect")
        End Try
        System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = False
        Me.Opacity = 0
    End Sub


    'create wordApplication object with the key word ?WithEvents?
    'Private Sub oWord_DocumentBeforeSave(ByVal Doc As Microsoft.Office.Interop.Word.Document, ByRef SaveAsUI As Boolean, ByRef Cancel As Boolean) Handles oWord.DocumentBeforeSave
    '   Cancel = True
    '   MsgBox("Saving is disabled for these document, Make your changes and close out of Word to Save")
    'End Sub


    Private Sub oWord_DocumentBeforeClose(ByVal Doc As Microsoft.Office.Interop.Word.Document, ByRef Cancel As Boolean) Handles oWord.DocumentBeforeClose
        Cancel = True
        oWord.ActiveDocument.Close()
        'oWord.Quit()
        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


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


            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(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
                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
        'oWord.Application.Quit()
        oWord = Nothing


        Me.Close()
    End Sub


End Class

 
Top Bottom