Consulting

Page 2 of 2 FirstFirst 1 2
Results 21 to 31 of 31

Thread: Templates verses Forms/Document

  1. #21
    VBAX Master TonyJollans's Avatar
    Joined
    May 2004
    Location
    Norfolk, England
    Posts
    2,291
    Location
    Hi Aaron,

    For a dropdown you should be able to use
    [VBA]ActiveDocument.FormFields("DropdownBookmarkName").Result[/VBA]
    to get the string, or
    [VBA]ActiveDocument.FormFields("DropdownBookmarkName").Dropdown.Value[/VBA]
    to get the number in the list, which you can use as an index into
    [VBA]ActiveDocument.FormFields("DropdownBookmarkName").Dropdown.ListEntries[/VBA]

    For a text field, for some odd reason, you are better off using
    [VBA]ActiveDocument.Bookmarks("TextFieldBookmarkName").Fields(1).Result[/VBA]
    Enjoy,
    Tony

    ---------------------------------------------------------------
    Give a man a fish and he'll eat for a day.
    Teach him how to fish and he'll sit in a boat and drink beer all day.

    I'm (slowly) building my own site: www.WordArticles.com

  2. #22
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    1. Validation is a good thing. You will have to build up solid logic for it, since you have so many fields.

    2. To help with that, I would strongly recommend explicitly naming your formfields.

    DropDown8 - really tells you nothing
    ApprovedBy - does.

    What I am saying is:[vba]ActiveDocument.FormFields("ApprovedBy").Result[/vba] is much easier to follow through your code, than:[vba]ActiveDocument.FormFields("DropDown8").Result[/vba]

    Again, since you are talking about validation, having explicit names will likely make it easier.

    As another suggestion, make a FormField object of the document formfield collection. You will have to type a lot less. Right now you have to type:[vba]ActiveDocument.FormFields("DropDown8").Result[/vba]If you make a FormField object, it will contain all the formfields in the document. Then you can reference them using the object, like this:[vba]Dim oFF As Word.FormFields
    Dim strApprovedBy As String
    Set oFF = ActiveDocument.FormFields
    strApprovedBy = oFF("DropDown8").Result[/vba]which would give you a string of the selected item in DropDown8 - which again would be better off explicitly named.

  3. #23
    VBAX Master TonyJollans's Avatar
    Joined
    May 2004
    Location
    Norfolk, England
    Posts
    2,291
    Location
    To be fair, it was me who left the default names in the sample I knocked up. You are, of course, right that meaningful names should be used in any proper solution.
    Enjoy,
    Tony

    ---------------------------------------------------------------
    Give a man a fish and he'll eat for a day.
    Teach him how to fish and he'll sit in a boat and drink beer all day.

    I'm (slowly) building my own site: www.WordArticles.com

  4. #24
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    Tony, could you elaborate your comment re: text formfields, it is better using Fields(1)?

    I get an error.

    [vba]ActiveDocument.FormFields("Text1").Result[/vba]returns the result.

    [vba]ActiveDocument.Bookmarks("Text1").Fields(1).Result[/vba]returns an compiling error - "Member not found".

  5. #25
    VBAX Master TonyJollans's Avatar
    Joined
    May 2004
    Location
    Norfolk, England
    Posts
    2,291
    Location
    Oops! Sorry, that should be:
    [VBA]ActiveDocument.Bookmarks("Text1").Range.Fields(1).Result[/VBA]
    I have to confess that I don't know all the details but there are some situations where referencing text formfields doesn't work properly. One that immediately springs to mind is setting the value to a string longer than 255 characters. Also formatting can sometimes be applied to the field rather than the formfield. As referencing the field always works it is generally a safer recommendation.
    Enjoy,
    Tony

    ---------------------------------------------------------------
    Give a man a fish and he'll eat for a day.
    Teach him how to fish and he'll sit in a boat and drink beer all day.

    I'm (slowly) building my own site: www.WordArticles.com

  6. #26
    VBAX Regular
    Joined
    Sep 2006
    Posts
    15
    Location
    Hi,

    First of all, I would like to say thank you for all your assistance. It really helped me.

    I would like to ask one more question with regards to the tool bar that was added.

    To add or remove buttons, where can I see how this is done?

    T.I.A.

    Cheers,
    Aaron

  7. #27
    VBAX Master TonyJollans's Avatar
    Joined
    May 2004
    Location
    Norfolk, England
    Posts
    2,291
    Location
    I can't remember exactly what I did but here is some sample code which should get you on the right road. Drop this in a module in your template
    [vba]
    Sub ToolbarSetUp()
    Dim myCB As CommandBar
    Dim myCBCombo As CommandBarComboBox
    Dim myCBButton As CommandBarButton

    CustomizationContext = MacroContainer

    On Error Resume Next
    CommandBars("Toolbar Name").Delete
    On Error GoTo 0

    Set myCB = CommandBars.Add("Toolbar Name", msoBarTop)
    With myCB
    Set myCBButton = .Controls.Add(msoControlButton)
    With myCBButton
    .Style = msoButtonCaption
    .Caption = "Button 1 Caption"
    .OnAction = "Button1Code"
    End With
    Set myCBButton = .Controls.Add(msoControlButton)
    With myCBButton
    .Style = msoButtonCaption
    .BeginGroup = True
    .Caption = "Button 2 Caption"
    .OnAction = "Button2Code"
    End With
    Set myCBButton = .Controls.Add(msoControlButton)
    With myCBButton
    .Style = msoButtonCaption
    .Caption = "Button 3 Caption"
    .OnAction = "Button3Code"
    End With
    .Enabled = True
    .Visible = True
    End With
    end sub
    [/vba]
    Enjoy,
    Tony

    ---------------------------------------------------------------
    Give a man a fish and he'll eat for a day.
    Teach him how to fish and he'll sit in a boat and drink beer all day.

    I'm (slowly) building my own site: www.WordArticles.com

  8. #28
    VBAX Regular
    Joined
    Sep 2006
    Posts
    15
    Location
    Hi,

    Actually, it was not done in code.

    Cheers,
    Aaron

  9. #29
    VBAX Master TonyJollans's Avatar
    Joined
    May 2004
    Location
    Norfolk, England
    Posts
    2,291
    Location
    Hi Aaron,

    We're obviously talking about different things. The toolbar in my sample was created in code with something similar to what I last posted. What are you referring to?
    Enjoy,
    Tony

    ---------------------------------------------------------------
    Give a man a fish and he'll eat for a day.
    Teach him how to fish and he'll sit in a boat and drink beer all day.

    I'm (slowly) building my own site: www.WordArticles.com

  10. #30
    VBAX Regular
    Joined
    Sep 2006
    Posts
    15
    Location
    I did not see any code that created a toolbar in the example you sent me.

    Cheers
    Aaron

  11. #31
    VBAX Master TonyJollans's Avatar
    Joined
    May 2004
    Location
    Norfolk, England
    Posts
    2,291
    Location
    The code was a one-time thing. Once it had been run, the toolbar was there and the code was not needed in the template any more. As I said, I can't remember exctly what I did but it would have been similar to the code I just posted.

    You can create toolbars manually via Tools > Customize. Create the toolbar from the Toolbars tab and add the buttons from the Commands tab.
    Enjoy,
    Tony

    ---------------------------------------------------------------
    Give a man a fish and he'll eat for a day.
    Teach him how to fish and he'll sit in a boat and drink beer all day.

    I'm (slowly) building my own site: www.WordArticles.com

Posting Permissions

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