0

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:

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.

and this is the code:

  ' 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)
Andrew Morton
  • 24,203
  • 9
  • 60
  • 84
Rabeea qabaha
  • 526
  • 8
  • 23

1 Answers1

0

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

  • 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. The Using 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.

The easy way: The alternative is to omit the extension and let Word choose it for you:

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

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.

Andrew Morton
  • 24,203
  • 9
  • 60
  • 84