Results 1 to 6 of 6

Thread: VBA Word Macros

  1. #1
    New Lounger
    Join Date
    Dec 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    VBA Word Macros

    I hope someone is able to help me out. I have created a macro which auto runs and opens a form which asks the user questions, the answers are then entered into a document at bookmarked locations.
    This worked fine whilst the questions were text, however now I need to be able to use check boxes and drop downs and have them enter info into the document.
    This and example of what I am using for the text entry.

    Selection.GoTo What:=wdGoToBookmark, Name:="name"
    Selection.TypeText Text:=txtName

    If there is a better way of doing it please let me know.

    Thanks

  2. #2
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    3,947
    Thanks
    0
    Thanked 204 Times in 185 Posts
    Hi LearnerPlates,

    Welcome to the forum.

    You'd probably do better to store the user inputs in custom document properties, which can then be retrieved in the document via DOCPROPERTY fields.

    Using bookmarks is risky - delete the content and the bookmark goes with it, both losing the data forever and invalidating any cross-references to the bookmarks and any further updates via your form. Similarly, it's all too easy to inadvertantly add content to a bookmark and screw up any cross-references or, if the bookmark is updated from the form, the extra content disappears.

    As it stands, your bookmark updating code doesn't actually update the bookmark's contents - it merely appends the output after it. If the user changes their mind, you might end up with both the before and after content in the document. Having said that, to update a bookmark, use code like:
    Code:
    Sub UpdateBookmark (BmkNm as string, NewTxt as string)
    Dim BmkRng as Range
    With ActiveDocument
      If.Bookmarks.Exists(BmkNm) Then
        Set BmkRng =.Bookmarks(BmkNm).Range
        BmkRng.Text = NewTxt
        .Bookmarks.Add BmkNm, BmkRng
      End if
    End With
    Set BmkRng = Nothing
    End Sub
    To use this, you'd add a line like the following to your code:
    Call UpdateBookmark ("name", txtName)
    Cheers,

    Paul Edstein
    [MS MVP - Word]

  3. #3
    New Lounger
    Join Date
    Dec 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks Paul,

    How do I create DocProperty fields as I need my users to be able to enter things like height weight and tick boxes?

  4. #4
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    3,947
    Thanks
    0
    Thanked 204 Times in 185 Posts
    Hi LearnerPlates,

    Creating the custom document properties varies by Word version. In Word 2003 & earlier, the quickest way there is Alt-f, i then click on Custom. In Word 2007 & later, the quickest way there is Alt-f, i then click on Properties|Advanced|Custom. You can then start adding the names, types & default values.

    In the document, you then reference these by creating the DOCPROPERTY fields wherever you want the output to appear (you can make multiple references to the same document properties, if you want).

    The simplest way to make a DOCPROPERTY field that points to a property named 'myprop' is to press Ctrl-F9 to create a pair of field braces (ie '{ }'), then type 'DOCPROPERTY myprop' between them, so that you end up with {DOCPROPERTY myprop}. Press F9 and you're done. Regarding your form's tick boxes, if you want to replicate their state in the document, you'll need a bit more than a simple DOCPROPERTY field. Post back if that's what you're trying to do.

    One final thing you'll want to add to your code is a line like 'ActiveDocument.Fields.Update', to update the document content after you've updated the document properties.
    Cheers,

    Paul Edstein
    [MS MVP - Word]

  5. #5
    New Lounger
    Join Date
    Dec 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks again Paul.
    I hope this is the right track.

    So I have added the following fields to my custom docproperty list, then added the fields to my document, I am using Word 2010 (I should have mentioned that earlier sorry).

    Can you tell me what the difference is between doing as you suggested and typing Ctrl-F9 {DOCPROPERTY Name} or adding them using the insert field option, in which case I get {DOCPROPERTY Name \* MERGEFORMAT}?

    So if my understanding is correct we are creating these to replace the bookmarks in order that they are not able to be moved or replaced by the users accidently, is that correct?

    How do I reference them in my macro and yes please do tell me how to get a tick box response to merge into my document.

    Thank you so much for your help

  6. #6
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    3,947
    Thanks
    0
    Thanked 204 Times in 185 Posts
    Hi LearnerPlates,

    The '\* MERGEFORMAT' switch is added by Word when you leave the 'Preserve formatting during updates' option checked while inserting a field. I've never found it to do anything useful and, sometimes, to have produced weird results.

    As with bookmarks, DOCPROPERTY fields can be deleted. The main reasons for using custom document properties are that, even if the DOCPROPERTY fields get deleted, the properties remain in the document and can still be :
    1. updated via your form
    2. re-inserted in the document via another DOCPROPERTY field.

    To update the custom document properties and the document content from your macro, you'd use code like:
    Code:
    With ActiveDocument
      .CustomDocumentProperties("Red").Value = "123"
      .CustomDocumentProperties("Blue").Value = ""
      .CustomDocumentProperties("Green").Value = "Yes"
      .CustomDocumentProperties("Yellow").Value = "No"
      .Fields.Update
    End With
    For a checkbox output, suppose you have a custom document property named 'Chk1', whose values are 1=checked, 0=unchecked. In that case, you'd nest a DOCPROPERTY field inside a SYMBOL field coded as:
    {SYMBOL {=163-{DOCPROPERTY Chk1}*79} \a \f "Wingdings 2"}
    In this construction, the '163' is the character code for an unchecked box in the "Wingdings 2" font - it's what you'd get with {SYMBOL 163 \a \f "Wingdings 2"}. The corresponding character code for the checked box is 84. So what I've done is applied some field maths to the DOCPROPERTY field to deduct 79 from the 163 if the custom document property's value is 1.

    Note: All of the field brace pairs (ie '{ }') for the above example are created via Ctrl-F9 - you can't simply type them or copy & paste them from this message.
    Cheers,

    Paul Edstein
    [MS MVP - Word]

Posting Permissions

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