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.

Sat Mar 09, 2019 3:47 pm

hello
i write this post because i dont know how to increase my convert function for text pattern "[%name_0%]" convert to word bookmark.

Code: Select all
        Using docBase As New Document("C:\tmp\Base.docx")

            Dim f As Regex = New Regex("\[\%[\w]+\%\]")
            Dim selections As TextSelection() = docBase.FindAllPattern(f)
            For Each selection As TextSelection In selections
                Dim range As TextRange = selection.GetAsOneRange()
                Dim id As Integer = range.OwnerParagraph.ChildObjects.IndexOf(range)
                Dim par As Paragraph = range.OwnerParagraph
                Dim nomSignet As String = range.Text.Replace("[%}", "")
                nomSignet = nomSignet.Replace("{%]", "")
                par.AppendBookmarkStart(nomSignet)
                par.AppendBookmarkEnd(nomSignet)
             
                range.Text = ""
                Dim bookmarkNavigator As BookmarksNavigator = New BookmarksNavigator(docBase)
                bookmarkNavigator.MoveToBookmark(nomSignet)
                bookmarkNavigator.InsertText(nomSignet)
            Next
            docBase.SaveToFile("C:\tmp\Baseresult.docx", FileFormat.Auto)
        End Using

can you give me some idea to set directly the text betweenAppendBookmarkStart and AppendBookmarkEnd ?
i try to set bookmarstart to par(id -1) (the end select pargraph -1) but that wont work


thank's
FH

labaffagaston
 
Posts: 42
Joined: Sun Dec 24, 2017 7:38 pm

Mon Mar 11, 2019 6:18 am

Hello,

Thanks for your inquiry and sorry for late reply as weekend.
What you want is to set the BookmarkStart and BookmarkEnd and make them contain the selection "[%name_0%]", right? If so, please refer to the following code. If there is any misunderstanding, please provide further details about your requirement, such as your input Word and expected output for our reference.
Code: Select all
 Using docBase As New Document("C:\tmp\Base.docx")
    Dim f As New Regex("\[\%[\w]+\%\]")
    Dim selections() As TextSelection = docBase.FindAllPattern(f)
    For Each selection As TextSelection In selections
        Dim range As TextRange = selection.GetAsOneRange()
        Dim id As Integer = range.OwnerParagraph.ChildObjects.IndexOf(range)
        Dim par As Paragraph = range.OwnerParagraph
        Dim nomSignet As String = range.Text.Replace("[%", "")
        nomSignet = nomSignet.Replace("%]", "")
        'Bookmark start
        Dim bmStart As New BookmarkStart(docBase, nomSignet)
        'Bookmark end
        Dim bmEnd As New BookmarkEnd(docBase, nomSignet)
        'Inset them at specific location
        par.ChildObjects.Insert(id, bmStart)
        par.ChildObjects.Insert(id + 2, bmEnd)
        range.Text = ""
        Dim bookmarkNavigator As New BookmarksNavigator(docBase)
        bookmarkNavigator.MoveToBookmark(nomSignet)
        bookmarkNavigator.InsertText(nomSignet)
    Next selection
    docBase.SaveToFile("C:\tmp\Baseresult.docx", FileFormat.Auto)
End Using

Sincerely,
Nina
E-iceblue support team
User avatar

Nina.Tang
 
Posts: 1187
Joined: Tue Sep 27, 2016 1:06 am

Mon Mar 11, 2019 7:49 am

hello,

i need to locate precisely in range.text the place of BmEnd

exemple

range text = "azerty"
i want include BmEnd at the postion -> if char-1 = "t"
result
[bmstart]azert[BmEnd]y


thank you for your help
FH

labaffagaston
 
Posts: 42
Joined: Sun Dec 24, 2017 7:38 pm

Mon Mar 11, 2019 8:28 am

Hi,

Thanks for your prompt reply.
Please refer to the following code to achieve your requirement. If there is any question, welcome to write back.
Code: Select all
 Using docBase As New Document("C:\test.docx")
     Dim selections() As TextSelection = docBase.FindAllString("azerty", False, True)
     For Each selection As TextSelection In selections
         Dim range As TextRange = selection.GetAsOneRange
         Dim id As Integer = range.OwnerParagraph.ChildObjects.IndexOf(range)
         Dim par As Paragraph = range.OwnerParagraph
         'split the "azerty" to "azert" and "y"
         Dim str1 As String = range.Text.Substring(0, (range.Text.ToCharArray.Count - 1))
         Dim str2 As String = range.Text.Remove(0, (range.Text.ToCharArray.Count - 1))
         'create a textrange and insert the "azert"
         Dim range1 As TextRange = New TextRange(docBase)
         range1.Text = str1
         'create a textrange and insert the "y"
         Dim range2 As TextRange = New TextRange(docBase)
         range2.Text = str2
         'insert the two textranges at the specific location
         par.ChildObjects.Insert(id, range1)
         par.ChildObjects.Insert((id + 1), range2)
         'remove the previous range
         par.ChildObjects.Remove(range)
         'Bookmark start
         Dim bmStart As BookmarkStart = New BookmarkStart(docBase, "test")
         'Bookmark end
         Dim bmEnd As BookmarkEnd = New BookmarkEnd(docBase, "test")
         'Inset them at specific location
         par.ChildObjects.Insert(id, bmStart)
         par.ChildObjects.Insert((id + 2), bmEnd)
     Next
     docBase.SaveToFile("Baseresult.docx", FileFormat.Auto)
 End Using

Sincerely,
Nina
E-iceblue support team
User avatar

Nina.Tang
 
Posts: 1187
Joined: Tue Sep 27, 2016 1:06 am

Mon Mar 11, 2019 10:23 am

ouuffffffff :o
it is'nt an easy way but its work fine. :)

thank you for your help
FH

labaffagaston
 
Posts: 42
Joined: Sun Dec 24, 2017 7:38 pm

Mon Mar 11, 2019 11:05 am

Hi,

Thanks for your feedback.
If you have other questions in the future, please feel free to write back.

Sincerely,
Nina
E-iceblue support team
User avatar

Nina.Tang
 
Posts: 1187
Joined: Tue Sep 27, 2016 1:06 am

Return to Spire.Doc