Hi dnnyobi,
Refer below code i have checked only three condition you can check how many condition you want.
HTML
<asp:TextBox runat="server" ID="txtName" />
Namespaces
C#
using System.IO;
VB.Net
Imports System.IO
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
string[] allRTFDocument = Directory.GetFiles(Server.MapPath("~/Files"), "*.rtf");
Merge(allRTFDocument, Server.MapPath("~/Folder/Final.rtf"), true, txtName.Text);
}
}
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
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
If Not Me.IsPostBack Then
Dim allRTFDocument As String() = Directory.GetFiles(Server.MapPath("~/Files"), "*.rtf")
Merge(allRTFDocument, Server.MapPath("~/Folder/Final.rtf"), True, txtName.Text)
End If
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