none
columns ranges help RRS feed

  • Question

  • My code basically tries to to make changes to content controls based on what another might say. Here is an example: 
    For Each ccWound In ccWounds
        If ccWound.Range.InRange(Selection.Tables(1).Range) Then
            If InStr(1, ccWound.Range.Text, "443.9") > 0 Then
                ccWound.Range.Columns.Select
                For Each ccEti1 In ccEti1s
                If ccEti1.Range.InRange(Selection.Range) Then
                ccEti1.Range.Text = "arterial ulcer"
                End If
                Next
                
                ccWound.Range.Columns.Select
                For Each ccEti2 In ccEti2s
                If ccEti2.Range.InRange(Selection.Range) Then
                ccEti2.Range.Text = " "
                End If
                Next
            End If
        End If
    Next
    The issue is that the changes never seem to adhere to the correct column. Even when I step through the code, the correct column is selected but completely disregarded. it's something missing?
    Sunday, August 19, 2012 6:35 PM

Answers

  • You still haven't specified what all those range definitions/references apply to. Nevertheless, you should be able to use something based on:

    Private Sub Document_ContentControlOnExit(ByVal ContentControl As ContentControl, Cancel As Boolean)
    With ContentControl
      If .Range.Information(wdWithInTable) = True Then
        If ActiveDocument.Bookmarks("Wounds").Range.InRange(.Range.Tables(1).Range) = True Then
          If .Range.Rows(1).Index = 1 Then
            If InStr(.Range.Text, "443.9") > 0 Then
              .Range.Columns(1).Cells(2).Range.ContentControls(1).Range.Text = "arterial ulcer"
            End If
          End If
        End If
      End If
    End With
    End Sub

    Note that nothing gets selected by the code.


    Cheers
    Paul Edstein
    [MS MVP - Word]

    • Marked as answer by tonayie Monday, August 20, 2012 10:50 PM
    Monday, August 20, 2012 5:44 AM

All replies

  • It's a bit hard to say when we don't know: what is selected (or even why you're using selections rather than defined ranges); or what 'ccWound' and ccWounds refer to; and, likewise, what 'ccEti1', 'ccEti1s', 'ccEti2'  or 'ccEti2s' refer to.

    Aside from that, if these outputs are always dependent, (ie you're not simply setting default values that a user can change), you don't need Content Controls for the output ranges at all - you can populate the corresponding table cells directly. In fact, you might  even do better to update a custom document property and simply use DOCPROPERTY fields, which you update, to display the data. That way, you don't need to directly manipulate the table content and, having updated the custom document property (once), all you'd need is a statement like Activedocument.Fields.Update.


    Cheers
    Paul Edstein
    [MS MVP - Word]

    Sunday, August 19, 2012 10:39 PM
  • The outputs are not always dependent, I think content controls might actually would work best for my case. Below is a sample of what i am working with. The 2,3, & 4th  columns are all populated with content controls.

    I feel like this whole issue could be abated by simply knowing how to select column as a range. I've have tried with cells, but it seems to select more than i would like. I have tried .Range.Information(wdEndofRangeColumnNumber). This worked well, but in large documents, slowed the code considerably. 


    • Edited by tonayie Monday, August 20, 2012 9:40 AM don't like my documents floating around on the internet.
    Monday, August 20, 2012 12:01 AM
  • You still haven't specified what all those range definitions/references apply to. Nevertheless, you should be able to use something based on:

    Private Sub Document_ContentControlOnExit(ByVal ContentControl As ContentControl, Cancel As Boolean)
    With ContentControl
      If .Range.Information(wdWithInTable) = True Then
        If ActiveDocument.Bookmarks("Wounds").Range.InRange(.Range.Tables(1).Range) = True Then
          If .Range.Rows(1).Index = 1 Then
            If InStr(.Range.Text, "443.9") > 0 Then
              .Range.Columns(1).Cells(2).Range.ContentControls(1).Range.Text = "arterial ulcer"
            End If
          End If
        End If
      End If
    End With
    End Sub

    Note that nothing gets selected by the code.


    Cheers
    Paul Edstein
    [MS MVP - Word]

    • Marked as answer by tonayie Monday, August 20, 2012 10:50 PM
    Monday, August 20, 2012 5:44 AM
  • Thanks. what would be the easiest way to do it for each column in the table, depending on how many columns that there are? Assuming that it isn't known before hand (it could be 2 columns, or 3, or 4).
    • Edited by tonayie Monday, August 20, 2012 9:29 AM
    Monday, August 20, 2012 9:29 AM
  • That suggests you haven't tried adjusting the code to work with your document. It already acts on whichever column the triggering content control (assumed to be on row 1 for the purposes of illustration) is in. Consequently, you already don't need to know how many columns there are. As coded, all it requires is that the table has the bookmark 'Wounds' somewhere in its range - you don't even need to bookmark the entire table.

    Cheers
    Paul Edstein
    [MS MVP - Word]

    Monday, August 20, 2012 9:36 AM
  • you're right, i haven't. I definitely will before any other replies. I am deeply grateful, once again.

    edit: well, it worked and very well at that.

    but I don't think that this exact method is for me. First these tables are gonna be copied and pasted a lot and i don't think that these bookmarks are gonna copy with them. I guess the alternative is to create bookmarks each time the code is run and delete them afterwords, though this seems like an inefficient way of handling things. secondly, i don't feel comfortable handling with the onexit event in a document with as many content controls as this. I think that i will mark your answer as correct but for now concede defeat here.

    • Edited by tonayie Monday, August 20, 2012 10:50 PM
    Monday, August 20, 2012 9:39 AM