如何在Visual Studio中使用邮件合并步骤向导?

问题描述 投票:-1回答:1

我已经有一个带有模板和数据库的Word文档,所有字段都在那里,我只想按“ID1”过滤来创建字母并将它们保存在.pdf中

我正在过滤它们,因为我需要.pdf文件用于具有不同名称的不同部分,所以我想自动化多个过滤器“从ID1 = x到ID1 = y”并保存每个部分的.pdf文件

我正在使用Visual Studio C#

这是我的代码,我只是打开单词模板:

using System;  
using System.Collections.Generic;  
using System.Linq;  
using System.Text;  
using System.Threading.Tasks;
using Microsoft.Office.Interop.Word;

namespace MailMerge
{
    class Program 
    {
        static void Main(string[] args) 
        {
            var application = new Microsoft.Office.Interop.Word.Application();
            var document = new Microsoft.Office.Interop.Word.Document();

            document = application.Documents.Add(Template: @"C:/docPath.docx");
            application.Visible = true;

            Console.Write("Press Enter");
            Console.ReadLine();

            document.Close();
        }
    }
}

enter image description here

Mail merge step by step wizard IMAGE

c# visual-studio ms-word
1个回答
0
投票

你没有按照你提议的方式这样做;相反,您提供过滤器作为MailMerge.OpenDataSource方法的SQLStatement参数的一部分。如果手动配置包含所有必需参数的mailmerge主文档,则以下Word宏将告诉您需要将哪些内容合并到代码中:

Sub MMTest()
With ActiveDocument.MailMerge
  If .MainDocumentType <> wdNotAMergeDocument Then
    MsgBox "Mail Merge Data Source Name:" & vbCr & .DataSource.Name
    MsgBox "Mail Merge Connect String:" & vbCr & .DataSource.ConnectString
    MsgBox "Mail Merge Query String:" & vbCr & .DataSource.QueryString
  Else
    MsgBox "Not A Merge Document"
  End If
End With
End Sub
© www.soinside.com 2019 - 2024. All rights reserved.