Click here to Skip to main content
15,881,424 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,

I have a word document which has a merge field and 3 lines of address after that. For example,

<<SAMPLEMERGEFIELD>>
ADDRESS LINE 1
ADDRESS LINE 2
STATE


Through my vb.net application, I am replacing merge field with the value retrieved from the database. This functionality is working properly.
If value retrieved from database is NULL, then I would like to delete the <<SAMPLEMERGEFIELD>> as well as the 3 lines after that.
Do you have code to accomplish this?
Posted

1 solution

I really do believe there is an option to skip NULL data.

Have a look here:
Mail Merge: Part I[^]
Mail Merge: Part II[^]

If you still want to delete some portion of data, you need to know there is no "lines", there are paragraphs[^].
 
Share this answer
 
Comments
smehta.er 23-Jun-14 17:22pm    
Thanks for the reply. The address is hardcoded in the doc. Are you saying that I would have to find this address in the word document and then delete it?
Maciej Los 23-Jun-14 17:31pm    
If address is hard-coded, yeah! Follow this link: http://msdn.microsoft.com/en-us/library/f1f367bx.aspx
smehta.er 25-Jun-14 14:29pm    
Address is present at multiple places in the same doc. If I find and replace that with blank, there is a chance that address will be replaced at multiple places with blank. As of now, I found a workaround to delete the address. This address takes around 55 characters after the merge field. I am trying to delete 55 characters once I find the merge field in the doc and observe that the value retrieved from the database is Null or empty. Here is the sample code:

For Each wrdrange In wrdDoc.StoryRanges
With wrdrange.Find
.Forward = True
.ClearFormatting()
.Execute(FindText:=SAMPLEMERGEFIELD)
End With
If UCase(wrdrange.Text) = UCase(Trim(SAMPLEMERGEFIELD)) Then
If Trim(varsamplemergefield) = String.Empty Then
wrdrange.Delete(Unit:=Word.WdUnits.wdCharacter, Count:=55)
Else
wrdrange.Text = varsamplemergefield
End If
End If

Next wrdrange

I am looking for a solution to delete 3 lines rather than deleting 55 characters. I tried changing the units to wdLine but it does not work. Do you have any idea?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900