自动 Word 邮件合并未按预期工作

发布于 2024-07-07 00:13:48 字数 2054 浏览 19 评论 0原文

我遇到了一些邮件合并代码的问题,这些代码应该在我们的应用程序中生成信件。 我知道这段代码目前有点粗糙,但在我们整理它之前,我们正处于“让某些东西正常工作”阶段。

现在,这应该工作的方式,以及当我们手动执行时的工作方式,是我们有一个文件(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 技术交流群。

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

发布评论

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

评论(3

木有鱼丸 2024-07-14 00:13:48

我找到了解决方案。 当您合并文档时,它会创建一个新文档,其中包含合并结果。 下面的代码片段解释了。

wrdDoc = wrdApp.Documents.Add(TemplateFileName)
wrdDoc.Select()
Dim wrdSelection As Word.Selection
Dim wrdMailMerge As Word.MailMerge


wrdDoc.MailMerge.OpenDataSource(DataFileName)

wrdSelection = wrdApp.Selection()
wrdMailMerge = wrdDoc.MailMerge()
With wrdMailMerge
    .Execute()
End With

' This is the wrong thing to do. It just re-saves the template file you opened. '
'wrdDoc.SaveAs(OutputFileName) '

' The resulting merged document is actually stored '
' in the MailMerge object, so you have to save that '
wrdMailMerge.Application.ActiveDocument.SaveAs(OutputFileName)

wrdApp.Quit(False)

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.

wrdDoc = wrdApp.Documents.Add(TemplateFileName)
wrdDoc.Select()
Dim wrdSelection As Word.Selection
Dim wrdMailMerge As Word.MailMerge


wrdDoc.MailMerge.OpenDataSource(DataFileName)

wrdSelection = wrdApp.Selection()
wrdMailMerge = wrdDoc.MailMerge()
With wrdMailMerge
    .Execute()
End With

' This is the wrong thing to do. It just re-saves the template file you opened. '
'wrdDoc.SaveAs(OutputFileName) '

' The resulting merged document is actually stored '
' in the MailMerge object, so you have to save that '
wrdMailMerge.Application.ActiveDocument.SaveAs(OutputFileName)

wrdApp.Quit(False)
七堇年 2024-07-14 00:13:48

我认为问题出在这一行:

wrdDoc.MailMerge.OpenDataSource(templateName)

所以 templateName 类似于“C:\My Template.doc”,对吗?

看起来您正在提供 Word 文档本身的文件路径,而不是链接到“.template”文档的数据源(Excel 或 CSV 文件、Access 表等)。

尝试

data = "C:\My Data.xls"
wrdDoc.MailMerge.OpenDataSource(data)

I think the problem is this line:

wrdDoc.MailMerge.OpenDataSource(templateName)

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:

data = "C:\My Data.xls"
wrdDoc.MailMerge.OpenDataSource(data)
眼前雾蒙蒙 2024-07-14 00:13:48

一种作弊方法是在手动执行您提到的所有步骤的同时录制宏。 完成后,调整宏使其更加灵活。

这就是我上次所做的,我在编写自己的宏时无法弄清楚:)

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 :)

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