从另一个 Word 文档的副本创建 Word 文档时遇到格式问题

发布于 2024-09-25 18:32:14 字数 2472 浏览 8 评论 0原文

我支持的应用程序是通过将几个 Word 文档依次复制到一个文档中来创建合并的 Word 文档。

问题是合并文档中附加的文档的某些字段的格式发生了更改,而合并文档是 AppendDocument 的副本(想象一下,如果我们有一个文档要在合并文档中复制)文档)

Word 文档的第一行和第二行如下所示:

发件人: Mr x 收件人: Gary Y

地址: NorkYork 日期: 2010/05/01

发件人:收件人:地址:日期:AppendDocument 中为粗体,大小为 10,而在合并文档中,它们为粗体,但大小更改为 12!

我很困惑,不知道为什么这 4 个项目的尺寸会改变,即使它们的实际值具有相同的尺寸!

请参阅下面的代码,其中传递了 2 个文档路径。第一个是 BaseDocuemnt 或合并文档,第二个是附加文档。

Private Sub DocumentAppend(ByVal strBaseDocument As String, ByVal strAppendDocument As String)
    Dim FirstDocument As Boolean
    Dim fleBaseDocument As File
    Dim wrdRange As Word.Range
    Dim wrdAppendDocument As Word.DocumentClass
    wrdAppendDocument = New Word.DocumentClass()
    Dim AmalgamatedDocument As Word.DocumentClass
    AmalgamatedDocument = New Word.DocumentClass()
    Dim wrdApp As Word.ApplicationClass
    wrdApp = AmalgamatedDocument.Application
    Dim AmalgamatedDocumentRange As Word.Range
    Try
        wrdApp.Visible = True
        If fleBaseDocument.Exists(strBaseDocument) Then
            FirstDocument = False
            AmalgamatedDocument = wrdApp.Documents.Open(strBaseDocument)
        Else
            FirstDocument = True
            AmalgamatedDocument = wrdApp.Documents.Add()
        End If

        AmalgamatedDocumentRange = AmalgamatedDocument.Content
        AmalgamatedDocumentRange.Collapse(Word.WdCollapseDirection.wdCollapseEnd)
        If Not FirstDocument Then
            AmalgamatedDocumentRange.InsertBreak (Word.WdBreakType.wdSectionBreakNextPage)
        End If
        ''# get the document to be appended
        wrdAppendDocument = wrdApp.Documents.Open(strAppendDocument)
        wrdAppendDocument.Activate()
        wrdAppendDocument.Select()
        ''# +++++++++++++++++++++++
        wrdApp.Selection.Copy()
        wrdApp.Selection.CopyFormat()
        AmalgamatedDocument.Activate()
        wrdRange = AmalgamatedDocument.Content
        wrdRange.Collapse(Word.WdCollapseDirection.wdCollapseEnd)
        wrdRange.Paste()
        ''# New
        wrdApp.Selection.PasteFormat()
        ''# +++++++++++++++++++++++
        wrdAppendDocument.Close()
        ''# save the new document
        AmalgamatedDocument.SaveAs(FileName:=strBaseDocument)
        AmalgamatedDocument.Close()
End Sub

任何建议将不胜感激!

The application I support is creating an amalgamted Word document by copying couple of Word documents in one document right after each other.

The problem is the format of the some of the fields of the document that gets appended is changed in amalgamated document while the amalgamated document is the copy of AppendDocument (imagine if we have one document to copy in the amalgamated document)

The first and second line of of the Word document looks like:

From: Mr x To: Gary Y

Address: NorkYork Date: 2010/05/01

From:, To:, Address: and Date: are bold and size 10 in the AppendDocument while in amalgamated document they are bold but their size change to 12!

I confused I am not sure why the size for these 4 items are changed even their actual values have the same size!

Please see the below code which 2 documents path are passed. The first one is the BaseDocuemnt or amalgamated document and the second one is the document which is appended.

Private Sub DocumentAppend(ByVal strBaseDocument As String, ByVal strAppendDocument As String)
    Dim FirstDocument As Boolean
    Dim fleBaseDocument As File
    Dim wrdRange As Word.Range
    Dim wrdAppendDocument As Word.DocumentClass
    wrdAppendDocument = New Word.DocumentClass()
    Dim AmalgamatedDocument As Word.DocumentClass
    AmalgamatedDocument = New Word.DocumentClass()
    Dim wrdApp As Word.ApplicationClass
    wrdApp = AmalgamatedDocument.Application
    Dim AmalgamatedDocumentRange As Word.Range
    Try
        wrdApp.Visible = True
        If fleBaseDocument.Exists(strBaseDocument) Then
            FirstDocument = False
            AmalgamatedDocument = wrdApp.Documents.Open(strBaseDocument)
        Else
            FirstDocument = True
            AmalgamatedDocument = wrdApp.Documents.Add()
        End If

        AmalgamatedDocumentRange = AmalgamatedDocument.Content
        AmalgamatedDocumentRange.Collapse(Word.WdCollapseDirection.wdCollapseEnd)
        If Not FirstDocument Then
            AmalgamatedDocumentRange.InsertBreak (Word.WdBreakType.wdSectionBreakNextPage)
        End If
        ''# get the document to be appended
        wrdAppendDocument = wrdApp.Documents.Open(strAppendDocument)
        wrdAppendDocument.Activate()
        wrdAppendDocument.Select()
        ''# +++++++++++++++++++++++
        wrdApp.Selection.Copy()
        wrdApp.Selection.CopyFormat()
        AmalgamatedDocument.Activate()
        wrdRange = AmalgamatedDocument.Content
        wrdRange.Collapse(Word.WdCollapseDirection.wdCollapseEnd)
        wrdRange.Paste()
        ''# New
        wrdApp.Selection.PasteFormat()
        ''# +++++++++++++++++++++++
        wrdAppendDocument.Close()
        ''# save the new document
        AmalgamatedDocument.SaveAs(FileName:=strBaseDocument)
        AmalgamatedDocument.Close()
End Sub

Any advice would be greatly appreciated!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

谜兔 2024-10-02 18:32:14

首先,当您实际上不想操作 Word 中的活动屏幕文档时,我首先尝试避免使用 SELECTION 对象。

我还建议研究 Range.InsertFile 方法。基本上,您打开或创建“目标”文档,然后获取 CONTENT 的范围对象,将其折叠到末尾,最后调用 INSERTFILE 在该点插入文件。

像这样的东西

        dim rngend = Doc.Content
        rngend.Collapse(WdCollapseDirection.wdCollapseEnd)
        rngend.InsertFile(File, ConfirmConversions:=False, Link:=False, Attachment:=False)

通常会忠实地保留格式,尽管我遇到过一些不完全100%的情况

Well, first, I'd start by trying to avoid use of the SELECTION object when you're not actually wanting to manipulate the active onscreen document in Word.

I'd also suggest looking into the Range.InsertFile method. Basically, you open or create your "target" document, then obtain a range object of the CONTENT, collapse it to the end, and finally invoke the INSERTFILE to insert the file at that point.

Something like this

        dim rngend = Doc.Content
        rngend.Collapse(WdCollapseDirection.wdCollapseEnd)
        rngend.InsertFile(File, ConfirmConversions:=False, Link:=False, Attachment:=False)

That will usually preserve formatting faithfully, though I've run into some off situations where it's not quite 100%

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文