Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 05-24-2017, 09:24 AM
NickDenton NickDenton is offline VBA user forms for auto filling word document Windows 8 VBA user forms for auto filling word document Office 2016
Novice
VBA user forms for auto filling word document
 
Join Date: May 2017
Posts: 1
NickDenton is on a distinguished road
Default VBA user forms for auto filling word document

Hi, I am trying to write some simple code in VBA to use user forms to auto fill text in a Word document.


I have created bookmarks in the Word document and text boxes on the User form but when I click the OK command button nothing happens.
this is the code I have written:-

Private Sub cmdOK_Click()
With ActiveDocument
.Bookmarks("BorrowerName").Range.Text = txtBorrowerName.Value
End With

Any help would be gratefully received
Reply With Quote
  #2  
Old 05-24-2017, 08:26 PM
gmayor's Avatar
gmayor gmayor is offline VBA user forms for auto filling word document Windows 10 VBA user forms for auto filling word document Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,101
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

I would recommend that you use the function FillBM from my web site (and repeated below) to fill the named bookmark (of which the spelling must be correct). The syntax here would be

FillBM "BorrowerName", txtBorrowerName.Text

Code:
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
In a simple userform the code could be like the following, albeit with your bookmark names and the textbox texts.

Code:
Private Sub cmdOK_Click()
   'Hide the userform
   Hide
   'e.g. Assign the values of the three text boxes to the three   bookmarks
   'Using the Function FillBM
   FillBM "bm1", TextBox1.Text
   FillBM "bm2", TextBox2.Text
   FillBM "bm3", TextBox3.Text
   'Unload the form
   Unload Me
End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
VBA user forms for auto filling word document filling in forms character limit benjii19 Word 1 12-29-2015 11:49 AM
Problems with filling in forms Gijs Verhoeff Word 0 09-24-2015 03:29 AM
Automate filling in PDF Forms from Word? dlowrey Word 5 09-23-2014 10:12 PM
filling in forms mepossem Word 3 05-25-2013 02:35 AM
VBA user forms for auto filling word document Filling out job application forms. wordy Word 1 07-31-2012 12:16 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 08:13 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft