Spire.Doc is a professional Word .NET library specifically designed for developers to create, read, write, convert and print Word document files. Get free and professional technical support for Spire.Doc for .NET, Java, Android, C++, Python.

Tue Apr 17, 2018 2:04 pm

Hallo,
We replace our existing tabledata like this:

Code: Select all
For col As Integer = 0 To datacolcount - 1
           datarow.Cells(col).Paragraphs.Clear()
           Dim p As Paragraph = datarow.Cells(col).AddParagraph
           Dim tr As TextRange = p.AppendText(data(r - firstDataRowIndex, col))
Next


How to prevent an existing bookmark (in the cell) from deleting?

thx. for help
Johannes

gaj
 
Posts: 6
Joined: Thu Apr 12, 2018 8:48 am

Wed Apr 18, 2018 3:34 am

Hello,

Thanks for your post. Please refer to the below code snippet and give it a try.
Code: Select all
Dim document As Document = New Document
document.LoadFromFile("test.docx")
Dim bn As BookmarksNavigator = New BookmarksNavigator(document)
bn.MoveToBookmark("test", true, true)
Dim table As Table = CType(bn.CurrentBookmark.BookmarkEnd.OwnerParagraph.Owner.Owner.Owner,Table)
Dim tableCell As TableCell = CType(bn.CurrentBookmark.BookmarkEnd.OwnerParagraph.Owner,TableCell)
Dim tableRow As TableRow = tableCell.OwnerRow
Dim rowIndex As Integer = table.Rows.IndexOf(tableRow)
Dim cellIndex As Integer = table.Rows(rowIndex).Cells.IndexOf(tableCell)
'remove the objects in the cell that contains the bookmark except the bookmark
For Each p As Paragraph In tableCell.Paragraphs
    Dim objCount As Integer = (p.ChildObjects.Count - 1)
    Do While (objCount >= 0)
        Dim obj = p.ChildObjects(objCount)
        If Not ((TypeOf obj Is BookmarkStart)  _
                    OrElse (TypeOf obj Is BookmarkEnd)) Then
            p.ChildObjects.Remove(obj)
        End If
       
        objCount = (objCount - 1)
    Loop
   
Next
'replace data in the cells
Dim row As Integer = 0
Do While (row < table.Rows.Count)
    Dim col As Integer = 0
    Do While (col < table.Rows(row).Cells.Count)
        'replace data in the cells which don't contain bookmark
        If Not ((row = rowIndex)  _
                    AndAlso (col = cellIndex)) Then
            table.Rows(row).Cells(col).Paragraphs.Clear
            Dim p As Paragraph = table.Rows(row).Cells(col).AddParagraph
            Dim tr As TextRange = p.AppendText("hello")
        Else
            'insert data to the cell which contains the bookmark
            bn.InsertText("bookmark text", true)
            'table.Rows(row).Cells(col).Paragraphs(0).AppendText("bookmark text");//both ok
        End If
       
        col = (col + 1)
    Loop
   
    row = (row + 1)
Loop

document.SaveToFile("Result.docx", Spire.Doc.FileFormat.Docx2010)
System.Diagnostics.Process.Start("Result.docx")


Best regards,
Simon
E-iceblue support team
User avatar

Simon.yang
 
Posts: 620
Joined: Wed Jan 11, 2017 2:03 am

Thu Apr 19, 2018 8:41 am

Hello,

Did you try the code I provided? How is it going?
We will appreciate it if you could give us some feedback.

Best regards,
Simon
E-iceblue support team
User avatar

Simon.yang
 
Posts: 620
Joined: Wed Jan 11, 2017 2:03 am

Thu Apr 19, 2018 12:18 pm

Hallo Simon,
Yes I tryed your code and it is working perfect.

Thx for your great Support :)
Johannes

gaj
 
Posts: 6
Joined: Thu Apr 12, 2018 8:48 am

Fri Apr 20, 2018 1:31 am

Hello,

Glad to hear that it helps. It's my pleasure. Just feel free to contact us if you have questions.

Best regards,
Simon
E-iceblue support team
User avatar

Simon.yang
 
Posts: 620
Joined: Wed Jan 11, 2017 2:03 am

Return to Spire.Doc

cron