PDA

View Full Version : Fluky behaviour in CC.Add method



gmaxey
04-05-2014, 08:40 AM
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

snb
04-05-2014, 01:15 PM
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

gmaxey
04-05-2014, 01:48 PM
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.

fumei
04-06-2014, 03:22 AM
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?

gmaxey
04-06-2014, 07:00 AM
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

snb
04-06-2014, 12:17 PM
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

gmaxey
04-06-2014, 01:23 PM
Your point is?

fumei
04-06-2014, 04:13 PM
Wow, I wonder why snb even bothers. What a pointless piece of code.

gmaxey
04-06-2014, 05:30 PM
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?

fumei
04-06-2014, 06:18 PM
snicker...maybe if we read the explanation and code comments. Oh dear.

gmaxey
04-06-2014, 06:33 PM
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.

fumei
04-06-2014, 09:45 PM
And we thank you, because it is in fact noteworthy.