Showing and hiding details in word document

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.

sorry this code

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

Hi
Thanks for your inquiry. Maybe you should check whether parentRow is not null. Please try using the following code:

If (e.FieldName = "SDA") Then
If e.FieldValue = 0 Then
Dim parantRow As Node = e.Field.Start.GetAncestor(NodeType.Row)
'Remove row
If (Not parantRow Is Nothing) Then
parantRow.Remove()
End If
End If
End If

Hope this helps.
Best regards.

Thank You Very Much.