Deleting Spaces


#1

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
Street
Town
County

How do I get rid of this blank space?

Thanks


#2

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


#3

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)
Else
'NEEDS TO DELETE THE BOOKMARK AND SPACES HERE
End If
End If
End If
Next


#4

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 = ""


#5

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

Ta

Gary


#6

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

Ta


Gary


#7

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.