C# Snippets
DataSourceFieldsToListBox
public static void DataSourceFieldsToListBox(System.Windows.Forms.ListBox lsbListBox, Word.MailMergeDataSource objDataSource)
{
int ifieldcount;
string sdatafields;
try
{
if (clsError.ErrorFlag() == true)
return;
sdatafields = "";
for (ifieldcount = 1; ifieldcount <= objDataSource.FieldNames.Count; ifieldcount++)
sdatafields = sdatafields + objDataSource.FieldNames(ifieldcount).Name + ",";
// remove the last comma
if (sdatafields.Length > 0)
sdatafields = sdatafields.Substring(0, sdatafields.Length - 1);
clsStr.ToListBox(sdatafields, lsbListBox, ",");
}
catch (Runtime.InteropServices.COMException objCOMException)
{
mobjCOMException = objCOMException;
}
catch (Exception objException)
{
mobjException = objException;
}
finally
{
if (gbDEBUG_WORD == true | ((!mobjCOMException == null) | (!mobjException == null)))
clsError.Handle("DataSourceFieldsToListBox", msCLASSNAME, "transfer the list of fields in the datasource to the listbox .", mobjCOMException, mobjException);
}
}
Public Shared Sub DataSourceFieldsToListBox(ByVal lsbListBox As System.Windows.Forms.ListBox, _
ByVal objDataSource As Word.MailMergeDataSource)
Dim ifieldcount As Integer
Dim sdatafields As String
Try
If clsError.ErrorFlag() = True Then Exit Sub
sdatafields = ""
For ifieldcount = 1 To objDataSource.FieldNames.Count
sdatafields = sdatafields & objDataSource.FieldNames(ifieldcount).Name & ","
Next
'remove the last comma
If sdatafields.Length > 0 Then
sdatafields = sdatafields.Substring(0, sdatafields.Length - 1)
End If
Call clsStr.ToListBox(sdatafields, lsbListBox, ",")
Catch objCOMException As System.Runtime.InteropServices.COMException
mobjCOMException = objCOMException
Catch objException As Exception
mobjException = objException
Finally
If gbDEBUG_WORD = True Or _
((Not mobjCOMException Is Nothing) Or (Not mobjException Is Nothing)) Then
Call clsError.Handle("DataSourceFieldsToListBox", msCLASSNAME, _
"transfer the list of fields in the datasource to the listbox .", _
mobjCOMException, mobjException)
End If
End Try
End Sub
DataSourceLinkFile
public static void DataSourceLinkFile(string sFolderPath, string sFileName = "", string sExtension = "", bool bLinkToSource = false, bool bAddToRecentFiles = false)
{
try
{
if (clsError.ErrorFlag() == true)
return;
string sfullpath;
sfullpath = sFolderPath + sFileName + sExtension;
{
var withBlock = gApplicationWord.ActiveDocument.MailMerge;
withBlock.OpenDataSource(Name: sfullpath, SQLStatement: "SELECT * FROM " + sfullpath, LinkToSource: (object)bLinkToSource, AddToRecentFiles: (object)bAddToRecentFiles);
}
}
catch (Runtime.InteropServices.COMException objCOMException)
{
mobjCOMException = objCOMException;
}
catch (Exception objException)
{
mobjException = objException;
}
finally
{
if (gbDEBUG_WORD == true | ((!mobjCOMException == null) | (!mobjException == null)))
clsError.Handle("DataSourceLinkFile", msCLASSNAME, "attach the following datasource file to this document." + gsCRLF + "'" + sFolderPath + sFileName + sExtension + "'.", mobjCOMException, mobjException);
}
}
Public Shared Sub DataSourceLinkFile(ByVal sFolderPath As String, _
Optional ByVal sFileName As String = "", _
Optional ByVal sExtension As String = "", _
Optional ByVal bLinkToSource As Boolean = False, _
Optional ByVal bAddToRecentFiles As Boolean = False)
Try
If clsError.ErrorFlag() = True Then Exit Sub
Dim sfullpath As String
sfullpath = sFolderPath & sFileName & sExtension
With gApplicationWord.ActiveDocument.MailMerge
.OpenDataSource(Name:=sfullpath, _
SQLStatement:="SELECT * FROM " & sfullpath, _
LinkToSource:=CType(bLinkToSource, Object), _
AddToRecentFiles:=CType(bAddToRecentFiles, Object))
End With
Catch objCOMException As System.Runtime.InteropServices.COMException
mobjCOMException = objCOMException
Catch objException As Exception
mobjException = objException
Finally
If gbDEBUG_WORD = True Or _
((Not mobjCOMException Is Nothing) Or (Not mobjException Is Nothing)) Then
Call clsError.Handle("DataSourceLinkFile", msCLASSNAME, _
"attach the following datasource file to this document." & _
gsCRLF & _
"'" & sFolderPath & sFileName & sExtension & "'.", _
mobjCOMException, mobjException)
End If
End Try
End Sub
MergeDocument
public static Word.Document MergeDocument()
{
try
{
if (clsError.ErrorFlag() == true)
return;
{
var withBlock = gApplicationWord.ActiveDocument.MailMerge;
withBlock.Destination = Word.WdMailMergeDestination.wdSendToNewDocument;
withBlock.SuppressBlankLines = true;
{
var withBlock1 = withBlock.DataSource;
withBlock1.FirstRecord = Word.WdMailMergeDefaultRecord.wdDefaultFirstRecord;
withBlock1.LastRecord = Word.WdMailMergeDefaultRecord.wdDefaultLastRecord;
}
withBlock.Execute(Pause: false);
}
MergeDocument = gApplicationWord.ActiveDocument;
}
catch (Runtime.InteropServices.COMException objCOMException)
{
mobjCOMException = objCOMException;
}
catch (Exception objException)
{
mobjException = objException;
}
finally
{
if (gbDEBUG_WORD == true | ((!mobjCOMException == null) | (!mobjException == null)))
clsError.Handle("MergeDocument", msCLASSNAME, "merge the attached datasource with this document.", mobjCOMException, mobjException);
}
}
Public Shared Function MergeDocument() As Word.Document
Try
If clsError.ErrorFlag() = True Then Exit Function
With gApplicationWord.ActiveDocument.MailMerge
.Destination = Word.WdMailMergeDestination.wdSendToNewDocument
.SuppressBlankLines = True
With .DataSource
.FirstRecord = Word.WdMailMergeDefaultRecord.wdDefaultFirstRecord
.LastRecord = Word.WdMailMergeDefaultRecord.wdDefaultLastRecord
End With
.Execute(Pause:=False)
End With
MergeDocument = gApplicationWord.ActiveDocument
Catch objCOMException As System.Runtime.InteropServices.COMException
mobjCOMException = objCOMException
Catch objException As Exception
mobjException = objException
Finally
If gbDEBUG_WORD = True Or _
((Not mobjCOMException Is Nothing) Or (Not mobjException Is Nothing)) Then
Call clsError.Handle("MergeDocument", msCLASSNAME, _
"merge the attached datasource with this document.", _
mobjCOMException, mobjException)
End If
End Try
End Function
© 2026 Better Solutions Limited. All Rights Reserved. © 2026 Better Solutions Limited Top