自动 Word 邮件合并未按预期工作
我遇到了一些邮件合并代码的问题,这些代码应该在我们的应用程序中生成信件。 我知道这段代码目前有点粗糙,但在我们整理它之前,我们正处于“让某些东西正常工作”阶段。
现在,这应该工作的方式,以及当我们手动执行时的工作方式,是我们有一个文件(fileOut 变量+“.template”),它是信件的模板。 我们打开该模板,合并它,然后将其保存为 fileOut 变量中的文件名。
但是,它所做的是将模板文件的副本保存到文件输出文件名,而不是合并的输出。
我已经搜索过,但我似乎正在用头撞砖墙。
datafile 是包含合并数据的数据文件。
使用相同的文件,如果您手动执行,则一切正常。
Public Function processFile(ByVal datafile As String, ByVal fileOut As String) As String
Dim ans As String = String.Empty
errorLog = "C:\Temp\Template_error.log"
If (File.Exists(datafile)) Then
Try
' Create an instance of Word and make it invisible.'
wrdApp = CreateObject("Word.Application")
wrdApp.Visible = False
' Add a new document.'
wrdDoc = wrdApp.Documents.Add(fileOut & ".template")
wrdDoc.Select()
Dim wrdSelection As Word.Selection
Dim wrdMailMerge As Word.MailMerge
wrdDoc.MailMerge.OpenDataSource(datafile)
wrdSelection = wrdApp.Selection()
wrdMailMerge = wrdDoc.MailMerge()
With wrdMailMerge
.Execute()
End With
wrdDoc.SaveAs(fileOut)
wrdApp.Quit(False)
' Release References.'
wrdSelection = Nothing
wrdMailMerge = Nothing
wrdDoc = Nothing
wrdApp = Nothing
ans = "Merged OK"
Call writeToLogFile(errorLog, "This worked, written to " & fileOut)
Catch ex As Exception
ans = "error : exception thrown " & ex.ToString
Call writeToLogFile(errorLog, ans)
End Try
Else
ans = "error ; unable to open Date File : " & datafile
If (logErrors) Then
Call writeToLogFile(errorLog, "The specified source csv file does not exist. Unable " & _
"to process it. Filename provided: " & datafile)
End If
End If
Return ans
End Function
I'm having a problem with some mail merge code that is supposed to produce letters within our application. I'm aware that this code is a bit rough at the moment, but we're in the "Get something working" phase before we tidy it up.
Now the way this is supposed to work, and the way it works when we do it manually, is we have a file (the fileOut variable + ".template") which is a template for the letter. We open that template, merge it, and then save it as the filename in the fileOut variable.
However, what it is doing is saving a copy of the template file to the fileout filename, instead of the output of the merge.
I've searched, and I seem to be banging my head against a brick wall.
datafile is the datafile that contains the merge data.
Using the same files, it all works if you do it manually.
Public Function processFile(ByVal datafile As String, ByVal fileOut As String) As String
Dim ans As String = String.Empty
errorLog = "C:\Temp\Template_error.log"
If (File.Exists(datafile)) Then
Try
' Create an instance of Word and make it invisible.'
wrdApp = CreateObject("Word.Application")
wrdApp.Visible = False
' Add a new document.'
wrdDoc = wrdApp.Documents.Add(fileOut & ".template")
wrdDoc.Select()
Dim wrdSelection As Word.Selection
Dim wrdMailMerge As Word.MailMerge
wrdDoc.MailMerge.OpenDataSource(datafile)
wrdSelection = wrdApp.Selection()
wrdMailMerge = wrdDoc.MailMerge()
With wrdMailMerge
.Execute()
End With
wrdDoc.SaveAs(fileOut)
wrdApp.Quit(False)
' Release References.'
wrdSelection = Nothing
wrdMailMerge = Nothing
wrdDoc = Nothing
wrdApp = Nothing
ans = "Merged OK"
Call writeToLogFile(errorLog, "This worked, written to " & fileOut)
Catch ex As Exception
ans = "error : exception thrown " & ex.ToString
Call writeToLogFile(errorLog, ans)
End Try
Else
ans = "error ; unable to open Date File : " & datafile
If (logErrors) Then
Call writeToLogFile(errorLog, "The specified source csv file does not exist. Unable " & _
"to process it. Filename provided: " & datafile)
End If
End If
Return ans
End Function
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我找到了解决方案。 当您合并文档时,它会创建一个新文档,其中包含合并结果。 下面的代码片段解释了。
I've found the solution. When you merge a document, it creates a new document with the merge results in it. Code fragment below explains.
我认为问题出在这一行:
所以 templateName 类似于“C:\My Template.doc”,对吗?
看起来您正在提供 Word 文档本身的文件路径,而不是链接到“.template”文档的数据源(Excel 或 CSV 文件、Access 表等)。
尝试:
I think the problem is this line:
So templateName is something like 'C:\My Template.doc', right?
It looks like you are supplying the file path of a the Word document itself instead of a data source (the Excel or CSV file, Access Table, etc.) linked to the ".template" document.
Try:
一种作弊方法是在手动执行您提到的所有步骤的同时录制宏。 完成后,调整宏使其更加灵活。
这就是我上次所做的,我在编写自己的宏时无法弄清楚:)
One way to cheat is to record a macro while manually doing all the steps you mention. Once you're done, adjust the macro to be more flexible.
That's what I did last time I couldn't figure it out while writing my own macro's :)