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
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
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.