Showing and hiding details in word document

Dear all,
I got a dataset which produces salary details of employees in an organization according to there grades.Now salary components are different as well as salary structure for different employees. For ex: for junior officer salary components like car maintenance and driver allowance are missing but for managers these components are present. So i fill the dataset dynamically as per the employees grade. But to show that data in a word document dynamically is seems to be impossible coz we usually merge the fields in the word document once and for all. So i want to have a sort of mechanism where in if the particular component is missing in the dataset then that component should not be displayed in the word document. So kindly help me to achieve this asap.Also find the attached salary structure in which i have to show only those components which are present in the dataset .
Thanks in Advance
Amit Dangre.

This message was posted using Page2Forum (attachment) from Technical Support - Aspose.Words for .NET and Java

Hi
Thanks for your request. I think that you can achieve this using MergeField event handler. For example see the following code and attached documents.

public void Test157()
{
    string[] names = { "option1", "option2", "option3", "option4" };
    // Row that contains empty data will be removed
    string[] values = { "val1", "val2", "", "val4" };
    // Open document
    Document doc = new Document(@"Test157\in.doc");
    // Add MergeField event handler
    doc.MailMerge.MergeField += new MergeFieldEventHandler(MailMerge_MergeField157);
    // Execute mail merge
    doc.MailMerge.Execute(names, values);
    // Save document
    doc.Save(@"Test157\out.doc");
}
void MailMerge_MergeField157(object sender, MergeFieldEventArgs e)
{
    // Check if value is empty
    if (string.IsNullOrEmpty(e.FieldValue.ToString()))
    {
        // Get parent row 
        Node parantRow = e.Field.Start.GetAncestor(typeof(Row));
        // Remove row
        parantRow.Remove();
    }
}

I hope this could help you.
Best regards.

please send me the equivalent vb code so i can put into my program .

Hi
Thanks for your request. Here is the same code in VB.

Public Sub Test157()
Dim names As String() = {"option1", "option2", "option3", "option4"}
'Row that contains empty data will be removed
Dim values As String() = {"val1", "val2", "", "val4"}
'Open document
Dim doc As Document = New Document("in.doc")
'Add MergeField event handler
AddHandler doc.MailMerge.MergeField, AddressOf MailMerge_MergeField157
'Execute mail merge
doc.MailMerge.Execute(names, values)
'Save document
doc.Save("out.doc")
End Sub
Private Sub MailMerge_MergeField157(ByVal sender As Object, ByVal e As MergeFieldEventArgs)
'Check if value is empty
If (String.IsNullOrEmpty(e.FieldValue.ToString())) Then
'Get parent row 
Dim parantRow As Node = e.Field.Start.GetAncestor(NodeType.Row)
'Remove row
parantRow.Remove()
End If
End Sub

Best regards.

Above code is applicable only when we use tables and there we want to remove the row. But i have got a document which contains only text . So in that case how to hide/show contents on certain conditions.

Hi
Thanks for your request. I think that you can try using bookmarks. Insert optional text into bookmarks. You should just set bookmark text as empty string for removing its content. For example see the following code.

doc.Range.Bookmarks["myBookamrk"].Text = string.Empty;

Hope this helps.
Best regards.

Hi,
Thanks for your suggestion .It works fine. but when i use bookmarks to hide/show part of document then unnecessary space is created. Kindly tell me how to remove spaces dynamically so the original format will be intact.

Hi
Thanks for your request. I think that you can also remove paragraph with bookmark. For example you can use the following code:

doc.Range.Bookmarks["myBookamrk"].Text = string.Empty;
doc.Range.Bookmarks["myBookamrk"].BookmarkStart.ParentNode.Remove();

Hope this helps.
Best regards.

Hi,
I am sending you the document i m trying to merge. But in the document there is a table in which i have created a bookmark . I want to hide/show this particular row in the table on condition for that i followed the same procedure. but when i run the code it gives me error as
“Start and end node should have the same grand parent”.I think bookmark doesnt work on table item. So please tell me alternative for the same so i can remove table items in the same document using bookmark or some other concept.Kindly find the attached document and also the code i m using for the same.
Plz reply as soon as possible

Hi
Thanks for your inquiry. I think that you can just remove Row from table. Please see my first post in this thread to learn how to achieve this.
Best regards.

Hi alexey,
thanks for ur suggestion.That option worked but now the problem is when i remove that particular row then ordering is changed. For instance if i remove row numbered 3 then automatically numbering gets changed and i see row no 4 after row 2. So can you gimme some solution on this. And displaying row no. is mandatory so i cant change it with bullets.

Hi
Thanks for your inquiry. You can try using the following code:

Private Sub MailMerge_MergeField157(ByVal sender As Object, ByVal e As MergeFieldEventArgs)
'Check if value is empty
If (String.IsNullOrEmpty(e.FieldValue.ToString())) Then
'Get parent row 
Dim parentRow As Row = CType(e.Field.Start.GetAncestor(NodeType.Row), Row)
'Get parent table
Dim parentTab As Table = parentRow.ParentTable
'Remove row
parentRow.Remove()
'Reorder numbers
For rowIndex As Integer = 1 To parentTab.Rows.Count - 1
Dim firstRun As Run = CType(parentTab.Rows(rowIndex).Cells(0).FirstParagraph.FirstChild, Run)
firstRun.Text = rowIndex.ToString()
Next
End If
End Sub

Hope this helps.
Best regards.

Hi,
Thanks a lot. This code was working fine in table. But i have got same numbering issue in simple text in the same document. So could you tell me how to avoid the same using bookmarks.

Hi
Thanks for your inquiry. Could you please provide me sample document? I will investigate this issue and provide you more information.
Best regards.

In this document we have listed all the points which are not part of table using numbering like 1,2,3. and i am using the bookmark to hide those points on condition. But when i hide point no 7. then automatically after point 6 , point 8 comes. so check the document and reply me asap.

Hi
Thank you for additional information. I think that the best way to solve this problem is to make each numbered paragraph to be a list item. If you need I can send you the changed document (if so please provide me your e-mail).
Best regards.

Hi,
I appreciate your support so far. But kindly help me with this issue since it is urgent requirement for me or atleast let me know whether it is possible or not.

Hi,
My email id is amitdangre2008@gmail.com. Please send me the updated document and also let me know how to use listitem in document.

Hi

Thanks for your request. I already sent the document to your mail. To use lists in word document you should just select paragraphs that should be list items and right click, select “Bullets and numbering” and select needed numbering format.
Best regards.

Hi Alexey,
I am using the following code for hiding a row in a word document

If (e.FieldName = "SDA") Then
If e.FieldValue = 0 Then
'Dim parantRow As Node = e.Field.Start.GetAncestor(NodeType.Row)
'Remove row
parantRow.Remove()
'doc.Range.Bookmarks("SDAV").BookmarkStart.ParentNode.Remove()
End If
End If

But it is giving me “OBJECT Reference not Sent” error at parantRow.Remove() line…pls help me.