View Single Post
 
Old 10-30-2022, 04:19 AM
bugy bugy is offline Windows 10 Office 2010
Novice
 
Join Date: Jan 2019
Posts: 16
bugy is on a distinguished road
Default

Quote:
Originally Posted by Italophile View Post
When you next ask a question please include as much detail as possible. It is very hard to help someone when they haven't provided vital details, i.e. "I have a document with inserted bookmarks" and "The document is created by a VB6 program"

The attached screenshot shows the difference between a collapsed and enclosing bookmark, and how to show bookmarks in your document.

If you have no control over the document and you know that the code is never more than 4 characters you can edit your code as follows:
Code:
Sub TestBookmark()
    Dim rng As Range
    Dim code As String
    If ActiveDocument.Bookmarks.Exists("code") Then
        Set rng = ActiveDocument.Bookmarks("code").Range
        If rng.Start = rng.End Then
            rng.MoveEnd wdCharacter, 4
            code = rng.Text
            Debug.Print code
        Else
            code = rng.Text
            Debug.Print code
        End If
    Else
        MsgBox "This bookmark doesn't exist" & vbCr & "code"
    End If
End Sub
attached image of my bookmark
I have executed the code that you have sent me and it has not given any error.
Code:
Sub TestBookmark()
    Dim rng As Range
    Dim code As String
    If ActiveDocument.Bookmarks.Exists("CodiPacient") Then
        Set rng = ActiveDocument.Bookmarks("CodiPacient").Range
        If rng.Start = rng.End Then
            rng.MoveEnd wdCharacter, 4
            code = rng.Text
            Debug.Print code
        Else
            code = rng.Text
            Debug.Print code
        End If
    Else
        MsgBox "This bookmark doesn't exist" & vbCr & "code"
    End If
End Sub
Can I save the value of the bookmark in a variable?
thanks
Attached Images
File Type: jpg 30-10-2022_12-14-47.jpg (8.7 KB, 16 views)
Reply With Quote