Hi dnnyobi,
Refer below code.
C#
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnMerge_Click(object sender, EventArgs e)
{
string[] allRTFDocument = System.IO.Directory.GetFiles(AppDomain.CurrentDomain.BaseDirectory.Replace("bin\\Debug\\", "") + "Files", "*.rtf");
string[] characters = new string[] { "A", "B", "CH", "D", "E", "Ẹ", "F", "G", "GB", "GH", "GW", "H",
"I", "Ị", "J", "K", "KP", "KW", "L", "M", "N", "NW", "NY", "Ṇ",
"O", "Ọ", "P", "S", "Ṣ", "T", "U", "Ụ", "V", "W", "Y", "Z", "Ẓ" };
List<string> files = new List<string>();
foreach (string character in characters)
{
string chars = character.ToLower();
string[] matchFiles = Array.FindAll(allRTFDocument, c => System.IO.Path.GetFileName(c).ToLower().StartsWith(chars));
for (int i = 0; i < matchFiles.Length; i++)
{
files.Add(matchFiles[i]);
}
}
Merge(files.ToArray(), AppDomain.CurrentDomain.BaseDirectory.Replace("bin\\Debug\\", "") + "Folder" + @"\Final.rtf", true);
}
public static void Merge(string[] filesToMerge, string outputFilename, bool insertPageBreaks)
{
object missing = System.Type.Missing;
object pageBreak = Microsoft.Office.Interop.Word.WdBreakType.wdSectionBreakNextPage;
object outputFile = outputFilename;
Microsoft.Office.Interop.Word._Application wordApplication = new Microsoft.Office.Interop.Word.Application();
try
{
Microsoft.Office.Interop.Word.Document wordDocument = wordApplication.Documents
.Add(ref missing, ref missing, ref missing, ref missing);
Microsoft.Office.Interop.Word.Selection selection = wordApplication.Selection;
int documentCount = filesToMerge.Length;
int breakStop = 0;
foreach (string file in filesToMerge)
{
selection.TypeText("________________");
selection.TypeParagraph();
selection.TypeText(System.IO.Path.GetFileName(file).Substring(0, 1).ToUpper()
+ "-" + System.IO.Path.GetFileName(file).Substring(0, 1).ToLower());
selection.TypeParagraph();
selection.TypeText("________________");
selection.TypeParagraph();
breakStop++;
selection.InsertFile(file, ref missing, ref missing, ref missing, ref missing);
selection.TypeParagraph();
if (insertPageBreaks && breakStop != documentCount)
{
selection.InsertBreak(ref pageBreak);
}
}
wordDocument.SaveAs(ref outputFile, ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing);
wordDocument = null;
}
catch (Exception ex)
{
throw ex;
}
finally
{
wordApplication.Quit(ref missing, ref missing, ref missing);
}
}
}
VB.Net
Public Class Form1
Private Sub btnMerge_Click(sender As System.Object, e As System.EventArgs) Handles btnMerge.Click
Dim allRTFDocument As String() = System.IO.Directory.GetFiles(AppDomain.CurrentDomain.BaseDirectory.Replace("bin\Debug\", "") & "Files", "*.rtf")
Dim characters As String() = New String() {"A", "B", "CH", "D", "E", "Ẹ", "F", "G", "GB", "GH", "GW", "H",
"I", "Ị", "J", "K", "KP", "KW", "L", "M", "N", "NW", "NY", "Ṇ",
"O", "Ọ", "P", "S", "Ṣ", "T", "U", "Ụ", "V", "W", "Y", "Z", "Ẓ"}
Dim files As List(Of String) = New List(Of String)()
For Each character As String In characters
Dim chars As String = character.ToLower()
Dim matchFiles As String() = Array.FindAll(allRTFDocument, Function(c) System.IO.Path.GetFileName(c).ToLower().StartsWith(chars))
For i As Integer = 0 To matchFiles.Length - 1
files.Add(matchFiles(i))
Next
Next
Merge(files.ToArray(), AppDomain.CurrentDomain.BaseDirectory.Replace("bin\Debug\", "") & "Folder" & "\Final.rtf", True)
End Sub
Public Shared Sub Merge(ByVal filesToMerge As String(), ByVal outputFilename As String, ByVal insertPageBreaks As Boolean)
Dim missing As Object = System.Type.Missing
Dim pageBreak As Object = Microsoft.Office.Interop.Word.WdBreakType.wdSectionBreakNextPage
Dim outputFile As Object = outputFilename
Dim wordApplication As Microsoft.Office.Interop.Word._Application = New Microsoft.Office.Interop.Word.Application()
Try
Dim wordDocument As Microsoft.Office.Interop.Word.Document = wordApplication.Documents.Add(missing, missing, missing, missing)
Dim selection As Microsoft.Office.Interop.Word.Selection = wordApplication.Selection
Dim documentCount As Integer = filesToMerge.Length
Dim breakStop As Integer = 0
For Each file As String In filesToMerge
selection.TypeText("________________")
selection.TypeParagraph()
selection.TypeText(System.IO.Path.GetFileName(file).Substring(0, 1).ToUpper() _
+ "-" + System.IO.Path.GetFileName(file).Substring(0, 1).ToLower())
selection.TypeParagraph()
selection.TypeText("________________")
selection.TypeParagraph()
breakStop += 1
selection.InsertFile(file, missing, missing, missing, missing)
selection.TypeParagraph();
If insertPageBreaks AndAlso breakStop <> documentCount Then
selection.InsertBreak(pageBreak)
End If
Next
wordDocument.SaveAs(outputFile, missing, missing, missing, missing, missing, missing,
missing, missing, missing, missing, missing, missing, missing, missing, missing)
wordDocument = Nothing
Catch ex As Exception
Throw ex
Finally
wordApplication.Quit(missing, missing, missing)
End Try
End Sub
End Class