保存 Word 文档 - 错误 [英] Saving Word document - error
问题描述
我有一些代码可以在 Word 文件中插入多个图像.一切都很好直到我尝试保存文档,然后出现此错误:
I have some code to insert multiple images in a Word file. Everything is good until I try to save the document, whereupon it gives this error:
pdf1.exe 中发生类型为System.Runtime.InteropServices.COMException"的未处理异常
An unhandled exception of type 'System.Runtime.InteropServices.COMException' occurred in pdf1.exe
附加信息:此文件名不正确.
Additional information: This filename is incorrect.
尝试以下一项或多项:
探测曲目以确保其输入正确.
Probe the track to make sure it is typed correctly.
从文件和文件夹列表中选择一个文件.
Select a file from the list of files and folders.
这是代码:
' first we are creating application of word.
Dim WordApp As New Microsoft.Office.Interop.Word.Application()
' now creating new document.
WordApp.Documents.Add()
' see word file behind your program
WordApp.Visible = True
' get the reference of active document
Dim doc As Microsoft.Office.Interop.Word.Document = WordApp.ActiveDocument
' set openfiledialog to select multiple image files
Dim ofd As New OpenFileDialog()
ofd.Filter = "Images (*.BMP;*.JPG;*.GIF)|*.BMP;*.JPG;*.GIF"
ofd.Title = "Select Image To Insert...."
ofd.Multiselect = True
' if user select OK, then process for adding images
If ofd.ShowDialog() = DialogResult.OK Then
' iterating process for adding all images which is selected by filedialog
For Each filename As String In ofd.FileNames
' now add the picture in active document reference
doc.InlineShapes.AddPicture(filename, Type.Missing, Type.Missing, Type.Missing)
Next
End If
' file is saved.
doc.SaveAs("E:\Doc8.docx", Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing)
' application is now quit.
WordApp.Quit(Type.Missing, Type.Missing, Type.Missing)
推荐答案
困难的方法: 似乎如果您在文件名中提供文件扩展名,那么您必须 指定文件格式,文件扩展名和文件格式的扩展名必须匹配:
The hard way: It seems that if you give a file extension in the filename then you have to specify the fileformat, and the file extension and the extension for the fileformat must match:
Option Infer On
Option Strict On
Imports Microsoft.Office.Interop
Public Class Form1
Private Sub ProcessWordDocument()
Dim WordApp As New Word.Application()
WordApp.Documents.Add()
WordApp.Visible = True
Dim doc = WordApp.ActiveDocument
Using ofd As New OpenFileDialog()
ofd.Filter = "Images (*.BMP;*.JPG;*.GIF)|*.BMP;*.JPG;*.GIF"
ofd.Title = "Select Image To Insert...."
ofd.Multiselect = True
If ofd.ShowDialog() = DialogResult.OK Then
For Each filename As String In ofd.FileNames
doc.InlineShapes.AddPicture(filename, Type.Missing, Type.Missing, Type.Missing)
Next
End If
End Using
doc.SaveAs("C:\temp\Doc8.docx", FileFormat:=Word.WdSaveFormat.wdFormatDocumentDefault)
doc.Close()
WordApp.Quit()
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
ProcessWordDocument()
' ensure instance of Word used in ProcessWordDocument is disposed of...
GC.Collect()
GC.WaitForPendingFinalizers()
GC.Collect()
GC.WaitForPendingFinalizers()
End Sub
End Class
该代码中需要考虑的其他项目
Other items to consider in that code
- 我使用了 命名参数格式;它只是碰巧使它所指的内容更加明显.
- OpenFileDialog 的实例应该调用
.Dispose()
.Using
结构会为您解决这个问题. - 您应该确保正确处理 Word 实例 - 我使用了处理 Excel com 的正确方法中显示的模式使用 VB.NET 的对象?为此.
- I used a named parameter for the format; it just happened to make it more obvious what it refers to.
- The instance of OpenFileDialog should have
.Dispose()
called on it. TheUsing
construct takes care of that for you. - You should make sure that the instance of Word is disposed of properly - I used the pattern shown in The proper way to dispose Excel com object using VB.NET? for that.
简单的方法:另一种方法是省略扩展名,让 Word 为您选择:
The easy way: The alternative is to omit the extension and let Word choose it for you:
doc.SaveAs("C:\temp\Doc8")
导致保存文件C:\temp\Doc8.docx".但是您仍然需要使用Using
并确保正确处理Word实例,如上所示.
results in a file "C:\temp\Doc8.docx" being saved. But you still need to use Using
and make sure that the Word instance is disposed of properly, as shown above.
这篇关于保存 Word 文档 - 错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!