[x]
Posted via EE Mobile

Search, ask, and monitor your questions on the go with EE Mobile. Visit Experts Exchange from your mobile device and never be out of touch again.

Question
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

9.1

Reg mail merge for ms-word using vb.net

Asked by MSBCKumar in Microsoft Visual Interdev, Microsoft Word, Microsoft Visual Basic.Net

Tags: Office, VBA, .net

I want mail merge for MS word in single document.
I have done it but it gives me different document for each template.
My code is written as below.
Please any one can help me for the same.
Thanks in advance.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
163:
164:
165:
166:
167:
168:
169:
170:
171:
172:
173:
174:
175:
176:
177:
178:
179:
180:
181:
182:
183:
184:
185:
186:
187:
188:
189:
190:
191:
192:
193:
194:
195:
196:
197:
198:
199:
200:
201:
202:
203:
204:
205:
206:
207:
208:
209:
210:
211:
212:
213:
214:
215:
216:
217:
218:
219:
220:
221:
222:
223:
224:
225:
226:
227:
228:
229:
230:
231:
Imports Microsoft.Office.Interop
Imports Microsoft.Office.Interop.Word
 
Dim wrdApp As word.Application
Dim wrdDoc As word._Document
 
Private Sub tlbGenLetter_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tlbGenLetter.Click
        Try
            If dgvClient.RowCount > 0 Then
                If dgvClient.CurrentRow Is Nothing Then Exit Sub
                dgvClient.EndEdit()
                Dim i As Integer
                For i = 0 To dgvClient.RowCount - 1
                    If dgvClient.Rows(i).Cells("ChkSelect").Value = True Then
                        strQuery = ""
                        strQuery = "Select CTD_Code,CTD_ClientID,CTD_ClientNm,CTD_Partner,ContactTitle,ContactInitial,ContactFName,ContactLName,Address1,Address2,Address3 from ClientTypeDetail where CTD_ClientID='" & dgvClient.Rows(i).Cells("CTD_ClientID").Value & "'"
                        Dim dt As New System.Data.DataTable
                        dt.Clear()
                        dt = objDAL.ExecuteDataTable(DAC.GEN.DACOConnectionString, strQuery, "ClientBankDetail")
                        If dt.Rows.Count > 0 Then
                            Dim moApp As Word.Application
                            Dim sTemplateFileName As String = "Clientnamechangeletter.doc"
                            moApp = CType(CreateObject("Word.Application"), Word.Application)
                            If IsNothing(moApp) = False Then
                                moApp.Visible = False
                                Dim sDocFileName As String
                                moApp.Documents.Open(Path.Combine(My.Application.Info.DirectoryPath & "\Letters", sTemplateFileName))
                                sDocFileName = dt.Rows(0)("CTD_Code") & ".doc"
 
                                Dim r As Microsoft.Office.Interop.Word.Range
                                Dim f As Microsoft.Office.Interop.Word.Field
                                Dim s As Microsoft.Office.Interop.Word.Shape
                                For Each r In moApp.ActiveDocument.StoryRanges
                                    If r.StoryType <> Word.WdStoryType.wdTextFrameStory Then
                                        For Each f In r.Fields
                                            If f.Type = Word.WdFieldType.wdFieldMergeField Then
                                                f.Select()
                                                f.UpdateSource()
                                                If Trim(f.Code.Text) = "MERGEFIELD  CTD_Code" Then
                                                    moApp.Selection.TypeText(dt.Rows(0)("CTD_Code").ToString())
                                                ElseIf Trim(f.Code.Text) = "MERGEFIELD  CTD_Partner" Then
                                                    If IsDBNull(dt.Rows(0)("CTD_Partner")) = True Then
                                                        moApp.Selection.TypeText(" ")
                                                    Else
                                                        moApp.Selection.TypeText(LCase(dt.Rows(0)("CTD_Partner").ToString()))
                                                    End If
                                                ElseIf Trim(f.Code.Text) = "MERGEFIELD  CTD_ClientNm" Then
                                                    moApp.Selection.TypeText(dt.Rows(0)("CTD_ClientNm").ToString())
                                                ElseIf Trim(f.Code.Text) = "MERGEFIELD  ContactTitle" Then
                                                    If IsDBNull(dt.Rows(0)("ContactTitle")) = True Then
                                                        moApp.Selection.TypeText(" ")
                                                    Else
                                                        If dt.Rows(0)("ContactTitle").ToString() = "" Then
                                                            moApp.Selection.TypeText(" ")
                                                        Else
                                                            moApp.Selection.TypeText(dt.Rows(0)("ContactTitle").ToString())
                                                        End If
                                                    End If
                                                ElseIf Trim(f.Code.Text) = "MERGEFIELD  ContactInitial" Then
                                                    If IsDBNull(dt.Rows(0)("ContactInitial")) = True Then
                                                        moApp.Selection.TypeText(" ")
                                                    Else
                                                        If dt.Rows(0)("ContactInitial").ToString() = "" Then
                                                            moApp.Selection.TypeText(" ")
                                                        Else
                                                            moApp.Selection.TypeText(dt.Rows(0)("ContactInitial").ToString())
                                                        End If
                                                    End If
                                                ElseIf Trim(f.Code.Text) = "MERGEFIELD  ContactFName" Then
                                                    If IsDBNull(dt.Rows(0)("ContactFName")) = True Then
                                                        moApp.Selection.TypeText(" ")
                                                    Else
                                                        If dt.Rows(0)("ContactFName").ToString() = "" Then
                                                            moApp.Selection.TypeText(" ")
                                                        Else
                                                            moApp.Selection.TypeText(dt.Rows(0)("ContactFName").ToString())
                                                        End If
                                                    End If
                                                ElseIf Trim(f.Code.Text) = "MERGEFIELD  ContactLName" Then
                                                    If IsDBNull(dt.Rows(0)("ContactLName")) = True Then
                                                        moApp.Selection.TypeText(" ")
                                                    Else
                                                        If dt.Rows(0)("ContactLName").ToString() = "" Then
                                                            moApp.Selection.TypeText(" ")
                                                        Else
                                                            moApp.Selection.TypeText(dt.Rows(0)("ContactLName").ToString())
                                                        End If
                                                    End If
                                                ElseIf Trim(f.Code.Text) = "MERGEFIELD  Address1" Then
                                                    If IsDBNull(dt.Rows(0)("Address1")) = True Then
                                                        moApp.Selection.TypeText(" ")
                                                    Else
                                                        If dt.Rows(0)("Address1").ToString() = "" Then
                                                            moApp.Selection.TypeText(" ")
                                                        Else
                                                            moApp.Selection.TypeText(dt.Rows(0)("Address1").ToString())
                                                        End If
                                                    End If
                                                ElseIf Trim(f.Code.Text) = "MERGEFIELD  Address2" Then
                                                    If IsDBNull(dt.Rows(0)("Address2")) = True Then
                                                        moApp.Selection.TypeText(" ")
                                                    Else
                                                        If dt.Rows(0)("Address2").ToString() = "" Then
                                                            moApp.Selection.TypeText(" ")
                                                        Else
                                                            moApp.Selection.TypeText(dt.Rows(0)("Address2").ToString())
                                                        End If
                                                    End If
                                                ElseIf Trim(f.Code.Text) = "MERGEFIELD  Address3" Then
                                                    If IsDBNull(dt.Rows(0)("Address3")) = True Then
                                                        moApp.Selection.TypeText(" ")
                                                    Else
                                                        If dt.Rows(0)("Address3").ToString() = "" Then
                                                            moApp.Selection.TypeText(" ")
                                                        Else
                                                            moApp.Selection.TypeText(dt.Rows(0)("Address3").ToString())
                                                        End If
                                                    End If
                                                ElseIf Trim(f.Code.Text) = "MERGEFIELD  letterdate" Then
                                                    moApp.Selection.TypeText(Date.Now.ToString("dd/MMM/yyyy"))
                                                End If
                                            End If
                                        Next
                                    End If
                                Next
                                For Each s In moApp.ActiveDocument.Shapes
                                    If s.Type = Microsoft.Office.Core.MsoShapeType.msoTextBox Then
                                        For Each f In s.TextFrame.TextRange.Fields
                                            If f.Type = Word.WdFieldType.wdFieldMergeField Then
                                                f.Select()
                                                f.UpdateSource()
                                                If Trim(f.Code.Text) = "MERGEFIELD  CTD_Code" Then
                                                    moApp.Selection.TypeText(dt.Rows(0)("CTD_Code").ToString())
                                                ElseIf Trim(f.Code.Text) = "MERGEFIELD  CTD_Partner" Then
                                                    If IsDBNull(dt.Rows(0)("CTD_Partner")) = True Then
                                                        moApp.Selection.TypeText(" ")
                                                    Else
                                                        moApp.Selection.TypeText(LCase(dt.Rows(0)("CTD_Partner").ToString()))
                                                    End If
                                                ElseIf Trim(f.Code.Text) = "MERGEFIELD  CTD_ClientNm" Then
                                                    moApp.Selection.TypeText(dt.Rows(0)("CTD_ClientNm").ToString())
                                                ElseIf Trim(f.Code.Text) = "MERGEFIELD  ContactTitle" Then
                                                    If IsDBNull(dt.Rows(0)("ContactTitle")) = True Then
                                                        moApp.Selection.TypeText(" ")
                                                    Else
                                                        If dt.Rows(0)("ContactTitle").ToString() = "" Then
                                                            moApp.Selection.TypeText(" ")
                                                        Else
                                                            moApp.Selection.TypeText(dt.Rows(0)("ContactTitle").ToString())
                                                        End If
                                                    End If
                                                ElseIf Trim(f.Code.Text) = "MERGEFIELD  ContactInitial" Then
                                                    If IsDBNull(dt.Rows(0)("ContactInitial")) = True Then
                                                        moApp.Selection.TypeText(" ")
                                                    Else
                                                        If dt.Rows(0)("ContactInitial").ToString() = "" Then
                                                            moApp.Selection.TypeText(" ")
                                                        Else
                                                            moApp.Selection.TypeText(dt.Rows(0)("ContactInitial").ToString())
                                                        End If
                                                    End If
                                                ElseIf Trim(f.Code.Text) = "MERGEFIELD  ContactFName" Then
                                                    If IsDBNull(dt.Rows(0)("ContactFName")) = True Then
                                                        moApp.Selection.TypeText(" ")
                                                    Else
                                                        If dt.Rows(0)("ContactFName").ToString() = "" Then
                                                            moApp.Selection.TypeText(" ")
                                                        Else
                                                            moApp.Selection.TypeText(dt.Rows(0)("ContactFName").ToString())
                                                        End If
                                                    End If
                                                ElseIf Trim(f.Code.Text) = "MERGEFIELD  ContactLName" Then
                                                    If IsDBNull(dt.Rows(0)("ContactLName")) = True Then
                                                        moApp.Selection.TypeText(" ")
                                                    Else
                                                        If dt.Rows(0)("ContactLName").ToString() = "" Then
                                                            moApp.Selection.TypeText(" ")
                                                        Else
                                                            moApp.Selection.TypeText(dt.Rows(0)("ContactLName").ToString())
                                                        End If
                                                    End If
                                                ElseIf Trim(f.Code.Text) = "MERGEFIELD  Address1" Then
                                                    If IsDBNull(dt.Rows(0)("Address1")) = True Then
                                                        moApp.Selection.TypeText(" ")
                                                    Else
                                                        If dt.Rows(0)("Address1").ToString() = "" Then
                                                            moApp.Selection.TypeText(" ")
                                                        Else
                                                            moApp.Selection.TypeText(dt.Rows(0)("Address1").ToString())
                                                        End If
                                                    End If
                                                ElseIf Trim(f.Code.Text) = "MERGEFIELD  Address2" Then
                                                    If IsDBNull(dt.Rows(0)("Address2")) = True Then
                                                        moApp.Selection.TypeText(" ")
                                                    Else
                                                        If dt.Rows(0)("Address2").ToString() = "" Then
                                                            moApp.Selection.TypeText(" ")
                                                        Else
                                                            moApp.Selection.TypeText(dt.Rows(0)("Address2").ToString())
                                                        End If
                                                    End If
                                                ElseIf Trim(f.Code.Text) = "MERGEFIELD  Address3" Then
                                                    If IsDBNull(dt.Rows(0)("Address3")) = True Then
                                                        moApp.Selection.TypeText(" ")
                                                    Else
                                                        If dt.Rows(0)("Address3").ToString() = "" Then
                                                            moApp.Selection.TypeText(" ")
                                                        Else
                                                            moApp.Selection.TypeText(dt.Rows(0)("Address3").ToString())
                                                        End If
                                                    End If
                                                ElseIf Trim(f.Code.Text) = "MERGEFIELD  letterdate" Then
                                                    moApp.Selection.TypeText(Date.Now.ToString("dd/MMM/yyyy"))
                                                End If
                                            End If
                                        Next
                                    End If
                                Next
                                moApp.ActiveDocument.SaveAs("C:\Paresh\" & sDocFileName)
                                moApp.Documents.Close()
                                moApp.Quit(Word.WdSaveOptions.wdDoNotSaveChanges)
                            End If
                            moApp = Nothing
                        End If
                    End If
                Next
            End If
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical, Me.Text)
        End Try
    End Sub
[+][-]08/08/09 07:41 AM, ID: 25050034Administrative Comment

Experts Exchange has a courteous staff of administrators who help members get the most out of the website by means of administrative comments like this one.

Start your 30-day free trial to view this Administrative Comment or ask the Experts your question.

 
[+][-]08/08/09 07:41 AM, ID: 25050035Administrative Comment

Experts Exchange has a courteous staff of administrators who help members get the most out of the website by means of administrative comments like this one.

Start your 30-day free trial to view this Administrative Comment or ask the Experts your question.

 
[+][-]08/09/09 03:11 AM, ID: 25053551Accepted Solution

View this solution now by starting your 30-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

About this solution

Zones: Microsoft Visual Interdev, Microsoft Word, Microsoft Visual Basic.Net
Tags: Office, VBA, .net
Sign Up Now!
Solution Provided By: GrahamSkan
Participating Experts: 1
Solution Grade: A
 
 
Loading Advertisement...
20091021-EE-VQP-81 - Hierarchy / EE_QW_3_20080625