我有一个 vb.net 程序,用户能够从文本文件创建一个 word 文件,打开并编辑它,并将他们的更改保存回 Web 服务器。
当用户创建文件并上传它时,一切正常。当他们打开任何文档时会发生此问题。当用户完成编辑并保存文件然后关闭 word 时,Office 会抛出一条没有响应的消息并强制他们关闭 Office 程序,但是当我检查他们试图上传到服务器的文档时,他们所做的更改是存在的我想我的程序正在完成它的过程而没有错误。
这只发生在 Microsoft Office 2013 中。
下面是我正在使用的代码:
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
我尝试改变关闭word程序的方式,关闭文档和word的顺序,并尝试完全保持word打开,但我尝试过的没有任何效果。
Office 2013 和 beforeclose 或以编程方式关闭文档是否存在问题?