2017-08-11 153 views
0

我有一些代码在Word文件中插入多个图像。一切都很好 直到我试图保存文件,于是它给出了这样的错误:保存Word文档 - 错误

An unhandled exception of type 'System.Runtime.InteropServices.COMException' occurred in pdf1.exe

Additional information: This filename is incorrect.

Try one or more of the following:

  • 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) 
+1

在VB中,我们不必包含'Type.Missing'。 – OneFineDay

回答

0

难的方法:看来,如果你给文件扩展名为必须指定文件格式,文件扩展名和扩展名为fileformat 必须为匹配:

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 

其他项目要考虑在该代码

最简单的办法:另一种方法是省略扩展名,并让Word中选择它为您提供:

doc.SaveAs("C:\temp\Doc8") 

所生成的文件 “C:\ TEMP \ Doc8.docx” 被保存。但是您仍然需要使用Using并确保Word实例正确处置,如上所示。