Consulting

Results 1 to 12 of 12

Thread: Fluky behaviour in CC.Add method

  1. #1
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,340
    Location

    Fluky behaviour in CC.Add method

    It seems there is a minor bug in the CC.add method. Despite the fact that you can target a specific location to add a CC, if another CC is selected, the method fails:

    Sub CCAddBug()
    Dim oCC As ContentControl
    Dim oRng As Word.Range
    'Attempts to insert a CC as an optional range target fails if another CC has the focus (is selected)
      'Create three empty paragraphs.
      ActiveDocument.Range.Text = vbCr + vbCr
      Set oRng = ActiveDocument.Range(0, 0)
      'Add CC at oRng.
      Set oCC = ActiveDocument.ContentControls.Add(wdContentControlText, oRng)
      'Notice oCC.Range is now selected.
      Set oRng = ActiveDocument.Paragraphs(2).Range
      On Error GoTo err_Debugger
      'Since the previous CC is selected an error occurs.
      Set oCC = ActiveDocument.ContentControls.Add(wdContentControlText, oRng)
    lbl_Exit:
      Exit Sub
    err_Debugger:
      ActiveDocument.Paragraphs(3).Range.Select
      Resume
    End Sub
    Sub CCAddNoError()
    Dim oCC As ContentControl
    Dim oRng As Word.Range
      'Create three empty paragraphs.
      ActiveDocument.Range.Text = vbCr + vbCr
      Set oRng = ActiveDocument.Paragraphs(1).Range
      'Add CC at oRng.
      Set oCC = ActiveDocument.ContentControls.Add(wdContentControlText, oRng)
      'Notice oCC.Range is "NOT" selected.
      Set oRng = ActiveDocument.Paragraphs(2).Range
      'Since the previous CC is "NOT" selected there is no error.
      Set oCC = ActiveDocument.ContentControls.Add(wdContentControlText, oRng)
    lbl_Exit:
      Exit Sub
    End Sub
    Greg

    Visit my website: http://gregmaxey.com

  2. #2
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,645
    Sub M_snb()
       With ActiveDocument
            .Content = String(30, vbCr)
            With .ContentControls
                .Add 1, ActiveDocument.Paragraphs(3).Range
                .Add 1, ActiveDocument.Paragraphs(5).Range
                .Add 1, ActiveDocument.Paragraphs(7).Range
            End With
        End With
    End Sub

  3. #3
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,340
    Location
    snb,

    Other than to demonstrate once again your knack for writing concise code, what is the point of your last post?

    The issue I was hoping to illustrate is that the ContentControls.Add method will fail if another content control is selected. Since the method provides an optional range parameter that "points" to a specific range where the CC should be added, that shouldn't cause an error if that range does not cover a content control.

    However, using your much improved method, I can now illustrate that as so:

    [CODE][Sub M_snbII()
    With ActiveDocument
    .Content = String(30, vbCr)
    ActiveDocument.Range(0, 0).Select
    With .ContentControls
    .Add 1, Selection.Range
    .Add 1, ActiveDocument.Paragraphs(5).Range
    .Add 1, ActiveDocument.Paragraphs(7).Range
    End With
    End With
    End Sub/CODE]

    And as your code rarely provides error handling, the hapless user of the code will just have to figure out how to handle it themselves.
    Greg

    Visit my website: http://gregmaxey.com

  4. #4
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    And as your code rarely provides error handling, the hapless user of the code will just have to figure out how to handle it themselves.
    Rarely? Try never.

    However, that aside, and agreeing this does seem a minor bug, I have to ask why you DO make a selection of the previous CC. And more to the point, I find it very interesting that:

    Set oRng = ActiveDocument.Range(0, 0)

    selects, but:

    Set oRng = ActiveDocument.Paragraphs(1).Range

    does not. Although why using the document range (includes the CC) is different from using the paragraph range (does not include the CC), is beyond me. You are the CC expert. But yeah, it does seem like a bug. Maybe because CC are child objects of Document, not Paragraph?

  5. #5
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,340
    Location
    Gerry,

    I simply stumbled on the issue and did not necessarily intend to select the CC. Here is another example. Say I want to insert as CC at the selection and then at the next two following paragraphs:

    Sub ScratchMacro()
    'A basic Word macro coded by Greg Maxey
    With ActiveDocument
      .Content = String(30, vbCr)
      With .ContentControls
       .Add 1, Selection.Range
       .Add 1, Selection.Paragraphs(1).Next.Range
       .Add 1, Selection.Paragraphs(1).Next.Next.Range
     End With
    End With
    End Sub
    Greg

    Visit my website: http://gregmaxey.com

  6. #6
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,645
    Sub M_snb()
         With ActiveDocument.ContentControls
             Set sh3 = .Add(1, ActiveDocument.Paragraphs(3).Range)
             Set sh4 = .Add(1, ActiveDocument.Paragraphs(7).Range)
             Set sh5 = .Add(3, ActiveDocument.Paragraphs(12).Range)
    
             ActiveDocument.Paragraphs(1).Range.Select
             Set sh = .Add(1, Selection.Range)
    
             ActiveDocument.Paragraphs(5).Range.Select
             Set sh1 = .Add(1, Selection.Range)
    
             ActiveDocument.Paragraphs(11).Range.Select
             Set sh2 = .Add(3, Selection.Range)
        End With
    End Sub

  7. #7
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,340
    Location
    Your point is?
    Greg

    Visit my website: http://gregmaxey.com

  8. #8
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    Wow, I wonder why snb even bothers. What a pointless piece of code.
    Last edited by fumei; 04-06-2014 at 05:02 PM.

  9. #9
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,340
    Location
    3, 7, 12, 1, 5, 11. All but 1 will result in an unhandled error if executed in a new blank document. Maybe his point is to illustrate unhandled errors?
    Greg

    Visit my website: http://gregmaxey.com

  10. #10
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    snicker...maybe if we read the explanation and code comments. Oh dear.

  11. #11
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,340
    Location
    snb,

    Ok. I'm starting to wallow in the mud and should stop to keep from getting dirty. I think the message, subtle or direct that I and others are tying to make is that your drive by postings of short, concise, uncommented and sometimes dysfunctional code are sometimes harmless and pointless, but occasionally they do more harm than good. Your last posting adds absolutely nothing to this thread and is simply a total flop if someone runs it in a new blank document.

    The issue here is that if a document CC is selected, intentionally or unintentionally, and you then try to add another CC at a different targeted range using the .Add method an error occurs.

    Like you, while perhaps not in as few lines, I can write code in dozens of different ways to keep from having a CC selected, but that doesn't change the fact that an error occurs if and when a CC is selected.

    I posted the issues so that others could be aware of it.
    Last edited by gmaxey; 04-07-2014 at 03:45 AM.
    Greg

    Visit my website: http://gregmaxey.com

  12. #12
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    And we thank you, because it is in fact noteworthy.

Posting Permissions

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