Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 05-04-2015, 12:31 AM
rogelinepaula rogelinepaula is offline Build an Automatic Document Template for Word Windows 8 Build an Automatic Document Template for Word Office 2013
Novice
Build an Automatic Document Template for Word
 
Join Date: May 2015
Posts: 22
rogelinepaula is on a distinguished road
Question Build an Automatic Document Template for Word

Hi, all!



My boss gave me a task to create a standard template for his business. He wants a userform to appear once you open the word document. The first userform will be the Name of the Report, second is the Date, the third userform is for the Client's name, fourth is for the Contact Person's name and the last userform is for the Contact Person's address.

I have seen countless tutorials and unfortunately, I still couldn't get the hang of it. The information he will be typing in the said userforms have to autopopulate the bookmarked fields in the word document, by the way.

I could really use all the help since this is due 3 days from now. I already know how to add the bookmarks, the text boxes, labels, command buttons except when it comes to generating the code, that is where my mind goes totally blank.

Can anybody please help me generate a code for this?

Thank you in advance.
Reply With Quote
  #2  
Old 05-04-2015, 02:45 AM
gmayor's Avatar
gmayor gmayor is offline Build an Automatic Document Template for Word Windows 7 64bit Build an Automatic Document Template for Word Office 2010 32bit
Expert
 
Join Date: Aug 2014
Posts: 4,103
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

You only neeed 1 userform, with several fields. For a relatively simple description, see http://www.gmayor.com/Userform.htm That example uses docvariables and their associated fields rather than bookmarks, but if you want to fill bookmarks instead see the FillBM function at http://www.gmayor.com/word_vba_examples.htm
__________________
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
  #3  
Old 05-04-2015, 03:21 AM
rogelinepaula rogelinepaula is offline Build an Automatic Document Template for Word Windows 8 Build an Automatic Document Template for Word Office 2013
Novice
Build an Automatic Document Template for Word
 
Join Date: May 2015
Posts: 22
rogelinepaula is on a distinguished road
Default

Quote:
Originally Posted by gmayor View Post
You only neeed 1 userform, with several fields. For a relatively simple description, see http://www.gmayor.com/Userform.htm That example uses docvariables and their associated fields rather than bookmarks, but if you want to fill bookmarks instead see the FillBM function at http://www.gmayor.com/word_vba_examples.htm
Thank you, gmayor!

I have seen a YouTube tutorial and I basically followed everything. I got the UserForm that I want once you open the document but for some reason the "Client" field which I have bookmarked in the document isn't being populated by whatever it is that I am typing in the userform under the Client field.

Here's the code that I've been using:
Quote:
Private Sub CommandButton1_Click()
Dim RepTitle As Range
Set RepTitle = ActiveDocument.Bookmarks("RepTitle").Range
RepTitle.Text = Me.TextBox1.Value
Dim ReportDate As Range
Set ReportDate = ActiveDocument.Bookmarks("ReportDate").Range
ReportDate.Text = Me.TextBox2.Value
Dim Client As Range
Set Client = ActiveDocument.Bookmarks("Client").Range
Client.Text = Me.TextBox3.Value
Dim ContactName As Range
Set ContactName = ActiveDocument.Bookmarks("ContactName").Range
ContactName.Text = Me.TextBox4.Value
Dim ContactAdd As Range
Set ContactAdd = ActiveDocument.Bookmarks("ContactAdd").Range
ContactAdd.Text = Me.TextBox5.Value
Dim DivInCharge As Range
Set DivInCharge = ActiveDocument.Bookmarks("DivInCharge").Range
DivInCharge.Text = Me.TextBox6.Value
Me.Repaint
UserForm1.Hide


End Sub
I am using Word 2013. Should that be an issue re bookmarks? And also, how do I make the userform disappear as soon as I hit the "OK" button?


Thank you for all the help!
Reply With Quote
  #4  
Old 05-04-2015, 05:06 AM
Charles Kenyon Charles Kenyon is offline Build an Automatic Document Template for Word Windows 8 Build an Automatic Document Template for Word Office 2013
Moderator
 
Join Date: Mar 2012
Location: Sun Prairie, Wisconsin
Posts: 9,138
Charles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant future
Default

A couple of things. First, I think you mean the "Client" bookmark, not field.
Second, bookmarks work the same in Word 2013 as they do in earlier versions.
Is your OK button named CommandButton1?
Reply With Quote
  #5  
Old 05-04-2015, 09:07 AM
rogelinepaula rogelinepaula is offline Build an Automatic Document Template for Word Windows 8 Build an Automatic Document Template for Word Office 2013
Novice
Build an Automatic Document Template for Word
 
Join Date: May 2015
Posts: 22
rogelinepaula is on a distinguished road
Default

Quote:
Originally Posted by Charles Kenyon View Post
A couple of things. First, I think you mean the "Client" bookmark, not field.
Second, bookmarks work the same in Word 2013 as they do in earlier versions.
Is your OK button named CommandButton1?
Hello, Charles! Yes, I meant the Client bookmark in the document and the Name of Client field in the username. I was wondering why the text I am typing in the userform does not appear anywhere in the document when I have already bookmarked it.

And right, my OK button is named as CommandButton1.
Reply With Quote
  #6  
Old 05-04-2015, 10:32 AM
Charles Kenyon Charles Kenyon is offline Build an Automatic Document Template for Word Windows 8 Build an Automatic Document Template for Word Office 2013
Moderator
 
Join Date: Mar 2012
Location: Sun Prairie, Wisconsin
Posts: 9,138
Charles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant future
Default

Graham is the expert on Userforms, I'm just a novice myself, especially compared to him. If you have not already done so, I recommend strongly that you review Graham's written tutorials. Create a Simple Userform is the one with document variables.

For a different take, look at this one, too. Create & Employ a Userform

Both Graham Mayor and Greg Maxey know far more about this stuff than I do. I have successfully used UserForms by following their instructions. That is how I knew to ask the questions I did.

BTW, renaming that button cmdOK would make it easier to follow the code, at least for me. I tend to write these things and get them to work, then not look at them again for years.
Reply With Quote
  #7  
Old 05-04-2015, 10:55 AM
rogelinepaula rogelinepaula is offline Build an Automatic Document Template for Word Windows 8 Build an Automatic Document Template for Word Office 2013
Novice
Build an Automatic Document Template for Word
 
Join Date: May 2015
Posts: 22
rogelinepaula is on a distinguished road
Default

Quote:
Originally Posted by Charles Kenyon View Post
Graham is the expert on Userforms, I'm just a novice myself, especially compared to him. If you have not already done so, I recommend strongly that you review Graham's written tutorials. Create a Simple Userform is the one with document variables.

For a different take, look at this one, too. Create & Employ a Userform

Both Graham Mayor and Greg Maxey know far more about this stuff than I do. I have successfully used UserForms by following their instructions. That is how I knew to ask the questions I did.

BTW, renaming that button cmdOK would make it easier to follow the code, at least for me. I tend to write these things and get them to work, then not look at them again for years.
I appreciate your help, Charles. And yes, I might as well be steering in the docvariables direction since the one with the bookmark doesn't seem to be working in my favor. Many thanks again!
Reply With Quote
  #8  
Old 05-04-2015, 09:46 PM
gmayor's Avatar
gmayor gmayor is offline Build an Automatic Document Template for Word Windows 7 64bit Build an Automatic Document Template for Word Office 2010 32bit
Expert
 
Join Date: Aug 2014
Posts: 4,103
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

If you add the FillBM function that I pointed to in my earlier post you can simply call it as required to fill your document bookmarks (provided they don't overlap) e.g.

Code:
Private Sub CommandButton1_Click()
    Me.Hide
    FillBM "RepTitle", Me.TextBox1.Text
    FillBM "ReportDate", Me.TextBox2.Text
    FillBM "Client", Me.TextBox3.Text
    'etc
End Sub
I agree with Charles that it is better to use meaningful names for the elements, but let's not run before we can walk.
__________________
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
  #9  
Old 05-05-2015, 12:24 AM
rogelinepaula rogelinepaula is offline Build an Automatic Document Template for Word Windows 8 Build an Automatic Document Template for Word Office 2013
Novice
Build an Automatic Document Template for Word
 
Join Date: May 2015
Posts: 22
rogelinepaula is on a distinguished road
Default

Quote:
Originally Posted by gmayor View Post
If you add the FillBM function that I pointed to in my earlier post you can simply call it as required to fill your document bookmarks (provided they don't overlap) e.g.

Code:
Private Sub CommandButton1_Click()
    Me.Hide
    FillBM "RepTitle", Me.TextBox1.Text
    FillBM "ReportDate", Me.TextBox2.Text
    FillBM "Client", Me.TextBox3.Text
    'etc
End Sub
I agree with Charles that it is better to use meaningful names for the elements, but let's not run before we can walk.
Much appreciated, Graham! So basically this will just be the code that I need to copy and just replace the bookmark values, right? For some reason when I try to use the first code, everything I've bookmarked in the document disappears but the title and the other info were autopopulated. I will try this code and will update you with the result. Have great day!
Reply With Quote
  #10  
Old 05-05-2015, 02:55 AM
gmayor's Avatar
gmayor gmayor is offline Build an Automatic Document Template for Word Windows 7 64bit Build an Automatic Document Template for Word Office 2010 32bit
Expert
 
Join Date: Aug 2014
Posts: 4,103
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

Basically yes. You can copy the FillBM function to the template module that contains the macro that calls the userform. That macro at its simplest would be something like

Code:
Sub AutoNew()
Dim oFrm As New UserForm1        'The name of the userform
    oFrm.Show
    Unload oFrm
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
  #11  
Old 05-05-2015, 04:32 PM
rogelinepaula rogelinepaula is offline Build an Automatic Document Template for Word Windows 8 Build an Automatic Document Template for Word Office 2013
Novice
Build an Automatic Document Template for Word
 
Join Date: May 2015
Posts: 22
rogelinepaula is on a distinguished road
Default

Quote:
Originally Posted by gmayor View Post
Basically yes. You can copy the FillBM function to the template module that contains the macro that calls the userform. That macro at its simplest would be something like

Code:
Sub AutoNew()
Dim oFrm As New UserForm1        'The name of the userform
    oFrm.Show
    Unload oFrm
End Sub
Hello again, graham! I've tried using the code but it gave me the error "Compile Error: Sub or Function not defined." And then the "Private Sub CommandButton1_Click()" is highlighted in yellow with the arrow outside the box and the 'FillBM' is also highlighted. When I went back to the document, I've also noticed that the bookmarks were once again gone. I wonder if I did something wrong.
Reply With Quote
Reply

Tags
template, userform, vba



Similar Threads
Thread Thread Starter Forum Replies Last Post
Build an Automatic Document Template for Word VBA using Building Blocks to build document kintap Word VBA 2 07-11-2014 10:56 AM
Create a Form to Build a Document Wordsquirrel Word VBA 7 05-16-2014 10:33 AM
Build an Automatic Document Template for Word One document build from subfiles duckman Word 3 03-04-2014 10:12 AM
Build an Automatic Document Template for Word Automatic update of links in template - closing attached template without saving stefaan Word 2 11-02-2013 07:46 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 12:40 AM.


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