Results 1 to 21 of 21

Thread: VBA and Word

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2006
    Posts
    10

    VBA and Word

    I am having much difficulty with editing this form that I created. My company knows my knowledge w/ C/C++ and because of this had me create a registration form in Word. Here are my issues, hopefully someone can assist.

    1.) I have a few drop down boxes w/ 3 choices. If the user chooses a specific option, i need it to enable different checkboxes below, when the user exits the drop down.

    For ex. Size/Life/Money are in the drop down, what is the command to disable different check boxes, the code w/in the brackets is what I need? I have

    VB Code:
    1. IF [??? "size is chosen" ???] then activedocument.formfields("checkbox1").enabled = False

    2. Our company uses Lotus notes and I would like for the command button I have to automatically open up Lotus and attach the form as an attachment and populate the subject line and To: field. Not sure if this is possible but I have to ask. I have used so far:

    VB Code:
    1. activedocument.sendmail

    3. I have a few text boxes that I need to test the length for them, so for example if the length text_user1 (text box) is larger than 1, and other boxes are not filled out ... then POP UP ERROR MsgBox. Here is my pseudo

    if activedocument.formfields("user1") is > 0 AND user1_address = 0 Or user1_phone = 0 --> pop up MsgBox

    4. I am really looking to prevent the users from saving this form to their HDD, or pretty much having access to the menu bars, is there anyway via VBA coding to remove them or, have them remain there but not be able to click on them. Not sure if this will totally screw up their application, if it does, then I not worried about it, but would be nice to have this single document locked down.

    Thanks in advance, please remember that I am using really basic VBA code in this form because I am pretty much clueless, my forte is C/C++ and I offered to create something directly from that but they want Word.

    Thanks again!

  2. #2

    Thread Starter
    New Member
    Join Date
    Apr 2006
    Posts
    10

    Re: VBA and Word

    Anyone? Bumpin Up

  3. #3
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: VBA and Word

    Welcome to the Forums Chip.

    Your post is a liitle unclear. Are you using a word Document as a form or do you have a VBA UserForm included in the document?

    If the former, then..Are the controls you are using from the "Forms" or the "Control Toolbox" Toolbar?
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  4. #4

    Thread Starter
    New Member
    Join Date
    Apr 2006
    Posts
    10

    Re: VBA and Word

    I have a Word document that is loaded w/ bookmarks and I have VBA coding used within in...I was just handed this file and said...make it idiot proof.

    To make it clearer now that I know what you mean....there is no UserForm, just Microsoft Word Objects and a Module.
    Last edited by ChiPhiZD; Apr 26th, 2006 at 11:01 AM.

  5. #5
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: VBA and Word

    I have a few drop down boxes w/ 3 choices
    Are these dropdows from the "Forms" or the "Control Toolbox" Toolbar?
    This is the key question, the way we handle this is dependant on the answer.

    Can you upload a copy of the .doc file?
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  6. #6

    Thread Starter
    New Member
    Join Date
    Apr 2006
    Posts
    10

    Re: VBA and Word

    Believe me I wish I could but there is some sensitive company information on this document that prevents me from uploading it....

    All of the text, dropdowns, checkboxes are from the forms toobar, I have 2 command buttons that are from the command toolbar


    Hope this helps thanks again
    Last edited by ChiPhiZD; Apr 26th, 2006 at 11:20 AM.

  7. #7
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: VBA and Word

    Yes that helps.
    You are using Form Field controls which have a very limited events model when compared with VBA Forms controls. You can make this work, but you can only fire the code on the exit event of the dropdown. You do this by assiging the procedure in the "Rum Macro on Exit" DD in the field properties.

    Were you to use VBA Forms Controls, you would have a much richer environment to work in. For instance, you can have the code fire on the _change event, which IMHO is much nicer.

    I have attached 2 doc.
    the first one has an example of the form fields controls that you are using. The on_exit event code for the combobox, in Module1, is..
    VB Code:
    1. Sub FormComboChange()
    2. Dim sOption As String
    3.    
    4.     sOption = ThisDocument.FormFields("cboOptions").Result
    5.    
    6.     'clear current values
    7.     ThisDocument.FormFields("chkSize").CheckBox.Value = False
    8.     ThisDocument.FormFields("chkLife").CheckBox.Value = False
    9.     ThisDocument.FormFields("chkMoney").CheckBox.Value = False
    10.    
    11.     Select Case sOption
    12.         Case "Size": ThisDocument.FormFields("chkSize").CheckBox.Value = True
    13.         Case "Life": ThisDocument.FormFields("chkLife").CheckBox.Value = True
    14.         Case "Money": ThisDocument.FormFields("chkMoney").CheckBox.Value = True
    15.     End Select
    16. End Sub

    The second file, shows how you would achieve this result using VBA Forms controls (available on the "Control Toolbox" Toolbar). the procedures here are in the "ThisDocument" module, with the following _Change event procedure for the combobox.
    VB Code:
    1. Private Sub cboOptions_Change()
    2.    
    3.     Me.chkSize.Value = False
    4.     Me.chkLife.Value = False
    5.     Me.chkMoney.Value = False
    6.    
    7.     Select Case Me.cboOptions.Value
    8.         Case "Size": Me.chkSize.Value = True
    9.         Case "Life": Me.chkLife.Value = True
    10.         Case "Money": Me.chkMoney.Value = True
    11.     End Select
    12. End Sub
    Attached Files Attached Files
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  8. #8

    Thread Starter
    New Member
    Join Date
    Apr 2006
    Posts
    10

    Re: VBA and Word

    Ok see attachment maybe, one of us is not understanding....most likely me
    Attached Images Attached Images  

  9. #9
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: VBA and Word

    That's exactly what I'm showing you in ChiPhiZD_BookmarkForms_Example.doc
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  10. #10

    Thread Starter
    New Member
    Join Date
    Apr 2006
    Posts
    10

    Re: VBA and Word

    DKenny...you are the man, sorry about that brainfart......

    would it be something similar for checking length of textboxes?

    Once this done, all I have to worry about is attaching this to the email
    Attached Images Attached Images  

  11. #11
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: VBA and Word

    Sure, you would again use the on_exit macro and the code would be something like...
    VB Code:
    1. Sub txtSomeFieldExit()
    2.    
    3.     If Len(ThisDocument.FormFields("txtSomeField").Result) = 0 Then
    4.         MsgBox "This field is required"
    5.     End If
    6.  
    7. End Sub
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  12. #12
    Lively Member New2vba's Avatar
    Join Date
    Sep 2005
    Location
    UK
    Posts
    95

    Re: VBA and Word

    Unless I'm missing something Declan, your code is setting checkbox values as opposed to disabling them.

    Shouldn't it be something like this?

    VB Code:
    1. Sub FormComboChange()
    2. Dim sOption As String
    3.    
    4.     sOption = ThisDocument.FormFields("cboOptions").Result
    5.    
    6.     'clear current values
    7.     ThisDocument.FormFields("chkSize").CheckBox.Value = False
    8.     ThisDocument.FormFields("chkLife").CheckBox.Value = False
    9.     ThisDocument.FormFields("chkMoney").CheckBox.Value = False
    10.    
    11.     Select Case sOption
    12.         Case "Size"
    13.             ThisDocument.FormFields("chkSize").Enabled = False
    14.             ThisDocument.FormFields("chkLife").Enabled = True
    15.             ThisDocument.FormFields("chkMoney").Enabled = True
    16.         Case "Life"
    17.             ThisDocument.FormFields("chkSize").Enabled = True
    18.             ThisDocument.FormFields("chkLife").Enabled = False
    19.             ThisDocument.FormFields("chkMoney").Enabled = True
    20.         Case "Money"
    21.             ThisDocument.FormFields("chkSize").Enabled = True
    22.             ThisDocument.FormFields("chkLife").Enabled = True
    23.             ThisDocument.FormFields("chkMoney").Enabled = False
    24.     End Select
    25. End Sub
    Last edited by New2vba; Apr 26th, 2006 at 12:50 PM.
    "Those things we must learn to do, we must learn by doing" (or hope somebody else will take pity and help out )

  13. #13
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: VBA and Word

    No, they cannot.

    I hate to say it but I really think you need to reconsider the type of controls you are using on this project.
    VBA Form controls have a much richer object model and will allow for enabling/disabling and many more properties and methods that you will probably need.


    Sorry Chip, these comments are based on New2's question. I though that was you. Feel free to ignore.
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  14. #14
    Lively Member New2vba's Avatar
    Join Date
    Sep 2005
    Location
    UK
    Posts
    95

    Re: VBA and Word

    As far as I can tell, this revised version of your example works, although it may require additional code to clear checkbox values that were selected before the dropdown choice was made.

    Edit: Just realised that Declan's original code does clear values!
    Attached Files Attached Files
    Last edited by New2vba; Apr 26th, 2006 at 12:53 PM.
    "Those things we must learn to do, we must learn by doing" (or hope somebody else will take pity and help out )

  15. #15

    Thread Starter
    New Member
    Join Date
    Apr 2006
    Posts
    10

    Re: VBA and Word

    Ok you guys both rock...everything is working great...may not be pretty but its what they want......

    I have a command button that attaches this document to an email....what is the command to change the To: and Subject: fields? We use Lotus so that may be a problem and probably wont be able to be too much help, cause I know lotus is a tough one.

    Oh and is there any coding to prevent this form from being saved?

    Thanks again!!!! I owes yas a beer

  16. #16
    Lively Member New2vba's Avatar
    Join Date
    Sep 2005
    Location
    UK
    Posts
    95

    Re: VBA and Word

    Don't know of any way to prevent a save altogether.

    Perhaps you can use the DocumentBeforeSave event to display a message box warning the user not to save a copy of the document?
    "Those things we must learn to do, we must learn by doing" (or hope somebody else will take pity and help out )

  17. #17

    Thread Starter
    New Member
    Join Date
    Apr 2006
    Posts
    10

    Re: VBA and Word

    yeah that is what I have there now...

    Any ideas on the email subject/to: fields?


    oh crap this code doesnt seem to be playing nice....any ideas?

    Code:
    Sub sec4_drop_policy1()
    Dim sOption As String
        
        sOption = ActiveDocument.FormFields("sec4_drop_policy1").Result
        
        'clear current values
        ActiveDocument.FormFields("sec5_elig_e3_1").CheckBox.Value = False
        ActiveDocument.FormFields("sec5_claim1").CheckBox.Value = False
        ActiveDocument.FormFields("sec5_banking1").CheckBox.Value = False
        
        Select Case sOption
            Case "CES"
                ActiveDocument.FormFields("sec5_elig_e3_1").Enabled = True
                ActiveDocument.FormFields("sec5_claim1").Enabled = True
                ActiveDocument.FormFields("sec5_banking1").Enabled = True
            Case "PRIME"
                ActiveDocument.FormFields("sec5_elig_e3_1").Enabled = False
                ActiveDocument.FormFields("sec5_claim1").Enabled = False
                ActiveDocument.FormFields("sec5_banking1").Enabled = False
            Case "COSMOS"
                ActiveDocument.FormFields("sec5_elig_e3_1").Enabled = True
                ActiveDocument.FormFields("sec5_claim1").Enabled = True
                ActiveDocument.FormFields("sec5_banking1").Enabled = True
        End Select
    End Sub
    Last edited by ChiPhiZD; Apr 26th, 2006 at 01:20 PM.

  18. #18
    Lively Member New2vba's Avatar
    Join Date
    Sep 2005
    Location
    UK
    Posts
    95

    Re: VBA and Word

    What's wrong with the code?
    "Those things we must learn to do, we must learn by doing" (or hope somebody else will take pity and help out )

  19. #19

    Thread Starter
    New Member
    Join Date
    Apr 2006
    Posts
    10

    Re: VBA and Word

    ahhh just saw it and fixed it...there was no ":" after the case

  20. #20
    Lively Member New2vba's Avatar
    Join Date
    Sep 2005
    Location
    UK
    Posts
    95

    Re: VBA and Word

    Works fine on my machine without the colon, but anyway, glad your problems resolved.
    "Those things we must learn to do, we must learn by doing" (or hope somebody else will take pity and help out )

  21. #21

    Thread Starter
    New Member
    Join Date
    Apr 2006
    Posts
    10

    Re: VBA and Word

    Yeah now it works w/o it, who know.....

    Can anyone explain the DocumentBeforeSave event to me a bit better...like where I would put it, what exactly to use I have seen this:

    But in actuality, I'm not totally sure what it means.

    Anyway to include this coding in my command button, or where I would put when they are trying to save?

    Code:
    Public WithEvents appWord as Word.Application
    
    Private Sub appWord_DocumentBeforeSave _
            (ByVal Doc As Document, _
            SaveAsUI As Boolean, _
            Cancel As Boolean)
    
        Dim intResponse As Integer
    
        intResponse = MsgBox("Do you really want to " _
            & "save the document?", _
            vbYesNo)
    
        If intResponse = vbNo Then Cancel = True
    End Sub

Posting Permissions

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



Click Here to Expand Forum to Full Width