Replace a string with a Table in c#

Morning,

I need to replace a string with a table using ASPOSE.Word. I have found some samplr VB code on your blog but it is using the function ReplaceEvalutor which no longer exists. Do you have any sample code which replaces a string with a table for me to look at please.

Many thanks

Tim

Morning,

Many thanks for details.

with FindandInsertTable() I need to pass down an XDocument which contains the data for populating the table, would this be possible.

Hi Tim,

Thanks for your inquiry. I suggest you please read the article about ‘Find and Replace’ from here:
http://www.aspose.com/docs/display/wordsnet/Find+and+Replace+Overview

Hope this helps you. Please let us know if you have any more queries.

Morning,

I have read the article and sorry but non the wiser. Looking at the replace function it liks I can pass down a parameter. With VB Script the parameter was pass down and all I had to do was the following:

If Ptr1 > 0 then
NoRows = FldNo * Ptr1
RowPtr = 2
Ptr1 = Ptr1 - 1
NoSubs = FldNo - 1
Ptr2 = 0
Set oArrayList = CreateObject("System.Collections.ArrayList" )
For i = 0 to Ptr1
sDate = Table2(i,0)
sDay = Left(sDate,2)
sMonth = Mid(sDate,4,2)
sYear = Mid(sDate,7)
sDate = sYear & sMonth & sDay
oArrayList.Add sDate
Next
oArrayList.Sort
' oArrayList.Reverse()
Doc.Selection.Find.ClearFormatting
With Doc.Selection.Find
.Text = "/table/"
.Replacement.Text = ""
.Forward = True
.Wrap = 1
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
End With
Doc.Selection.Find.Execute
DummyVar = Doc.Selection.ConvertToTable(0,1,5,0)
With Doc.Selection.Tables(1)
If .Style <> "Table Grid" Then
.Style = "Table Grid"
End If
.ApplyStyleHeadingRows = True
.ApplyStyleLastRow = True
.ApplyStyleFirstColumn = True
.ApplyStyleLastColumn = True
End With
Set objTable = theWordDocument.Tables(3)
objTable.Cell(1, 1).Range.Text = "Date of Change"
objTable.Cell(1, 2).Range.Text = "Sub-account" & Chr(13) & Chr(10) & "Number"
objTable.Cell(1, 3).Range.Text = "Interest Rate"
objTable.Cell(1, 4).Range.Text = "Fixed or variable"
objTable.Cell(1, 5).Range.Text = "Sub Account" & Chr(13) & Chr(10) & "Monthly" & Chr(13) & Chr(10) & "Payment"
Doc.Selection.SelectRow
Doc.Selection.Font.Bold = 9999998
'
' Start to merge data
'
For Each strItem in oArrayList
If StoredDate <> strItem then
StoredDate = strItem
for i = 0 to Ptr1
sDate = Table2(i,0)
sDay = Left(sDate,2)
sMonth = Mid(sDate,4,2)
sYear = Mid(sDate,7)
sDate = sYear & sMonth & sDay
If strItem = sDate then
For ii = 0 to NoSubs
If Table2(i,4) = Table1(ii,2) then
Table1(ii,0) = Table2(i,3)
Table1(ii,1) = Table2(i,1)
Table1(ii,3) = Table2(i,2)
End if
Next
End if
Next
TotalCMS = 0
For iii = 0 to NoSubs
TotalCMS = TotalCMS + Table1(iii,0)
SubAccNo = Table1(iii,2)
IntRate = Table1(iii,1)
ProductType = Table1(iii,3)
CMS = Table1(iii,0)
FldNo = Instr(CMS,".")
If FldNo > 0 then
FldNo2 = Len(CMS) - FldNo
If FldNo2 > 1 then
' Do nothing
else
CMS = CMS & "0"
end if
else
CMS = CMS & ".00"
end if
FldNo = Instr(TotalCMS,".")
If FldNo > 0 then
FldNo2 = Len(TotalCMS) - FldNo
If FldNo2 > 1 then
' Do nothing
else
TotalCMS = TotalCMS & "0"
end if
else
TotalCMS = TotalCMS & ".00"
end if
FldNo = Instr(IntRate,".")
FldNo2 = Len(IntRate) - FldNo
If FldNo2 > 1 then
IntRate = IntRate & "%"
else
IntRate = IntRate & "0" & "%"
end if
If Len(SubAccNO) = 1 then
SubAccNo = "0" & SubAccNo
End If
CMS = chr(163) & CMS
'
' Process product details
'
Ptr3 = Instr(ProductType,"/")
If Ptr3 > 0 then
Ptr3 = Ptr3 - 2
PYear = Mid(ProductType,Ptr3,5)
PYear = Right(PYear,2) & Left(PYear,2)
Else
PYear = CYear
End if
Ptr3 = Instr(ProductType,"EX FIX")
If Ptr3 > 0 then
ProductType = "VARIABLE"
else
Ptr3 = Instr(ProductType,"FIX")
If Ptr3 > 0 then
ProductType = "FIXED "
Else
Ptr4 = 0
Ptr3 = Instr(ProductType,"STAFF")
Ptr4 = Ptr4 + Ptr3
Ptr3 = Instr(ProductType,"HELD OVER")
Ptr4 = Ptr4 + Ptr3
Ptr3 = Instr(ProductType,"HIP 0%")
Ptr4 = Ptr4 + Ptr3
If Ptr4 > 0 then
ProductType = "FIXED"
else
ProductType = "VARIABLE"
End if
End if
If SubAccNo = "98" then
ProductType = "FIXED"
IntRate = "0.00%"
else
Ptr3 = Instr(ProductType,"TRK")
Ptr4 = Instr(ProductType,"TRACKER")
If Ptr3 > 0 or Ptr4 > 0 then
ProductType = "VARIABLE"
else
PYear = PYear + 0
If PYear < CYear then
ProductType = "VARIABLE"
If ERC_AMOUNT > 0 then
ProductType = "FIXED"
End If
End if
End if
End If
End if
TotalCMS = chr(163) & TotalCMS
If iii = 0 then
Doc.Selection.InsertRowsBelow(1)
sDate = strItem
sYear = Left(sDate,4)
sMonth = Mid(sDate,5,2)
sDay = Mid(sDate,7)
sDate = sDay & "/" & sMonth & "/" & sYear
objTable.Cell(RowPtr, 1).Range.Text = sDate
objTable.Cell(RowPtr, 2).Range.Text = SubAccno
objTable.Cell(RowPtr, 3).Range.Text = IntRate
objTable.Cell(RowPtr, 4).Range.Text = ProductType
objTable.Cell(RowPtr, 5).Range.Text = CMS
Doc.Selection.SelectRow
Doc.Selection.Font.Bold = 9999998
else
Doc.Selection.InsertRowsBelow(1)
objTable.Cell(RowPtr, 2).Range.Text = SubAccno
objTable.Cell(RowPtr, 3).Range.Text = IntRate
objTable.Cell(RowPtr, 4).Range.Text = ProductType
objTable.Cell(RowPtr, 5).Range.Text = CMS
End if
RowPtr = RowPtr + 1
Next
Doc.Selection.InsertRowsBelow(1)
objTable.Cell(RowPtr, 4).Range.Text = "Total Monthly Payment"
objTable.Cell(RowPtr, 5).Range.Text = TotalCMS
Doc.Selection.SelectRow
Doc.Selection.Font.Bold = 9999998
RowPtr = RowPtr + 1
End if
Next
Set oArrayList = Nothing
End if

Is there some way I can do the same please.

Hi Tim,

Thanks for your inquiry. It would be great if you please share following detail here for our reference. We will then provide you more information about your query along with code.

  • Please attach your input Word document.
  • Please
    attach your expected output Word document showing the desired behavior. You can
    use Microsoft Word to create your target Word document. I will
    investigate as to how you are expecting your final document be generated
    like.

Morning,

Please find attached the following:

1.Before-Table-Insert.doc , template that has the string Dummy Table which we want to replace.

2. After-Table-Insert.doc, template which has the table inserted

3. Sample-Vb-Code.txt - Sample code file which we used to update template with table. the coomand wordprintprocessor.SetVariableValue() allows the code to read the value of the variable defined in the ().

The idea of the code is to insert a table at a particular place in the table. So the text Dummy Table could be a table or any patten I can search for and replace. I did try replacing the Dummy Table wit a one row table by the code inserted the new table at the toop of the document and not where the dummy table was.

Many thanks for you help

Tim

Morning,

Sorry but I think there has been a misunderstanding between us. From what I can see the code above is a VB version of the C# routine. The VB script I attached contained the original code used when a Windows service which used MS Word to perform a merge called it passing down the Word Document and data to allow the VB script to replace a string with a table. With the new service now making use of ASPOSE.Word the VB script has been replace with a DLL written in C#. The new DLL amongst other functions needs to be able to replicate the ability to replace a string with a table which is populated with the data passed down via the service in the form of an xdocument. The original code you provided did the job with the exception of not populating the table with the data from the xdocument. My question was is there a way whereby I can pass down the xdcoument to the FindAndInsertTable class to allow me to access the data and populate the table. Are you saying I need to make a use an IReplaceingCallBack interface to get the data inside the FindAndInsertTable?.

Mant thanks for your help

Tim

Hi Tim,

Thanks for sharing the detail. There is no API in Aspose.Words to read the XDocument. You need to write the code (.NET classes) inside FindAndInsertTable class which read the XDocument (xml nodes) and write table or text using DocumentBuilder class. Please check the highlighted code snippet in previous code example where you will write your code.

Please let us know if you have any more queries.

Morning,

Sorry for my lack of knowledge. I have the code to read the xdocument but what I dont have a method to allow me to pass the XDocument pointer to the class FindAndInsertTable

Currenlty my DLL is pased the xdocument like so

private static Stream PostDataMergeCleanUp(XDocument letterData, Stream wordDocument)

and I need to pass letterData to FindAndInsertTable. Is this possible please.

HI,

Many thanks for details, will have a play and get back to you.

Regards

Tim

Hi Tim,

Please feel free to ask if you have any question about Aspose.Words, we will be happy to help you.

Just to let you know that the program is now working correctly, inserting teh table in the right place and populating it with the data from the xdocument.

Many thanks for your help and effort.

Tim

Hi Tim,

Thanks for your feedback. Please let us know if you have any more queries.

Hi

The DLL works fine on my laptop, but when I compile it and deploy to another desktop it returns different values into the table. I put some debug code into the program and it appears that the retrival of the xml node is not returning the value correctly

sample code

the calling code

FindandInsertTable oFindAndInsertTable = new FindandInsertTable();

oFindAndInsertTable.xdocument = letterData;

asposeDocument.Range.Replace(new Regex("insert_table"), oFindAndInsertTable, false);

asposeDocument.UpdateFields();

The entry point

public class FindandInsertTable : IReplacingCallback

{

public System.Xml.Linq.XDocument xdocument = null;

//ReplaceAction IReplacingCallback.Replacing(ReplacingArgs e)

ReplaceAction IReplacingCallback.Replacing(ReplacingArgs e)

{

and code that returns the correct details when running on my laptop but not when I deploy it.

var liv_cur_int_rt2 = xdocument.Descendants("LIV_CUR_INT_RT2").FirstOrDefault();

var prod_narr_2 = xdocument.Descendants("PROD_NARR_2").FirstOrDefault();

var liv_cur_int_rt3 = xdocument.Descendants("LIV_CUR_INT_RT3").FirstOrDefault();

var prod_narr_3 = xdocument.Descendants("PROD_NARR_3").FirstOrDefault();

var liv_cur_int_rt4 = xdocument.Descendants("LIV_CUR_INT_RT4").FirstOrDefault();

var prod_narr_4 = xdocument.Descendants("PROD_NARR_4").FirstOrDefault();

Am i missing a libray or something. Both the desktop and my laptop are using the same version of .net and msxml. Any ideas please

Hi Tim,

Thanks for your inquiry. This issue seems not to be related to Aspose.Words. I suggest you please create a separate application which only reads the elements of XDocument to test this issue. Please make sure that you are using the same XML at both locations.

Hi

THe issue was not my DLL but the program calling it. Issue resolved many thanks again

Hi Tim,

Thanks for your feedback. Please feel free to ask if you have any question about Aspose.Words, we will be happy to help you.