【发布时间】:2017-06-01 13:53:07
【问题描述】:
使用 Word 和 VB.net 自动化构建一个 Word 文档,该文档由多个附加在一起的文档组成。
所以我执行 Word.Documents.Add(firstDocument),然后转到文件末尾并执行 Word.Selection.InsertFile(secondDocument),它按预期工作,除非 firstDocument 是例如 Verdana 10pt 和secondDocument 是 Calibiri 11,它使用 firstDocument 的字体和样式插入 secondDocument。
我在 Selection.InsertFile 的文档中找不到任何会对格式产生任何影响的内容,因此我认为必须以另一种方式对其进行控制。我还尝试在插入文件之前插入分页符和分节符(带分页符),但发现它对字体没有影响。
目前我有应用程序从 secondDocument 复制所有内容,关闭 secondDocument,打开 firstDocument,移动到结尾然后粘贴。由于许多原因,这是有问题的,但它保留了格式。
有什么想法可以在插入 firstDocument 时保持 secondDocument 的精确格式?我需要摆脱使用剪贴板!
谢谢!
编辑:这是我一直在测试的代码:
Public Sub TestingWord()
Dim thisApp As New Word.Application
Dim SourceDoc As New Word.Document
Dim DestDoc As New Word.Document
Try
thisApp.Visible = False
DestDoc = thisApp.Documents.Add("X:\Isaac\First.docx")
thisApp.Selection.WholeStory()
thisApp.Selection.EndKey(Unit:=6)
thisApp.Selection.InsertBreak(Word.WdBreakType.wdSectionBreakNextPage)
thisApp.Selection.InsertFile("X:\Isaac\Second.docx")
thisApp.Selection.WholeStory()
thisApp.Selection.EndKey(Unit:=6)
thisApp.Selection.InsertBreak(Word.WdBreakType.wdSectionBreakNextPage)
thisApp.Selection.InsertFile("X:\Isaac\Third.docx")
DestDoc.SaveAs2("X:\Isaac\Yo.docx")
thisApp.Quit(SaveChanges:=Word.WdSaveOptions.wdSaveChanges)
releaseObject(DestDoc)
releaseObject(SourceDoc)
releaseObject(thisApp)
Catch ex As Exception
MsgBox("Error: " & ex.Message.ToString)
Finally
MsgBox("Success!")
End Try
End Sub
【问题讨论】:
-
好像可以复制粘贴文档,保留原格式stackoverflow.com/questions/32472020/…
-
是的,我可以复制和粘贴,它可以工作,但正如我在原帖中所说,出于多种原因,我不得不放弃使用剪贴板。