Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 
Not applicable

Get Bookmark ID by Name

Hi,

sorry to need your help again:

I found the code in the community listing all available bookmarks:

function bookMarkNames

     bookmarks = ActiveDocument.GetDocBookmarkNames

     for i = 0 to UBound(bookmarks)

         bm = bookmarks(i)

         msgbox(bm)

     next

end function

I want to write the list of bookmarks in the possible value list of an input field. So actually I have two questions:

- How can I add the values returned above to the list of possible values in an input field?

- How do I get the bookmark ID (like BM01) when I know the name of it? (I imagine something like "ActiveDocument.GetBookmarkIDbyName(id)" or something like that...)

Thank you so much in advance,

Martin

11 Replies
Not applicable
Author

Ok, I found the solution for the first question, now only the second is left.

Is there perhaps another function similar to ActiveDocument.GetDocBookmarkNames which gives me the ID? Or is there any way to retrieve just as well the name as the ID of all bookmarks?

Thanks

Martin

Anonymous
Not applicable
Author

I have the same basic question.  Is there a way to get the ID of a bookmark if you only have the name?  Alternatively, is a bookmarks collection available via the API that will allow me to loop through each bookmark and allow me to select the bookmarks I want to work with?

Not applicable
Author

The only way that I found is to

- export all bookmarks to a file

- read the file

- compare each read bookmark to the one whose ID i need

- save the ID when it fits

not too beautiful but it works...

Not applicable
Author

Do you have any sample could on how to efficiently extract the ID for all bookmarks from the exported file?

I was able to do the following, but it parses through everything.

   ActiveDocument.ExportBookmarks "test.qbm"     

   Set objXMLDoc = CreateObject("Microsoft.XMLDOM")

   objXMLDoc.async = False

   objXMLDoc.load("test.qbm")

   set nodes = objXMLDoc.selectNodes("//*") 

   name_flag = false

   ReportName=""

   ReportID=""

   ReportInfoText=""

   for i = 0 to nodes.length -1

      If (nodes(i).nodeName = "Bookmark") Then

         name_flag = false

      End if

      If (nodes(i).nodeName = "Id") Then

            ReportID = nodes(i).text

      End if     

      If (nodes(i).nodeName = "Name") Then

         If (name_flag = false) Then

               ReportName = nodes(i).text

               name_flag = true

            End if

      End if            

      If (nodes(i).nodeName = "InfoText") Then

          ReportInfoText = nodes(i).text

             set Result = ActiveDocument.DynamicUpdateCommand("DELETE FROM _SR WHERE _SRName='" & ReportName & "'") 'Remove duplicates

             set Result = ActiveDocument.DynamicUpdateCommand("INSERT INTO * (_SRName,_SRId,_SRDescription) VALUES (" & ReportName & "," & ReportID & "," & ReportInfoText & ")")      

             ReportID=""

             ReportInfoText=""

             ReportName=""                 

      End if  

   Next

Not applicable
Author

Hi Nancy,

I used this code:

function findBookmarkIDs(bmno)

          saveFolder           = GetTempPath

          OSUser                     = ActiveDocument.Variables("v_OSUser").GetContent.String

          saveFile          = "Bookmarks.xml"

          savePath          = saveFolder & OSUser & saveFile

          ActiveDocument.ExportBookmarks savePath

             set oFile = createObject("Scripting.FileSystemObject")

             currentStatus = oFile.FileExists(savePath)

              if currentStatus = true then

                    Set objTextStream = oFile.OpenTextFile(savePath, 1)

                    linecnt = 0

                    bmid = ""

                    bmname = ""

                    foundid = 0

                    bmidresult = ""

                    bmtofind = ActiveDocument.Variables("v_bookmark" & bmno).GetContent.String

                    While Not objTextStream.AtEndOfStream

                                 line = objTextStream.ReadLine

                                 linecnt = linecnt + 1

                                 if ucase(left(trim(line), 4)) = "<ID>" and mid(trim(line), instr(trim(line), "\")+1, 2) = "BM" then

                                        bmid = mid(trim(line), instr(trim(line), "\")+1, 4)

                                        idcnt = linecnt

                                        foundid = 1

                                 end if

                                 if ucase(left(trim(line), 6)) = "<NAME>" and foundid = 1 and linecnt = (idcnt + 1) then

                                           bmname = mid(trim(line), 7, instr(ucase(trim(line)), "</NAME>")-7)

                                           bmname = replace(replace(replace(bmname, "Ä", "Ä"), "ä", "ä"), "Ö", "Ö")

                                           bmname = replace(replace(replace(bmname, "ö", "ö"), "Ãœ", "Ü"), "ü", "ü")

                                           bmname = replace(bmname, "ß", "ß")

                                           ActiveDocument.Variables("v_bookmarkdebug").setContent bmname, true

                                        if bmname = bmtofind then

                                                  bmidresult = bmid

                                        end if

                                        foundid = 0

                                 end if

                    Wend

                    objTextStream.Close

                    Set objTextStream = Nothing

          end if

          set oFile = Nothing

          'ActiveDocument.Variables("v_bookmark" & bmno & "id").setContent bmidresult, true

          msgbox bmidresult

end function

Not applicable
Author

I am out of the office until 07/15/2013.

Note: This is an automated response to your message "[Development

(QlikView Desktop)] - Re: Get Bookmark ID by Name" sent on 07/12/2013

1:12:18.

This is the only notification you will receive while this person is away.

richard_pearce6
Luminary Alumni
Luminary Alumni


This is my code to read Bookmark ID's, names, and store them into input fields

' This code saves the document bookmarks as a text file
' For the code to work Allow System Access must be selected in the VBA editor
' Macro override security must be checked in document properties
' Users on Access Point must select correct macro security level Ctrl + Shift + M

' ---------- This table is required in the script ---------------------
' INPUTFIELD BOOKMARK.ID,BOOKMARK.NAME;
'
' Bookmarks:
' Load
'  RowNo() as BOOKMARK.Index
'  ,0 as BOOKMARK.ID
'  ,0 as BOOKMARK.NAME
' AutoGenerate 10; // <<< This is the max number of bookmarks the code can handle
' ----------------------------------------------------------------------


' Get the user NTName for the user (storred in a variable)
SET var_NTName = ActiveDocument.Variables("var_NTName")

' Replace the \ as this won't work in the file path
var_NTName = replace(var_NTName.Getcontent.string,"\","")

' This is the file name for the bookmark export, the NT name is added to stop any file saving clashes and so you don't pick up other peoples private bookmarks
vFileName = "\\FilePath\Bookmark_Export" & var_NTName & ".txt"

' Export Booksmarks as Text file
ActiveDocument.ExportBookmarks vFileName


' --------------------------------------------------- Reset the Input Fields Values --------------------------------------------------------------
dim x(1)
x(0) = 0 ' element to reset

set fld = ActiveDocument.Fields("BOOKMARK.ID")
fld.ResetInputFieldValues 0, x ' 0 = All values reset, 1 = Reset Possible value, 2 = Reset single value

set fld = ActiveDocument.Fields("BOOKMARK.NAME")
fld.ResetInputFieldValues 0, x ' 0 = All values reset, 1 = Reset Possible value, 2 = Reset single value



' --------------------------------------------------- This section reads the txt document ---------------------------------------------------------
'<filename>, IOMode (1=Read,2=write,8=Append)
Set objFileToRead = CreateObject("Scripting.FileSystemObject").OpenTextFile(vFileName,1)

' This variable will control the row number for the inputfields. Set to -1 as will be increased to zero before the first store
vBookmarkNumber = -1

' These are the input fields to be updated, the table needs an index field (rowno()) and autogenerated with enough lines
' to handle the max number of bookmarks
set vFieldID  = ActiveDocument.Fields("BOOKMARK.ID")
set vFieldName  = ActiveDocument.Fields("BOOKMARK.NAME")


' This loop reads each line of the txt file
Dim strLine
do while not objFileToRead.AtEndOfStream
      strLine = objFileToRead.ReadLine()
     
      ' First Trim the line to remove any leading or trailing spaces
      strLine = trim(strLine)
     
   if vBookMarkFound = 1 then
       if ucase(left(strLine,4)) = "<ID>" Then
    ' Set the input field removing the <ID> tags
        vFieldID.SetInputFieldValue vBookmarkNumber, mid(strLine,5,len(strLine)-9)
       
       Elseif ucase(left(strLine,6)) = "<NAME>" Then
        ' Set the input field removing the <NAME> tags
        vBookMarkFound = 0
        vFieldName.SetInputFieldValue vBookmarkNumber, mid(strLine,7,len(strLine)-13)
      
       END IF

   else
       ' <ID> tag is repeated through the file so need to ensure its immediately past the <BOOKMARK> flag
       if ucase(left(strLine,10)) = "<BOOKMARK>" Then
        vBookMarkFound = 1
        vBookmarkNumber = vBookmarkNumber + 1
       Else
        vBookMarkFound = 0
       END IF
      end if 
 
loop

objFileToRead.Close
Set objFileToRead = Nothing

rajeshvaswani77
Specialist III
Specialist III

Hi Martin,

Is there a way of doing this without an export?

thanks,

Rajesh Vaswani

Not applicable
Author

Hi Rajesh,

I didn't do further research on this topic, sorry. Perhaps there is another way but I don't know it.

Martin