保存 Word 文档 - 错误 [英] Saving Word document - error

查看:30
本文介绍了保存 Word 文档 - 错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些代码可以在 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

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

简单的方法:另一种方法是省略扩展名,让 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆