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)