Vb.net 将Word文档添加到Word应用程序而不保存到文件系统
我试图在VB.NET中动态创建word文档,我发现关于所有这些的文档似乎非常稀少。现在,我的程序通过循环一个数据库表来工作,对于每一行,它都会提取变量。然后,程序循环并解析模板word文档,用数据库中的变量替换该模板中的变量,然后另存为新文档 相反,对于我要创建的每个“新文档”,我只想将它附加到另一个文档上,这样当需要解析表中的1000行时,我就不必创建1000个不同的word文档并将其保存到文件系统中 我似乎找不到这方面的任何东西。所有内容都提到合并文档(我实际上想附加,而不是合并),或者我可以附加(“插入”)文档的唯一方法是使用Vb.net 将Word文档添加到Word应用程序而不保存到文件系统,vb.net,ms-word,Vb.net,Ms Word,我试图在VB.NET中动态创建word文档,我发现关于所有这些的文档似乎非常稀少。现在,我的程序通过循环一个数据库表来工作,对于每一行,它都会提取变量。然后,程序循环并解析模板word文档,用数据库中的变量替换该模板中的变量,然后另存为新文档 相反,对于我要创建的每个“新文档”,我只想将它附加到另一个文档上,这样当需要解析表中的1000行时,我就不必创建1000个不同的word文档并将其保存到文件系统中 我似乎找不到这方面的任何东西。所有内容都提到合并文档(我实际上想附加,而不是合并),或者我可
WordApplication.Selection.InsertFile(newWordDocument.FullName)
。这是可行的,但需要在插入文件系统之前将newWordDocument
保存到文件系统中
有没有办法在内存中添加newWordDocument
到我的WordApplication
对象
这是我现在拥有的伪代码
For each row in TableOfVariables
Dim WordApplication as New Word.Application
Dim tempDoc as New Word.Document
tempDoc = WordApplication.Documents.Add
tempDoc = fillVariablesOfTheDocument(tempDoc, row)
tempDoc.Save() 'This is the problem - it saves as a new file rather than appending into WordApplication
Next
你没有做作业,只是跟我说一句话 objWordApp.Documents.Add(tempDoc)'这就是问题所在 在你做好准备之前,你不必保存它。这同样有效
Dim Word As Word.Application
Dim Doc As Word.Document
'Start Word and open the document template.
Word = CreateObject("Word.Application")
Word.Visible = True
Doc = Word.Documents.Add
从花了一段时间,但不知怎的,我发现了
邮件合并
,我不知道它的存在。长话短说,我可以有一个带变量的模板文档,也可以有一个输入excel文档。excel文档将有一个包含变量名称的标题行,excel文档的每个单元格将表示将进入文档的变量。当我执行MailMerge
时,模板将替换为n
页数(其中n
是excel行数)。这解决了将多个文档添加到一个大型excel文档的问题。我在我的excel文档中用1000行测试了这一点,它工作得非常完美
以下是我最终得到的代码:
Dim wrdSelection As Word.Selection
Dim wrdMailMerge As Word.MailMerge
Dim wrdMergeFields As Word.MailMergeFields
' Create an instance of Word and make it visible.
wrdAppMailMrg = CreateObject("Word.Application")
wrdAppMailMrg.Visible = True
' Add a new document.
wrdDocMailMrg = wrdAppMailMrg.Documents.Open("Template.docx")
wrdDocMailMrg.Select()
wrdSelection = wrdAppMailMrg.Selection()
wrdMailMerge = wrdDocMailMrg.MailMerge()
'Open Data Source
wrdDocMailMrg.MailMerge.OpenDataSource("InputVariables.xlsx", SQLStatement:="SELECT * FROM [Sheet1$]")
' Perform mail merge.
wrdMailMerge.Destination = _
Word.WdMailMergeDestination.wdSendToNewDocument
wrdMailMerge.Execute(False)
' Close the original form document.
wrdDocMailMrg.Saved = True
wrdDocMailMrg.Close(False)
' Release References.
wrdSelection = Nothing
wrdMailMerge = Nothing
wrdMergeFields = Nothing
wrdDocMailMrg = Nothing
wrdAppMailMrg = Nothing
现在我只需要调整它,使它运行得更快,并清理UI,但功能完全在那里。我不认为您在将每个页面作为一个单独的文档处理时对自己有任何好处,而这是您最终不希望看到的。为什么不创建内容的副本并将其附加到文档中(只有一个!)。这基本上就是我将“文档”放在内存中的意思。“为什么不创建内容副本并附加到文档中”正是我想做的,我只是看不到一种方法。我没有太多地使用Word自动化,但不应该像
ActiveDocument那样。选择Selection.Copy
Selection.GoTo什么:=wdGoToPage,which:=wdGoToLast
Selection.Paste
do?GrayFox374,很抱歉误解,我的代码中确实有那些声明,为了简洁起见,我在问题中遗漏了它们。我的代码似乎工作得很好(所有声明都正确),我只想知道如何在内存中声明Word.Document,并在填充数据后将其添加到Word.Application,这就是我要解决的问题。objWordApp.Documents.Add(tempDoc)是您所说的问题所在,tempDoc=objWordApp.Documents.Add是解决方案。填充文档并将其添加到应用程序中,或者先将文档添加到应用程序中,然后再填充文档,我没有遇到任何问题。我对我的问题进行了重大编辑,希望能让它更容易理解。
Dim wrdSelection As Word.Selection
Dim wrdMailMerge As Word.MailMerge
Dim wrdMergeFields As Word.MailMergeFields
' Create an instance of Word and make it visible.
wrdAppMailMrg = CreateObject("Word.Application")
wrdAppMailMrg.Visible = True
' Add a new document.
wrdDocMailMrg = wrdAppMailMrg.Documents.Open("Template.docx")
wrdDocMailMrg.Select()
wrdSelection = wrdAppMailMrg.Selection()
wrdMailMerge = wrdDocMailMrg.MailMerge()
'Open Data Source
wrdDocMailMrg.MailMerge.OpenDataSource("InputVariables.xlsx", SQLStatement:="SELECT * FROM [Sheet1$]")
' Perform mail merge.
wrdMailMerge.Destination = _
Word.WdMailMergeDestination.wdSendToNewDocument
wrdMailMerge.Execute(False)
' Close the original form document.
wrdDocMailMrg.Saved = True
wrdDocMailMrg.Close(False)
' Release References.
wrdSelection = Nothing
wrdMailMerge = Nothing
wrdMergeFields = Nothing
wrdDocMailMrg = Nothing
wrdAppMailMrg = Nothing