Results 1 to 6 of 6

Thread: Winword Mail Merge Using VB.net

Threaded View

  1. #1

    Thread Starter
    Learning .Net danasegarane's Avatar
    Join Date
    Aug 2004
    Location
    VBForums
    Posts
    5,853

    Winword Mail Merge Using VB.net

    This code helps to AutoMate WinWord Using Mail Merge

    Main Method



    vb Code:
    1. Imports System.Data
    2. Imports System.IO
    3. Imports System.Reflection
    4. Imports Microsoft.Office.Interop
    5. 'Mark a Reference to Microsoft word Object from Com Tab
    6. Module Module1
    7.  
    8.     Sub Main()
    9.  
    10.         Dim sqlQry As String = "SELECT TOP 2 ContactName,ContactTitle,Address from customers"
    11.         'Get the values from table
    12.         Dim dttable As DataTable = DatabaseOperation.DatabaseOperation(sqlQry, DataBaseQueryType.Datatable)
    13.         Dim sTemplateFileName As String = "DocumentTemplate.doc"
    14.         'Word merging
    15.         Dim moApp As Object
    16.  
    17.         moApp = CreateObject("Word.Application")
    18.         If IsNothing(moApp) = False Then
    19.             moApp.visible = False
    20.             Dim sDocFileName As String
    21.             For Each row As DataRow In dttable.Rows
    22.                 'Open the Document
    23.                 moApp.Documents.Open(Path.Combine(ApplicationPath, sTemplateFileName))
    24.                 sDocFileName = Convert.ToString(row(0)) & ".doc"
    25.                 Dim intCount As Integer = 0
    26.                 For Each MergeField As Word.MailMergeField In moApp.ActiveDocument.MailMerge.Fields
    27.                     'Select the text
    28.                     MergeField.Select()
    29.                     'Type the text
    30.                     moApp.Selection.TypeText(Convert.ToString(row(intCount)))
    31.                     intCount += 1
    32.                 Next
    33.                 'Save the document
    34.                 moApp.ActiveDocument.SaveAs(Path.Combine(ApplicationPath, sDocFileName))
    35.                 'Close the document
    36.                 moApp.Documents.Close()
    37.             Next
    38.             'Dont save the template changes
    39.             moApp.Quit(Word.WdSaveOptions.wdDoNotSaveChanges)
    40.             'Dispose
    41.         End If
    42.         moApp = Nothing
    43.         dttable.Dispose()
    44.         dttable = Nothing
    45.  
    46.     End Sub
    47.  
    48.     Private Function ApplicationPath() As String
    49.         Return _
    50.         Path.GetDirectoryName([Assembly].GetEntryAssembly().Location)
    51.     End Function
    52.  
    53. End Module

    Database Operations

    vb Code:
    1. Imports System.Data
    2. Imports System.Data.SqlClient
    3. Imports System.Text.StringBuilder
    4. Imports System.Configuration
    5. Module DatabaseOperation
    6.  
    7.  
    8.     Public Enum DataBaseQueryType
    9.         Datatable = 1
    10.  
    11.     End Enum
    12.  
    13.  
    14.  
    15.     Dim con As SqlConnection
    16.     Dim cmd As SqlCommand
    17.     Dim sConstring As String
    18.  
    19.     Private Sub OpenConnection()
    20.         Dim connectionString As String = _
    21.         "Integrated Security=SSPI;Persist Security Info=False;" + _
    22.         "Initial Catalog=Northwind;Data Source=localhost"
    23.         con = New SqlConnection(connectionString)
    24.         If con.State = ConnectionState.Closed Then
    25.             con.Open()
    26.         End If
    27.     End Sub
    28.  
    29.     Private Sub CloseConnection()
    30.         If con.State = ConnectionState.Open Then
    31.             con.Close()
    32.         End If
    33.     End Sub
    34.  
    35.     Public Function DatabaseOperation(ByVal qry As String, ByVal type As DataBaseQueryType) As Object
    36.         Dim objDB As New Object
    37.         Dim dt As DataTable
    38.         Dim da As SqlDataAdapter
    39.         If type = DataBaseQueryType.Datatable Then
    40.             OpenConnection()
    41.             Dim cmd As New SqlCommand(qry)
    42.             cmd.Connection = con
    43.             dt = New DataTable
    44.             da = New SqlDataAdapter(cmd)
    45.             da.Fill(dt)
    46.             objDB = dt
    47.             dt.Dispose()
    48.             CloseConnection()
    49.             Return objDB
    50.         End If
    51.  
    52.         Return objDB
    53.  
    54.     End Function
    Attached Files Attached Files
    Please mark you thread resolved using the Thread Tools as shown

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width