Hi dnnyobi,
Refer below sample code.
Namespaces
C#
using System.IO;
VB.Net
Imports System.IO
Code
C#
private void Form1_Load(object sender, EventArgs e)
{
try
{
string[] allRTFDocument = Directory.GetFiles(AppDomain.CurrentDomain.BaseDirectory.Replace("bin\\Debug\\", "") + "Files", "*.rtf");
Merge(allRTFDocument, AppDomain.CurrentDomain.BaseDirectory.Replace("bin\\Debug\\", "") + "Folder" + @"\Final.rtf", true, txtName.Text);
}
catch (Exception)
{
throw;
}
}
public static void Merge(string[] filesToMerge, string outputFilename, bool insertPageBreaks, string name)
{
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)
{
if (Path.GetFileName(file.ToLower()).StartsWith(name))
{
breakStop++;
selection.InsertFile(file, ref missing, ref missing, ref missing, ref missing);
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
Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
Try
Dim allRTFDocument As String() = Directory.GetFiles(AppDomain.CurrentDomain.BaseDirectory.Replace("bin\Debug\", "") & "Files", "*.rtf")
Merge(allRTFDocument, AppDomain.CurrentDomain.BaseDirectory.Replace("bin\Debug\", "") & "Folder" & "\Final.rtf", True, txtName.Text)
Catch __unusedException1__ As Exception
Throw
End Try
End Sub
Public Shared Sub Merge(ByVal filesToMerge As String(), ByVal outputFilename As String, ByVal insertPageBreaks As Boolean, ByVal name As String)
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
If Path.GetFileName(file.ToLower()).StartsWith(name) Then
breakStop += 1
selection.InsertFile(file, missing, missing, missing, missing)
If insertPageBreaks AndAlso breakStop <> documentCount Then
selection.InsertBreak(pageBreak)
End If
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