#1
|
|||
|
|||
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. |
#2
|
||||
|
||||
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 |
#3
|
|||
|
|||
Quote:
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:
Thank you for all the help! |
#4
|
|||
|
|||
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? |
#5
|
|||
|
|||
Quote:
And right, my OK button is named as CommandButton1. |
#6
|
|||
|
|||
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. |
#7
|
|||
|
|||
Quote:
|
#8
|
||||
|
||||
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
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
#9
|
|||
|
|||
Quote:
|
#10
|
||||
|
||||
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 |
#11
|
|||
|
|||
Quote:
|
Tags |
template, userform, vba |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
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 |
One document build from subfiles | duckman | Word | 3 | 03-04-2014 10:12 AM |
Automatic update of links in template - closing attached template without saving | stefaan | Word | 2 | 11-02-2013 07:46 AM |