none
Calling a macro in MS Word not working correctly RRS feed

  • Question

  • i have a word doc that has a table with some check boxes and text fields:

    Event

    Frequency

    Event

    Frequency

     Break-Ins

        

     Arrests

        

     Vandalism

        

     Drug Activity

        

     Auto Theft

        

     Other (please specify):        

        

     Personal Assaults

        

     None

    The last check box is the "none", and when the user checks 'none' all the other checkboxes should be disabled, so they are not selected by mistake.  I wrote a macro that when the user checks 'none' it goes and disables the other checkboxes and text fields (it's called in the 'run macro on entry'), this works great, but if the user decides to uncheck 'none' I need the other checkboxes to become enabled.

    The macro is a IF statment, so if 'none' is check it disables and if unchecked is enables.  The only way I can get the enable to work is if I run the macro from the 'Run macro on EXIT', but that requires the user to uncheck 'none' and click in a field that is not in the table.

    How can I get the disable and enable to work when running the macro from the 'on entry'?

    Below is the macro


    Sub CheckForNone()

    Dim objfield As Object

    If ActiveDocument.FormFields(Selection.FormFields(1).Name).CheckBox.Value = True Then
        
        For Each objfield In ActiveDocument.FormFields
           
            If objfield.Type = wdFieldFormTextInput Then
               If objfield.Name = "tfreq2a1" Or _
                  objfield.Name = "tfreq2a2" Or _
                  objfield.Name = "tfreq2a3" Or _
                  objfield.Name = "tfreq2a4" Or _
                  objfield.Name = "tfreq2a5" Or _
                  objfield.Name = "tfreq2a6" Or _
                  objfield.Name = "tfreq2a7" Or _
                  objfield.Name = "tother2a" Then
                     objfield.Result = ""
                     objfield.Enabled = False
               End If
              
            End If
            If objfield.Type = wdFieldFormCheckBox Then
               If objfield.Name = "c2a1" Or _
                  objfield.Name = "c2a2" Or _
                  objfield.Name = "c2a3" Or _
                  objfield.Name = "c2a4" Or _
                  objfield.Name = "c2a5" Or _
                  objfield.Name = "c2a6" Or _
                  objfield.Name = "c2a7" Then
                    objfield.Result = False
                    objfield.Enabled = False
               End If
            End If
           
       
        Next objfield
    Else

        For Each objfield In ActiveDocument.FormFields
            If objfield.Type = wdFieldFormTextInput Then
               If objfield.Name = "tfreq2a1" Or _
                  objfield.Name = "tfreq2a2" Or _
                  objfield.Name = "tfreq2a3" Or _
                  objfield.Name = "tfreq2a4" Or _
                  objfield.Name = "tfreq2a5" Or _
                  objfield.Name = "tfreq2a6" Or _
                  objfield.Name = "tfreq2a7" Or _
                  objfield.Name = "tother2a" Then
                     objfield.Enabled = True
              End If
            End If
           
            If objfield.Type = wdFieldFormCheckBox Then
               If objfield.Name = "c2a1" Or _
                  objfield.Name = "c2a2" Or _
                  objfield.Name = "c2a3" Or _
                  objfield.Name = "c2a4" Or _
                  objfield.Name = "c2a5" Or _
                  objfield.Name = "c2a6" Or _
                  objfield.Name = "c2a7" Then
                     objfield.Enabled = True
               End If
            End If
       
        Next objfield
    End If

    End Sub

    Thanks in advance

    Dan Plocica


    Dan1104

    Wednesday, January 23, 2013 9:12 PM

Answers

  • Hi Dan

    Tom's answer is assuming you have Word 2010 or 2013 and are using content controls as "fields".

    I can see from your code (and your problem description) that you're using form fields. What Tom suggests can't work for you, unless you're able/willing to change from form fields to checkboxes.

    Even then, Tom's suggestion has the same drawbacks as what you're currently doing. Neither form fields nor content controls have "Click" events, only events that trigger when the user enters/exist the controls. This is by design and there's no getting around it.

    Your options are to either use an ActiveX control for the checkbox, or a MacroButton field. If the ActiveX control works, that will be the simpler solution to code, but you'll want to test how it looks and prints in your document before you decide to use it.

    Before we can provide more detailed help, should you require it, we do need to know which version of Word we're dealing with.


    Cindy Meister, VSTO/Word MVP, my blog

    Thursday, January 24, 2013 10:34 AM
    Moderator
  • Thanks Cindy,

    I have word 2010 and I'm using form fields,and I don't want to change them. I thought this was probley a design issue,  I figured out a work-around. when the user clicks 'none' it unchecks the other chckboxes and if the re-check a checkbos, it will uncheck the 'none'.

    Thanks for yor help

    Dan Plocica


    Dan1104

    • Marked as answer by Dan1104 Monday, January 28, 2013 1:41 PM
    Monday, January 28, 2013 1:41 PM

All replies

  • Hi Dan1104,

    Thanks for posting in the MSDN Forum.

    As usual, we will insert content control in word document to approach the goal. And based on my experience that you don't do it like that. Would you please clarify it?

    The following code will work for your goal if I don't misunderstand your will:

    Option Explicit
    
    Private Sub Document_ContentControlOnEnter(ByVal ContentControl As ContentControl)
        Dim r, c As Integer
        Dim Cel As Word.range
        Dim t As String
        
        If ContentControl.Title = "None" And ContentControl.Checked Then
            For r = 1 To ActiveDocument.Tables(1).Rows.Count
                For c = 1 To ActiveDocument.Tables(1).Columns.Count
                    Set Cel = ActiveDocument.Tables(1).Cell(r, c).range
                    If Cel.ContentControls(1).Title <> "None" Then
                        Cel.ContentControls(1).LockContentControl = False
                        Cel.ContentControls(1).Delete (True)
                    End If
                Next
            Next
        ElseIf ContentControl.Title = "None" And Not ContentControl.Checked Then
            For r = 1 To ActiveDocument.Tables(1).Rows.Count
                For c = 1 To ActiveDocument.Tables(1).Columns.Count
                    Set Cel = ActiveDocument.Tables(1).Cell(r, c).range
                    t = Replace(Cel.Text, Chr(13), "")
                    If Cel.ContentControls.Count = 0 Then
                        Cel.Select
                        Application.Selection.range.ContentControls.Add wdContentControlCheckBox
                        Application.Selection.MoveRight Unit:=wdCharacter, Count:=2
                        Application.Selection.range.Text = t
                    End If
                Next
            Next
        End If
    End Sub

    I hop it can help you,

    Tom


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.


    Thursday, January 24, 2013 4:48 AM
    Moderator
  • Hi Dan

    Tom's answer is assuming you have Word 2010 or 2013 and are using content controls as "fields".

    I can see from your code (and your problem description) that you're using form fields. What Tom suggests can't work for you, unless you're able/willing to change from form fields to checkboxes.

    Even then, Tom's suggestion has the same drawbacks as what you're currently doing. Neither form fields nor content controls have "Click" events, only events that trigger when the user enters/exist the controls. This is by design and there's no getting around it.

    Your options are to either use an ActiveX control for the checkbox, or a MacroButton field. If the ActiveX control works, that will be the simpler solution to code, but you'll want to test how it looks and prints in your document before you decide to use it.

    Before we can provide more detailed help, should you require it, we do need to know which version of Word we're dealing with.


    Cindy Meister, VSTO/Word MVP, my blog

    Thursday, January 24, 2013 10:34 AM
    Moderator
  • Thanks Cindy,

    I have word 2010 and I'm using form fields,and I don't want to change them. I thought this was probley a design issue,  I figured out a work-around. when the user clicks 'none' it unchecks the other chckboxes and if the re-check a checkbos, it will uncheck the 'none'.

    Thanks for yor help

    Dan Plocica


    Dan1104

    • Marked as answer by Dan1104 Monday, January 28, 2013 1:41 PM
    Monday, January 28, 2013 1:41 PM