如何将VB6代码转换为C#以进行MailMerge操作?

时间:2013-11-12 22:14:18

标签: c# winforms vb6 vb6-migration mailmerge

我正在尝试将以下VB6代码转换为C#,但正如预期的那样,我遇到了一些问题。谁能帮我吗?这是我第一次尝试使用MailMerge文档进行编码。甚至有些帮助设置oWord对象也应该让我继续前进。通过ODBC连接查询400系统,我设法通过填充数据的C#Windows窗体应用程序获得ListView控件,现在我需要使用这些数据来执行MailMerge文档。

Dim oWD As Object
Dim oDoc As Object
oDoc.MailMerge.Execute

Set oWord = CreateObject("Word.application")
oWord.Visible = False
Set oDoc = oWord.Documents.Open(docpath & docname & ".doc")

 oWord.ActiveDocument.PrintOut Background:=False
 oWord.ActiveDocument.Saved = True
 oWord.ActiveDocument.Close
 oWord.Windows(1).Activate
 oWord.ActiveDocument.Saved = True
 oWord.ActiveDocument.Close
 oWord.Quit (wdDoNotSaveChanges)

Public Sub CommitGroup(ByVal FileSys As String)

   Set oWord = CreateObject("Word.application")
   oWord.Visible = False
   Set oDoc = oWord.Documents.Open(docpath & docname & ".doc")

   For i = 1 To counter

     oWord.ActiveDocument.MailMerge.DataSource.FirstRecord = i
     oWord.ActiveDocument.MailMerge.DataSource.LastRecord = i

     If FileSys = "NAI" Or FileSys = "NTM" Or FileSys = "PAI" Or FileSys = "PTM" Then
       idx1 = Format(oWord.ActiveDocument.MailMerge.DataSource.DataFields.Item(4).Value, "000000")
       idx2 = ""
       idx3 = ""
       FYIDocName = UCase(Mid(FileSys, 1, 1)) & "%" & idx1 & "%" & idx2 & "%" & idx3 & _
                   "%CORRESPONDENCE%OUTGOING - ACKNOWLEDGEMENT%" & Format(Now, "yyyy-MM-dd-hh.mm.ss") & "BATCH" & i
     Else
       idx1 = Format(oWord.ActiveDocument.MailMerge.DataSource.DataFields.Item(21).Value, "000000")
       idx2 = oWord.ActiveDocument.MailMerge.DataSource.DataFields.Item(4).Value
       idx3 = Format(oWord.ActiveDocument.MailMerge.DataSource.DataFields.Item(5).Value, "000000")
       FYIDocName = UCase(Mid(FileSys, 1, 1)) & "%" & idx1 & "%" & idx2 & "%" & idx3 & _
                   "%CORRESPONDENCE%OUTGOING - ACKNOWLEDGEMENT%" & Format(Now, "yyyy-MM-dd-hh.mm.ss") & "BATCH" & i
     End If
     oWord.ActiveDocument.MailMerge.Execute

     oWord.ActiveDocument.SaveAs FileName:="c:\temp\to fyi\" & Trim(FYIDocName) & "BATCH.doc"
     oWord.ActiveDocument.Close
     oWord.ActiveDocument.MailMerge.DataSource.ActiveRecord = wdNextDataSourceRecord
   Next


oWord.Quit (wdDoNotSaveChanges)

End Sub

0 个答案:

没有答案