Consulting

Results 1 to 7 of 7

Thread: Adding Bookmark to Multiselect ListBox Text

  1. #1
    VBAX Newbie
    Joined
    Oct 2016
    Posts
    5
    Location

    Adding Bookmark to Multiselect ListBox Text

    Hello everyone,

    I am brand new to using VBA and still trying to learn how to use it. I have managed to get my document exactly how I need it but am hung up on only one part.

    In my userform, there is a ComboBox with several different options and based on what option is selected, certain text is inserted into the document. The one I am having trouble with though inserts the text from selections that are made in a multiselect listbox and inserts them at a bookmark within the document. I have got the code to where all selections are inserted into the document and the bookmark is re-created but, the bookmark is being created before the text that is entered from the listbox.

    What I am needing is to have the Listbox text appear in the bookmark. Here is my code:

    ElseIf ComboBox2.Value = "Clinical Request" Then
    Dim ii As Integer
    ActiveDocument.Bookmarks("Content").Select
    Set BMRange = ActiveDocument.Bookmarks("Content").Range
    For ii = o To ListBox1.ListCount - 1
    If ListBox1.Selected(ii) Then
    Selection.Text = ListBox1.List(ii)
    Selection.MoveRight
    Selection.TypeParagraph
    ActiveDocument.Bookmarks.Add "Content", BMRange
    End If
    Next ii
    Application.ScreenRefresh



    Any help would be greatly appreciated, thanks!

  2. #2
    You don't need to select a bookmark in order to write to it. The simplest solution is to use the FillBM function from my web site, which you can call from your code. (Shouldn't that be ComboBox1 ? )
    If ListBox1.ListIndex > -1 Then FillBM "Content", ListBox1.Text
    It might however be advisable to rename the bookmark (and its call in the code) to something that reflects the nature of the beast - e.g. bmContent. It may avoid confusion later.


    Public Sub FillBM(strBMName As String, strValue As String)
    'Graham Mayor - http://www.gmayor.com
    Dim orng As Range
        With ActiveDocument
            On Error GoTo lbl_Exit
            Set orng = .Bookmarks(strBMName).Range
            orng.Text = strValue
            orng.Bookmarks.Add strBMName
        End With
    lbl_Exit:
        Set orng = Nothing
        Exit Sub
    End Sub
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  3. #3
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,340
    Location
    If you are using Word 2007 or higher why not write to a content control (vice bookmark). Insert a plain text CC titled "Selections" in the document (or a bookmark bmSelections")

    Private Sub UserForm_Initialize()
      With ListBox1
        .MultiSelect = fmMultiSelectMulti
        .AddItem "A"
        .AddItem "B"
        .AddItem "C"
      End With
    lbl_Exit:
      Exit Sub
    End Sub
    Private Sub CommandButton1_Click()
    Dim lngIndex As Long
    Dim strSelected As String
      For lngIndex = 0 To ListBox1.ListCount - 1
        If ListBox1.Selected(lngIndex) Then
          strSelected = strSelected & ListBox1.List(lngIndex) & vbCr
        End If
      Next
      FillCC "Selections", strSelected
      FillBM "bmSelections", strSelected
      Hide
    lbl_Exit:
      Exit Sub
    End Sub
    
    Public Sub FillCC(strCCTitle As String, strValue As String)
      With ActiveDocument.SelectContentControlsByTitle(strCCTitle).Item(1)
        .MultiLine = True
        .Range.Text = strValue
      End With
    lbl_Exit:
      Exit Sub
    End Sub
    Public Sub FillBM(strBMName As String, strValue As String)
      'Graham Mayor - http://www.gmayor.com
    Dim orng As Range
      With ActiveDocument
        On Error GoTo lbl_Exit
        Set orng = .Bookmarks(strBMName).Range
        orng.Text = strValue
        orng.Bookmarks.Add strBMName
      End With
    lbl_Exit:
      Set orng = Nothing
      Exit Sub
    End Sub
    Greg

    Visit my website: http://gregmaxey.com

  4. #4
    VBAX Newbie
    Joined
    Oct 2016
    Posts
    5
    Location
    Thank you both very much, this fixed my problem.

    Is there a way to use the FillBM function with AutoText from Quick Parts?

  5. #5
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,340
    Location
    No, but this will. You will need to pass the bookmark name, template name e.g., Normal.dotm, the Building Block gallery type e.g., 9 for autotext, the category nam and building block name:

    Sub ScratchMacro()
      'A quick macro scratch pad created by Greg Maxey
      fcnFillBMwithBB "bmTarget", "Normal.dotm", 9, "Signatures", "George Washington"
    End Sub
    
    
    Function fcnFillBMwithBB(strBM As String, strTemplate As String, lngGal As Long, strCat As String, strBB As String)
    Dim oTmp As Template
    Dim lngIndex As Long
    Dim oRng As Word.Range
      Templates.LoadBuildingBlocks
      For Each oTmp In Templates
        If oTmp.Name = strTemplate Then Exit For
      Next oTmp
      'You can use this For ... Next loop to determine the gallery type constant for your BBs.
      For lngIndex = 1 To 35
        Debug.Print oTmp.BuildingBlockTypes(lngIndex).Name & " "; lngIndex
      Next
      Set oRng = ActiveDocument.Bookmarks(bmTarget).Range
      Set oRng = oTmp.BuildingBlockTypes(lngGal). _
          Categories(strCat).BuildingBlocks(strBB).Insert(oRng, True)
      ActiveDocument.Bookmarks.Add bmTarget, oRng
    lbl_Exit:
      Set oTmp = Nothing: Set oRng = Nothing
    End Function
    Last edited by gmaxey; 11-02-2016 at 09:22 AM.
    Greg

    Visit my website: http://gregmaxey.com

  6. #6
    I have no doubt that Greg's macro will do the job, but I also have a function to insert a building block in a bookmark:
    Public Sub BBToBM(strBMName As String, strTemplate As String, strBBName As String)
    'Graham Mayor - http://www.gmayor.com
    Dim orng As Range
    Dim iLen1 As Integer, iLen2 As Integer
        With ActiveDocument
            iLen1 = Len(ActiveDocument.Range)
            On Error GoTo lbl_Exit
            Set orng = .Bookmarks(strBMName).Range
            Application.Templates(strTemplate). _
                    BuildingBlockEntries(strBBName).Insert _
                    Where:=orng, _
                    RichText:=True
            iLen2 = Len(ActiveDocument.Range)
            orng.End = orng.End + (iLen2 - iLen1)
            orng.Bookmarks.Add strBMName
        End With
    lbl_Exit:
        Set orng = Nothing
        Exit Sub
    End Sub
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  7. #7
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,340
    Location
    Again Graham and I are showing you several ways of doing things with VBA. There is nothing wrong with Grahams "implicit" method and I often in not generally use the same "BuildingBlockEntries().Insert method as he did. You will have no problem with that method as long as you ensure you Building Blocks names are unique.

    However, suppose you or one of your users has a Building Block named "TestBB" in the Normal template AutoText gallery "General" category, one named "TestBB" in the same gallery in a "Test Category" category and one named "TestBB" in the QuickParts gallery "General" category.

    Which one will be inserted in the bookmark? Word has to decide and with Word 2010 it will choose the one in the AutoText gallery, General category. However in Word 2007 the choice is different.

    Here is a version of my earlier code that lets you decide if you want to pass just a BB name and let Word sort it out or explicitly pass the gallery, category and BB name.

    Note: once you have the list of gallery constants you can delete the For lngIndex = 1 to 35 ... Next loop.



    Sub Demo()
      'General (let Word sort it out)
      fcnFillBMwithBB "bmTest", "Normal.dotm", "TestBB"
      'Explicit
      fcnFillBMwithBB "bmTest", "Normal.dotm", "TestBB", 1, "Test Category"
    End Sub
    
    Function fcnFillBMwithBB(strBM As String, strTemplate As String, strBBName As String, _
                             Optional lngGal As Long = 0, Optional strCat As String = "General")
    Dim oTmp As Template
    Dim lngIndex As Long
    Dim oRng As Word.Range
      Templates.LoadBuildingBlocks
      For Each oTmp In Templates
        If oTmp.Name = strTemplate Then Exit For
      Next oTmp
      'You can use this For ... Next loop to determine the gallery type constant for your BBs.
      For lngIndex = 1 To 35
        Debug.Print oTmp.BuildingBlockTypes(lngIndex).Name & " "; lngIndex
      Next
      Set oRng = ActiveDocument.Bookmarks(strBM).Range
      If lngGal > 0 Then
        Set oRng = oTmp.BuildingBlockTypes(lngGal). _
            Categories(strCat).BuildingBlocks(strBBName).Insert(oRng, True)
      Else
        Set oRng = Application.Templates(strTemplate). _
            BuildingBlockEntries(strBBName).Insert(oRng, True)
      End If
      ActiveDocument.Bookmarks.Add strBM, oRng
    lbl_Exit:
      Set oTmp = Nothing: Set oRng = Nothing
    End Function
    Greg

    Visit my website: http://gregmaxey.com

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •