Consulting

Results 1 to 8 of 8

Thread: VBA to Move Data from UserForm to Bookmarks on Document

  1. #1

    VBA to Move Data from UserForm to Bookmarks on Document

    Hello, trying to make it so if I click on the "ok" button it takes all of the information I have written out in the textboxes, and put them on the document in the correct bookmark spot

    I have tried a few methods here and there, although cannot get them working...

    textbox name: txtFirstName
    bookmark name: firstName

    not sure if more information is needed - there are more textboxes / bookmarks although from one example I can easily fill in the rest.


    Thanks a lot
    Kryptooooo

  2. #2
    You will find the FillBM function at http://www.gmayor.com/word_vba_examples.htm
    Call this from your useform code as many times as you have bookmarks and fields, to fill your bookmarks with the textbox texts. The only proviso is that the bookmarks must not overlap.

    e.g.
    FillBM "firstName", Me.txtFirstName.Text
    etc
    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
    I don't know exactly where to put it, but I pasted it at the bottom of my Module containing all of the different button codes etc. and the first thing it did was return with an error
    "Compile Error: Invalid use of Me keyword" for the Me. parts.

    Thanks for the help.

  4. #4
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,335
    Location
    The FillBM procedure can go in the UserForm module or any standard module. The call i.e., FillBM "firstName", Me.txtFirstName.Text is a line of code you would put in the command button click event procedure in the Userform module. "Me" refers to the userform object.
    Greg

    Visit my website: http://gregmaxey.com

  5. #5
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  6. #6
    Thanks still had issues with the whole Me. thing sadly but I renamed them all with my UserForm name instead, now the only issue that I can see is it highlights "FillBM" and says
    "Compile Error: Sub or Function not defined"

    All my code is in a module called buttonMacros, but the FillBM Sub is in "ThisDocument" it is set as Public Sub, although it supposibly cannot be found.

    Public Sub FillBM(strBMName As String, strValue As String)     
        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:                                                     
        Exit Sub                                                  
    End Sub
    And this is the part where I am calling the FillBM:

    Sub mcrOk()
    
        frmParticipantInput.Hide                                                 
        
        FillBM "FirstandLastName", frmParticipantInput.txtFirstName.Text        
        FillBM "firstName", frmParticipantInput.txtFirstName.Text                
        FillBM "lastName", frmParticipantInput.txtLastName.Text 
    
        Unload frmParticipantInput 
    
    End Sub

  7. #7
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,335
    Location
    Why did you put FillBM in ThisDocument? ThisDocumet is a special class object. Put it in the same standard module as your mcrOk procedure or another standard module.
    Greg

    Visit my website: http://gregmaxey.com

  8. #8
    Ah, thanks - sorry don't know what I was thinking havn't messed with Word VBA + ThisDocument in a while, it works perfectly though thanks.

Tags for this Thread

Posting Permissions

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