简体   繁体   English

来自C#的word邮件合并打开多个实例

[英]mailmerge in word from c# opens multiple instances

I'm trying to automate mailmerge using a .net program. 我正在尝试使用.net程序自动进行邮件合并。 There's a one page letter in word document addressed to a particular person with a mailmerge field by name 'Person'. 在word文档中有一个一页的信,发给特定的人,其中有一个邮件合并字段,名称为“ Person”。 Our database has got multiple persons and each person name has to go into a copy of the letter. 我们的数据库有多个人,每个人的名字都必须写在信件的副本中。 We want the one-page letters to be concatenated one after the other. 我们希望将一页字母一个接一个地连接起来。 Currently this code tries to use two persons with - name1 & name2. 当前,此代码尝试使用两个人-名称1和名称2。 The code below opens a separate instance of word for every person name. 下面的代码为每个人的名字打开一个单独的单词实例。

        object oMissing = System.Reflection.Missing.Value;
        //CREATING OBJECTS OF WORD AND DOCUMENT
        Word.Application oWord = new Word.Application();
        Word.Document oWordDoc = new Word.Document("C:\\Test\\AddressTemplate.docx");


        //SETTING THE VISIBILITY TO TRUE
        oWord.Visible = true;
        //THE LOCATION OF THE TEMPLATE FILE ON THE MACHINE
        Object oTemplatePath = "C:\\Test\\AddressTemplate.docx";

        oWordDoc = oWord.Documents.Add(ref oTemplatePath, ref oMissing, ref oMissing, ref oMissing);

        foreach (Microsoft.Office.Interop.Word.Field field in oWordDoc.Fields)
        {
            if (field.Code.Text.Contains("Person"))
            {
                field.Select();
                oWord.Selection.TypeText(name1);
            }
        }

        oWordDoc = oWord.Documents.Add(ref oTemplatePath, ref oMissing, ref oMissing, ref oMissing);

        foreach (Microsoft.Office.Interop.Word.Field field in oWordDoc.Fields)
        {
            if (field.Code.Text.Contains("Person"))
            {
                field.Select();
                oWord.Selection.TypeText(name2);
            }
        }

Question: How can I change the code to open just one instance of word, fill in the mailmerge field and concatenate one letter at the end of the other? 问题:如何更改代码以仅打开单词的一个实例,填写mailmerge字段,然后在另一个字母的末尾连接一个字母?

If you call new Word.Application you're creating a new instance, if what you want is to create a new instance if there is none, but reuse one if there is already one open you can do the following: 如果调用新的Word.Application,则正在创建一个新实例,如果要创建一个新实例(如果没有),而要重用一个(如果已经打开),则可以执行以下操作:

        Application app;
        try
        {
            app = (Application)System.Runtime.InteropServices.Marshal.GetActiveObject("Word.Application");
        }
        catch
        {
            app = new Application();
        }

Based on your comment i think you actually only have 1 instance of word open, it just opens both documents in different Windows (but a single application). 根据您的评论,我认为您实际上只有1个word实例打开,它只是在不同的Windows中打开两个文档(但只有一个应用程序)。 Windows aren't the same as applications. Windows与应用程序不同。

If you want it all in a single window you could reuse the previous document or else close it before you open a new one 如果要在一个窗口中全部保存,则可以重用上一个文档,也可以在打开新文档之前将其关闭

Ronan's code above did the trick for me. 上面罗南的代码对我有用。 I was doing new Application() every time, adding WINWORD instances (I know, should've known better). 我每次都在做新的Application(),添加WINWORD实例(我知道,应该更了解)。 I speak both C# and VB, but here's the VB version I used in this project: 我说C#和VB,但这是我在该项目中使用的VB版本:

        ' Use same winword instance if there; if not, create new one
    Dim oApp As Application
    Try
        oApp = DirectCast(Runtime.InteropServices.Marshal.GetActiveObject("Word.Application"), Application)
    Catch ex As Exception
        ' Word not yet open; open new instance
        Try
            ' Ensure we have Word installed
            oApp = New Application()
        Catch eApp As Exception
            Throw New ApplicationException(String.Format("You must have Microsoft Word installed to run the Top Line report"))
        End Try
    End Try
    oApp.Visible = True

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM