C# Snippets


DataSourceFieldsToListBox

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 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 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

© 2024 Better Solutions Limited. All Rights Reserved. © 2024 Better Solutions Limited Top