邮件合并没有打开文档。 [英] Mail Merge without opening doc.

查看:75
本文介绍了邮件合并没有打开文档。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,


我正在尝试使用C#进行邮件合并。我已经准备好了所有模板的docx。我有一个带有数据的csv文件。



我打开文件 


  oDataDoc = wrdApp.Documents.Open(ref oName,ref oMissing,

             ref oMissing,ref oMissing,ref oMissing,ref oMissing,

            ref oMissing,ref oMissing,ref oMissing,ref oMissing,

             ref oFalse,ref oMissing,ref oMissing,ref oMissing,

             ref oMissing);


第一个问题是这实际上是使用Word应用程序在用户计算机上打开该文件。我宁愿不这样做而只是"打开编辑"。我的代码里面。我应该做一个不同的公开电话吗?


我的邮件合并就像 


  wrdSelection = wrdApp.Selection;

  wrdMailMerge = oDataDoc.MailMerge;

  wrdMailMerge.Destination = Microsoft.Office.Interop.Word.WdMailMergeDestination.wdSendToNewDocument;

  wrdMailMerge.Execute(ref oFalse);



这会生成一个也在用户计算机上打开的新文件。原始docx和csv都保存在服务器上,因此我想要做的是将新创建的doc保存在同一位置,然后将其下载到用户计算机。相反,
文件在用户计算机上打开,我不知道如何将其保存到服务器位置。


我一直在谷歌搜索但是还没有找到正确的答案。我们将不胜感激。


谢谢。


 




解决方案

在用户的机器上打开Word文档是您最不担心的事情;一旦你的代码在任何地方打开它,它会在等待用户响应mailmerge SQL提示时停止。在VBA中,将按以下方式处理:

 Sub RunMerge()
Dim wdApp As New Word.Application,wdDoc As Word。 Document,i As Long
Dim strWkBkNm As String,StrFldr As String,StrNm As String,j As Long
'非法文件名字符
Const StrNoChr As String =""" *。 ?/ \:|"
strWkBkNm = ThisWorkbook.FullName:StrFldr = ThisWorkbook.Path& " \"
使用wdApp
'Visible = True用于调试目的
.Visible = False
'禁用警报以防止SQL提示
.DisplayAlerts = wdAlertsNone
'打开mailmerge主文档
设置wdDoc = .Documents.Open(StrFldr&" Mail Merge Main Document.docx",_
ConfirmConversions:= False,ReadOnly:= True,AddToRecentFiles:= False)
使用wdDoc
使用.MailMerge
'定义mailmerge类型
.MainDocumentType = wdFormLetters
'定义输出
.Destination = wdSendToNewDocument
'对于超过255个字符
的SQL语句,连接到数据源,在'SQLStatement'行和
''SQLStatement1'行上应用所需的SQL语句,
'包括过滤器。 OpenDataSource名称:= strWkBkNm,ReadOnly:= True,_
AddToRecentFiles:= False,LinkToSource:= False,_
Connection:=" Provider = Microsoft.AC E.OLEDB.12.0;" &安培; _
" User ID = Admin; Data Source = StrWkBkNm;" &安培; _
" Mode = Read; Extended Properties ="" HDR = YES; IMEX = 1"" ;;",_
SQLStatement:=" SELECT * FROM`Pheet1

`",_
SQLStatement1:="",SubType:= wdMergeSubTypeAccess
.SuppressBlankLines = True
'处理每条记录
For i = 1到10'.DataSource.RecordCount
使用.DataSource
.FirstRecord = i
.LastRecord = i
.ActiveRecord = i
'获取输出文件名
StrNm = .DataFields(" Last_Name")& " _" &安培; .DataFields(" First_Name")
End with
'从文件名中删除指定的非法字符
for j = 1 To Len(StrNoChr)
StrNm = Replace(StrNm, Mid(StrNoChr,j,1)," _")
Next
StrNm = Trim(StrNm)
'为此记录创建mailmerge输出
。执行暂停:= False
'输出文档将自动成为'活动'一个
使用wdApp.ActiveDocument
.SaveAs Filename:= StrFldr& StrNm& " .docx",FileFormat:= wdFormatXMLDocument,AddToRecentFiles:= False
'和/或:
.SaveAs Filename:= StrFldr& StrNm& " .pdf",FileFormat:= wdFormatPDF,AddToRecentFiles:= False
'关闭保存的输出文件
。关闭SaveChanges:= False
结束
下一页i
'断开与数据源的连接
.MainDocumentType = wdNotAMergeDocument
End with
'关闭mailmerge主文档
。关闭SaveChanges:= False
以$ b $结尾b'恢复单词警报
.DisplayAlerts = wdAlertsAll
'退出Word
.Quit
结束
设置wdDoc =无:设置wdApp =无
End Sub

上面的代码启动一个新的Word实例,然后通过'.Visible = False'将其隐藏起来。该代码还使用'.DisplayAlerts = wdAlertsNone'来禁止mailmerge SQL提示,但这也会断开文档与数据源的连接。因此,必须使用相关的SQLStatement重新建立
连接(在上面的示例中,从运行宏的工作簿中获取Excel工作表中的所有记录)。如上所述,合并为每个处理的记录输出单独的文档和/或pdf,
以假定的'Last_Name'和&源代码中的"First_Name"数据字段。


我将留给您进行所需代码部分的C#转换。


Hey,

I'm attempting to use C# to do a mail merge. I've got a docx with all the templating ready to go. I've got a csv file with the data.

I open the file with 

  oDataDoc = wrdApp.Documents.Open(ref oName, ref oMissing,
                        ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                        ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                        ref oFalse, ref oMissing, ref oMissing, ref oMissing,
                        ref oMissing);

The first problem is that this actually opens the file on the users computer using their Word application. I'd rather not do that and only "open for editing" inside of my code. Is there a different open call I should be making?

I do my mail merge like 

 wrdSelection = wrdApp.Selection;
 wrdMailMerge = oDataDoc.MailMerge;
 wrdMailMerge.Destination = Microsoft.Office.Interop.Word.WdMailMergeDestination.wdSendToNewDocument;
 wrdMailMerge.Execute(ref oFalse);

This generates a new file that is also opened on the users machine. The original docx as well as the csv are both saved on a server so what I'd like to do is save the newly created doc in the same location then download it to the users machine. Instead the file is opening on the users machine to begin with, and I don't know how to save it to the server location.

I've been googling this but haven't found the right answer yet. Any insight would be appreciated.

Thank you.

 


解决方案

Opening the Word document on the user's machine is the least of your worries; once your code opens it anywhere it will stall while waiting for the user to respond to the mailmerge SQL prompt. In VBA that would be handled along the lines of:

Sub RunMerge()
Dim wdApp As New Word.Application, wdDoc As Word.Document, i As Long
Dim strWkBkNm As String, StrFldr As String, StrNm As String, j As Long
'Illegal filename characters
Const StrNoChr As String = """*./\:?|"
strWkBkNm = ThisWorkbook.FullName: StrFldr = ThisWorkbook.Path & "\"
With wdApp
  'Visible = True for debugging purposes
  .Visible = False
  'Disable alerts to prevent an SQL prompt
  .DisplayAlerts = wdAlertsNone
  'Open the mailmerge main document
  Set wdDoc = .Documents.Open(StrFldr & "Mail Merge Main Document.docx", _
    ConfirmConversions:=False, ReadOnly:=True, AddToRecentFiles:=False)
  With wdDoc
    With .MailMerge
      'Define the mailmerge type
      .MainDocumentType = wdFormLetters
      'Define the output
      .Destination = wdSendToNewDocument
      'Connect to the data source, Applying the required SQL Statement,
      'including filters, on the 'SQLStatement' line and the
      ''SQLStatement1' line, for SQL Statements over 255 characters
      .OpenDataSource Name:=strWkBkNm, ReadOnly:=True, _
        AddToRecentFiles:=False, LinkToSource:=False, _
        Connection:="Provider=Microsoft.ACE.OLEDB.12.0;" & _
        "User ID=Admin;Data Source=StrWkBkNm;" & _
        "Mode=Read;Extended Properties=""HDR=YES;IMEX=1"";", _
        SQLStatement:="SELECT * FROM `Sheet1


`", _ SQLStatement1:="", SubType:=wdMergeSubTypeAccess .SuppressBlankLines = True 'Process each record For i = 1 To 10 '.DataSource.RecordCount With .DataSource .FirstRecord = i .LastRecord = i .ActiveRecord = i 'Get the output filename StrNm = .DataFields("Last_Name") & "_" & .DataFields("First_Name") End With 'Remove specified illegal characters from the file name For j = 1 To Len(StrNoChr) StrNm = Replace(StrNm, Mid(StrNoChr, j, 1), "_") Next StrNm = Trim(StrNm) 'Create the mailmerge output for this record .Execute Pause:=False 'The output document will automatically be the 'active' one With wdApp.ActiveDocument .SaveAs Filename:=StrFldr & StrNm & ".docx", FileFormat:=wdFormatXMLDocument, AddToRecentFiles:=False ' and/or: .SaveAs Filename:=StrFldr & StrNm & ".pdf", FileFormat:=wdFormatPDF, AddToRecentFiles:=False 'Close the saved output file .Close SaveChanges:=False End With Next i 'Disconnect from the data source .MainDocumentType = wdNotAMergeDocument End With 'Close the mailmerge main document .Close SaveChanges:=False End With 'Restore the Word alerts .DisplayAlerts = wdAlertsAll 'Quit Word .Quit End With Set wdDoc = Nothing: Set wdApp = Nothing End Sub

The above code starts a new Word instance, then hides it from the user, via '.Visible = False'. The code also uses '.DisplayAlerts = wdAlertsNone' to suppress the mailmerge SQL prompt, but that also disconnects the document from the data source. Consequently, the connection must be re-established with the relevant SQLStatement (in the above case, getting all records from an Excel worksheet in the workbook the macro is run from). As written, the merge outputs a separate document and/or pdf for each record processed, named after the assumed 'Last_Name' & 'First_Name' datafields in the source.

I'll leave it to you to do the C# conversion of whatever parts of the code you need.


这篇关于邮件合并没有打开文档。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆