Page 1 of 2 12 LastLast
Results 1 to 15 of 18
  1. #1
    New Lounger
    Join Date
    Jan 2003
    Location
    Fort Worth, Texas, USA
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Newbie needs VBA help (Eng/WordXP/Office2000)

    I spent a couple years of my life writing WordPerfect (5.1) macros to produce a vast number of fill-in-the-blank forms. Now that our office has moved into the 20th century, (not yet the 21st, that will come in a few decades) I have been tasked with re-writing the macros in Word. Additionally, they have to be "user friendly". Read idiot proof and simple.

    I have created templates of all the forms. Several posts have given me a start on building UserForms to gather the information that needs to be plugged into the documents created from the templates. I started out with one form that asked the user about a dozen questions, but when it came time to open and produce the document, I could not find or create the code to allow the user to open a new document (from the template), make the UserForm come up, then put their entries into the document and get it to print as a duplex document (it is two pages, so should print on front and back).

    Not being able to gather the additional code I needed, I decided to create InputBoxes to fill in the forms blanks. That worked pretty well, but I am still up a creek needing code to do the same thing that I needed with my UserForm.

    Can ya'll give me some help either by referring articles/tutorials or with some of the cod?. I would appreciate any guidance or help that you can give me.

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Newbie needs VBA help (Eng/WordXP/Office2000)

    Hi Gwynne,

    I'll try to sketch some of the parts.

    1. Opening a new document.
    The templates you create should be installed in one of the templates folders. Word uses two folders (plus their subfolders) for this: the user templates folder and the workgroup templates folder. The user templates folder is usually located on the local hard disk of the user; the workgroup templates folder is not set in a default installation; you could have it point to a shared folder on the network. If you install the templates in this shared folder, your users will be able to create a new document based on one of these by selecting File | New... in Word, then clicking General Templates in the New Document task pane.

    2. Executing code automatically.
    You have already created a userform and written macros, so you have worked with the Visual Basic Editor. You may have noticed ThisDocument in the Project Explorer. This is the container for document-level macros. If you double click ThisDocument, its (class) module will open. Select Document from the object dropdown list in the upper left corner of the window. Now, you can select document-level events from the events dropdown list in the upper right corner.
    The Document_New event occurs when the user creates a new document based on the template, and the Document_Open event occurs when the user opens a document based on the template. You want to display a userform when the user creates a new document, so you will want to write code in the Document_New event.
    Code in the ThisDocument module can be hard to debug. I usually write most of my code in standard modules, and just call this from the document events.

    3. Displaying a form.
    This is simple: use FormName.Show where FormName is the name of your userform.

    4. Passing info to the document.
    You can use document variables to store information the user has entered in the form. ActiveDocument.Variables is a collection of variables you can add elements to:

    ActiveDocument.Variables.Add Name:="Temp", Value:="12"

    You can display document variables in a document by inserting a DocVariable field (Insert | Field...). The field code could look like { DOCVARIABLE "Temp" } (you can't type the braces yourself, you must use Insert | Field... or type Ctrl+F9 to get them).

    This is only a start. Perhaps it'll set you on your way, and no doubt others will chime in with more tips. Good luck.

  3. #3
    New Lounger
    Join Date
    Jan 2003
    Location
    Fort Worth, Texas, USA
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Newbie needs VBA help (Eng/WordXP/Office2000)

    Thanks Hans. At least this will get me started on the right path.

  4. #4
    New Lounger
    Join Date
    Aug 2002
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Newbie needs VBA help (Eng/WordXP/Office2000)

    (Edited by HansV to make URL live - see <!help=19>Help 19<!/help>)

    A long time ago, I also was programming WP macros (PerfectScript) before moving on to VBA, VB, etc.

    The best way to make the transition is to teach yourself Word VBA (and VBA in general) with a few good books. Check out this recommended VBA book list: http://www.microsystems.com/books_vba.htm. Of all the books they have listed, I'd start out with: Writing Word Macros and Word 2000 Developer's Handbook.

    Good luck,

    Stephan

  5. #5
    Gold Lounger
    Join Date
    Dec 2000
    Location
    New Hampshire, USA
    Posts
    3,386
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Newbie needs VBA help (Eng/WordXP/Office2000)

    See the list of WordVBA books at my URL below.
    You likely should start with Steve Roman's Writing Word Macros.

  6. #6
    5 Star Lounger kmurdock's Avatar
    Join Date
    Feb 2003
    Location
    Pacific Grove, California, USA
    Posts
    716
    Thanks
    10
    Thanked 34 Times in 28 Posts

    Re: Newbie needs VBA help (Eng/WordXP/Office2000)

    Hi Gwynne,

    OK, this is just my two cents...

    I started working in VBA when my firm decided to make the switch from WordPerfect to Word just before Office 97 came out. It was necessary to jump into VBA immediately, because the hundreds of forms and macros that the whole firm relied on were essential to their practice.

    Here's where the two cents come in: Remember that you don't always have to use macros, templates and wizards to do everything in Word. It has its own strengths and weaknesses. You might be able to accomplish a lot of what WP did with a macro using the regular features of Word like autotext and fields. My problem was I knew how to program but I didn't know so much about Word. (You know that old saying -- if the only tool you have is a hammer, every problem looks like a nail.)

    As you're learning VBA, you will naturally learn Word. Still, you can't learn Word soon enough -- you'll save yourself a lot of work. And believe me, there will still be plenty of programming to do. <img src=/S/grin.gif border=0 alt=grin width=15 height=15>

    Kim

  7. #7
    New Lounger
    Join Date
    Jan 2003
    Location
    Fort Worth, Texas, USA
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Newbie needs VBA help (Eng/WordXP/Office2000)

    Thanks for the tip, Kim. However, I have been working in Word for about 8 years now, but have not had to do macros as all our macros were in WordPerfect (ugh). I'm lost when it comes to programming! <img src=/S/confused.gif border=0 alt=confused width=15 height=20>. Let me explain what I have and maybe someone can jump in and give me some help. I have created a template called Subpoena. I have InPut boxes that ask for information that is then plugged into the *.DOC. Once I go to File/New, and pull up the DOC. from the DOT, how do I make my InPut boxes come up? I'm not very good at this <img src=/S/scratch.gif border=0 alt=scratch width=25 height=29>

  8. #8
    5 Star Lounger kmurdock's Avatar
    Join Date
    Feb 2003
    Location
    Pacific Grove, California, USA
    Posts
    716
    Thanks
    10
    Thanked 34 Times in 28 Posts

    Re: Newbie needs VBA help (Eng/WordXP/Office2000)

    Oooops. Then you know all about Word. I assumed you were a WP expert. But you seem to have inherited the Word programming . It sounded as if you worked in a law office; straddling the word processing fence is a common practice for a firm.

    I'm a little confused about your process, though. Your template has the Subpoena form and input boxes. Have you placed the code in a document event such as DocumentNew? That way the input boxes code will run automatically when you create a new document from the template. You can also put them in a DocumentOpen event so they will run whenever the document is opened (this can get very annoying, however, as it will happen EVERY time). You can also put a toolbar button on a new or standard toolbar that will run the sub whenever the user chooses.

    Does any of this do what you want? If so, I can elaborate...

    Kim

  9. #9
    New Lounger
    Join Date
    Jan 2003
    Location
    Fort Worth, Texas, USA
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Newbie needs VBA help (Eng/WordXP/Office2000)

    Used to work in a law office, but now work for the district clerk.

    I'vd created UserForms (InputBoxes) that I want to display when the new document is opened. When you pull up the new document, there are fields within. In VBE I have created InPut boxes that ask questions in order to obtain the results for those fields. I need help in bringing up the input boxes while the user is at the subpoena form. The only way I can do it is from VBE and pressing <F5>. Believe me, programming is not my forte!!!!

  10. #10
    5 Star Lounger kmurdock's Avatar
    Join Date
    Feb 2003
    Location
    Pacific Grove, California, USA
    Posts
    716
    Thanks
    10
    Thanked 34 Times in 28 Posts

    Re: Newbie needs VBA help (Eng/WordXP/Office2000)

    OK, it sounds like all you need is a mechanism to launch your code. There are a couple of ways to approach this.

    First, you can create a document new event that will run whenever you create a new document from your template. The way to do this is to open your template, then open VBA and find your template in the project explorer (usually on the left top of the window). You should see below the name the "Microsoft Word Objects." If the folder is closed, click the + next to it to show the "ThisDocument" object. If you double click on it, you should open a blank code window.

    In the code window, type

    Private Sub Document_New()

    **type or copy your code here**

    End Sub

    When you File|New to create a document based on your Subpoena template, the document_new event "fires" as VBA recognizes that you are creating a new document. Your code will run automatically and the document that results should have the results of your input boxes (since it works from running the code from the VBA editor).

    If you want the code to run every time the document is opened, your must put your code in a similar sub routine:

    Private Sub Document_Open()

    **your code here**

    End Sub

    This code will run EVERY time you open the document. It is probably not what you want, so unless there's an insurmountable reason that the input boxes capture data to an already existing document rather than while the new document is being created, then this isn't a very good solution. You can Cancel the input boxes, but if you have more than a half dozen, anyone using the documents will become tired of that.

    Because the code is not actually in the document, but really in the template associated with the document, you can associate the Subpoena with another template (like normal.dot) and the prompting will stop. This seems like an unnecessary step, unless there's something about your document that I don't know. Do you add information over several editing sessions?

    The last way to run the code is "on demand" by the user. Leave your code where it is in the template (it is in the template and not in normal, right?). Add a button to the toolbar that runs the code. Do that this way:

    Open the Subpoena template. Click Tools|Customize. Click on the Commands tab and in the Categories list, scroll down to "Macros" and click. To the right, you'll see all the macros that are currently available. Make certain that the "Save in" drop down list at the bottom of the window shows the name of your template.

    **We're going to save the button to run the code ONLY in the template that has the code. It will only appear when the Subpoena template is active. I like to have a separate toolbar for this kind of thing saved in normal.dot. The buttons come and go as templates are loaded and unloaded from the environment. The bottom line is that they should only appear when it is appropriate to use them.**)

    OK, now scroll down the macros listed on the right of the dialog box until you find your input box macro. Click and drag it to the toolbar of your choice. You can right click and make the button look better -- check the options on the context sensitive help.

    Then, you will be able to run your code by clicking on the button. You should, by the way, be able to go to Tools|Macro|Macros (or Alt-F8) and be able to find your code in the list of Macro names. You should also be able to run it from there.

    Note that running a macro or assigning it to a toolbar button requires that your code be public. The two examples I gave above use the word "Private" because only VBA has to find and use them. In order for a user to find and run the code, you need to have your sub look like this:

    Sub MySubRoutine()
    **code**
    End Sub

    -or-

    Public Sub MySubRoutine()
    **code**
    End Sub

    There are other interesting rules about code visibility that you'll address as you become more proficient. I learned a LOT of code from deconstructing the code that came from Microsoft (though much of it is a lot more than you'll ever need). I have also made good use of program help (make sure you have the VBA help installed, I think it is not the default to do so). Use the Knowledge Base at microsoft.com and there are several books out there (I think a thread ran last week in this forum on the subject).

    If this doesn't make sense, please let me know. If it does -- good!

    Kim

  11. #11
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Newbie needs VBA help (Eng/WordXP/Office2000)

    UserForm objects are something different than a Visual Basic InputBox. I mention this only because some people reading this might be confused about what you're working with.

    Let's suppose your userform is named UserForm1 - to cause Word to display UserForm1, you create one or more procedures in which you use code such as this:

    (Code in the ThisDocument module)
    Private Sub Document_New()
    InsertDataFromMyForm
    End Sub

    (Code in a regular module, e.g., Module1)
    Public Sub InsertDataFromMyForm()
    UserForm1.Show
    ' If the code "behind" UserForm1 hasn't already taken care of inserting info into
    ' the document, you would do it here
    Unload UserForm1
    Set UserForm1 = Nothing
    End Sub

    If you're new to all this, you're probably wondering how to get the data from the textboxes on your UserForm1 into the document. There are at least two typical approaches: (1) place bookmarks in the document in advance so your code can visit each bookmark and type in the information, or (2) use tables to create your form so you code can visit the appropriate cells to type in the information.

    Let's pretend that UserForm1 has an OK button named CommandButton1, and someone's first name is entered in TextBox1. The "code pane" for UserForm1 might contain a procedure like this:

    Private Sub CommandButton1_Click()
    Me.Hide
    'Position at the FirstName bookmark (if this bookmark is more than a point,
    ' it will be selected)
    Selection.GoTo What:=wdGoToBookmark, Name:="FirstName"
    ' Type user's data at the bookmarked place in the document
    Selection.TypeText Me.TextBox1.Value
    ' repeat until you've finished all the information
    End Sub

    If the data was supposed to go in the second cell on the first row of the first table in the document, the procedure might look like this:

    Private Sub CommandButton1_Click()
    Me.Hide
    ' Position at beginning of first cell of table
    ActiveDocument.Tables(1).Select
    Selection.Collapse Direction:=wdCollapseStart
    ' Tab to second cell (any contents will be selected)
    Selection.MoveRight Unit:=wdCell, Count:=1, Extend:=wdMove
    ' Enter user's data into the cell
    Selection.TypeText Me.TextBox1.Value
    ' move forward and repeat until you've finished all the information
    End Sub

    I am lazily not explaining these various commands in detail, but if you paste them into a code module, highlight the interesting parts and press F1, you will gets lots of information. Hope this helps.

  12. #12
    New Lounger
    Join Date
    Jan 2003
    Location
    Fort Worth, Texas, USA
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Newbie needs VBA help (Eng/WordXP/Office2000)

    Kim and Jefferson

    It works! It works! It works!

    Thanks to all who made this possible. This is a great site and a bunch of great people!

  13. #13
    Platinum Lounger
    Join Date
    Nov 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    5,016
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Newbie needs VBA help (Eng/WordXP/Office2000)

    Great post Jefferson! It puts things into perspective very nicely. You mention "at least two typical approaches":

    <hr>If you're new to all this, you're probably wondering how to get the data from the textboxes on your UserForm1 into the document. There are at least two typical approaches: (1) place bookmarks in the document in advance so your code can visit each bookmark and type in the information, or (2) use tables to create your form so you code can visit the appropriate cells to type in the information.
    <hr>

    What pros & cons do you see with using Document Variables to store UserForm information, and fields to display it within the document? I've found this approach to be very handy, especially if the document is reopened and the UserForm data is partially or totally changed i.e. the DocVars can be used to easily reload the form with the existing information, all ready for editing.

    Alan

  14. #14
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Newbie needs VBA help (Eng/WordXP/Office2000)

    At one point, I believed the the existence of Document Variables could lead Word to alert the user to the presence of macros and customizations. I don't know if that's true any more (or if it ever was), but that led me in the direction of using Custom Document Properties when I want to store information persistently in a document.

  15. #15
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Newbie needs VBA help (Eng/WordXP/Office2000)

    AFAIK, document variables don't cause any alert. I like to use them since they are invisible to a standard user, so it's hard to tamper with them (unless you know VBA of course <img src=/S/evilgrin.gif border=0 alt=evilgrin width=15 height=15>). Custom document properties work well too, but can be manipulated in the Word interface (File | Properties, Custom tab.)

Page 1 of 2 12 LastLast

Posting Permissions

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