2017-08-11 16 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

難しい方法:あなたが与える場合と思われますファイル名のファイル拡張子であるにはにファイル形式を指定し、ファイル形式のファイル拡張子と拡張子はである必要があります。

012です私はフォーマットのため named parameterを使っていたコードで考慮すべきの
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 

その他の項目

  • 。それはそれがそれが何を指しているかをより明白にするために起こったばかりです。
  • OpenFileDialogのインスタンスには.Dispose()が呼び出されます。 Using構造がそれを処理します。
  • Wordのインスタンスが正しく処分されていることを確認する必要があります。そのためにはThe proper way to dispose Excel com object using VB.NET?のパターンを使用しました。

簡単な方法:代替は拡張子を省略し、Wordがあなたのためにそれを選ぶようにすることです:ファイル内

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

結果 "C:TEMP \ \ Doc8.docx" が保存されています。しかし、まだUsingを使用し、上記のようにWordインスタンスが適切に処理されていることを確認する必要があります。

関連する問題