Deleting Spaces


I am using this component to open a word template and apply the data from a dataset to bookmarks in the document

The problem I am having is on the address where I have the house number and street on the same line.

When a house number doesn't exist it leaves a gap as follows

Person A

How do I get rid of this blank space?



Please attach a document and a code that you use to fill in the data.


dsData contains a dataset of customers name and addresses details
dsBookmarks contains a dataset showing what data item related to what bookmark in the document.

'create word document
Dim document As Aspose.Word.Document = New Aspose.Word.Document(droTemplate.Url)
Dim builder As Aspose.Word.DocumentBuilder = New Aspose.Word.DocumentBuilder(document)

'loop through bookmarks
Dim droBookmark As BookmarksDataSet.tblBookmarksRow
For Each droBookmark In dsBookmarks.tblBookmarks.Rows
If Not IsDBNull(dsData.Tables(0).Rows(0).Item(droBookmark.DatabaseField)) Then
Dim bookmark As Aspose.Word.Bookmark = builder.Document.Range.Bookmarks(droBookmark.DatabaseField)
If Not bookmark Is Nothing Then
If dsData.Tables(0).Rows(0).Item(droBookmark.DatabaseField).ToString.Trim.Length > 0 Then
builder.Document.Range.Bookmarks(droBookmark.BookmarkName).Text = dsData.Tables(0).Rows(0).Item(droBookmark.DatabaseField)
End If
End If
End If


Here is the code to remove space after the bookmark, called "BuildingNumber"

' move builder to after the end of bookmark "BuildingNumber"

builder.MoveToBookmark("BuildingNumber", False, True)

CType(builder.CurrentNode, Run).Text = ""


Keep getting the following error, builder.CurrentNode is set to nothing even after moving to the bookmark.

The first data item it fails on is OrganisationName, for which a bookmark exists but the data item is Null, so it should delete the bookmark line.

System.InvalidCastException: Conversion from string "Object reference not set to an i" to type 'Boolean' is not valid. ---> System.FormatException: Input string was not in a correct format.
   at Microsoft.VisualBasic.CompilerServices.Conversions.ParseDouble(String Value, NumberFormatInfo NumberFormat)
   at Microsoft.VisualBasic.CompilerServices.Conversions.ToBoolean(String Value)
   --- End of inner exception stack trace ---
   at Microsoft.VisualBasic.CompilerServices.Conversions.ToBoolean(String Value)
   at Service.CreateDocument(Int64 DocumentTemplateID, Int64 UniqueID, String SaveFileAs) in c:\inetpub\wwwroot\DocumentEngine\App_Code\Word.vb:line 161




Figured out a better way of doing it using the mailmerge and amending how the data is passed to it.




The code I’ve provided works if you actually have a paragraph with some text in it (in your case it is space) immediately after the bookmark with the given name. Otherwise, CurrentNode will be null after MoveToBookmark, which is normal.