Why an exception on a ContentControl add?

D

David Thielen

I call the following code where Range.Start == Range.End and it is
sitting at the end of a paragraph. There is no ContentControl anywhere
nearby. Range.ContentControls.Count == 0.

This occurs when I first delete a Field at this location, than add the
ContentControl. It appears to only be a problem when this happens at
the end of a paragraph.

object obj = rng;
ContentControl ctrl =
rng.ContentControls.Add(WdContentControlType.wdContentControlText, ref
obj);

The error I get is:

System.Runtime.InteropServices.COMException occurred
HelpLink="C:\\Program Files\\Microsoft
Office\\Office12\\1033\\WDMAIN11.CHM#37373"
Message="This method or property is not available because the
current selection partially covers a plain text content control."
Source="Microsoft Word"
ErrorCode=-2146823683
StackTrace:
at
Microsoft.Office.Interop.Word.ContentControls.Add(WdContentControlType
Type, Object& Range)
at
AutoTag2010.net.windward.autotag.word.WordTagHandler.WriteTag(WordTagLocation
tag, Image picture, ChartDataSet chartData) in
C:\src\version9\AutoTag\AutoTag2010\net\windward\autotag\word\WordTagHandler.cs:line
1465
InnerException:

??? - thanks - dave

david@[email protected]
Windward Reports -- http://www.WindwardReports.com
me -- http://dave.thielen.com

Cubicle Wars - http://www.windwardreports.com/film.htm
 
D

David Thielen

The problem seems to be that I have a AddIn command that walks through
and converts fields to ContentControls. The first one is fine, all
subsequent ones get this error.

There is a plain text control - but it's before what I am replacing.
Here's the sequence (numbers are ranges):
delete Field at 63 -> 106
insert CC at 63
CC range is 64 -> 89
delete Field at 106 -> 257
insert CC at 106 => exception

I changed the insert to be 108 so it's beyond where the 1st field was
- same exception.

??? - thanks - dave


I call the following code where Range.Start == Range.End and it is
sitting at the end of a paragraph. There is no ContentControl anywhere
nearby. Range.ContentControls.Count == 0.

This occurs when I first delete a Field at this location, than add the
ContentControl. It appears to only be a problem when this happens at
the end of a paragraph.

object obj = rng;
ContentControl ctrl =
rng.ContentControls.Add(WdContentControlType.wdContentControlText, ref
obj);

The error I get is:

System.Runtime.InteropServices.COMException occurred
HelpLink="C:\\Program Files\\Microsoft
Office\\Office12\\1033\\WDMAIN11.CHM#37373"
Message="This method or property is not available because the
current selection partially covers a plain text content control."
Source="Microsoft Word"
ErrorCode=-2146823683
StackTrace:
at
Microsoft.Office.Interop.Word.ContentControls.Add(WdContentControlType
Type, Object& Range)
at
AutoTag2010.net.windward.autotag.word.WordTagHandler.WriteTag(WordTagLocation
tag, Image picture, ChartDataSet chartData) in
C:\src\version9\AutoTag\AutoTag2010\net\windward\autotag\word\WordTagHandler.cs:line
1465
InnerException:

??? - thanks - dave

david@[email protected]
Windward Reports -- http://www.WindwardReports.com
me -- http://dave.thielen.com

Cubicle Wars - http://www.windwardreports.com/film.htm


david@[email protected]
Windward Reports -- http://www.WindwardReports.com
me -- http://dave.thielen.com

Cubicle Wars - http://www.windwardreports.com/film.htm
 
S

Scott McPhillips [MVP]

David Thielen said:
I call the following code where Range.Start == Range.End and it is
sitting at the end of a paragraph. There is no ContentControl anywhere
nearby. Range.ContentControls.Count == 0.

This occurs when I first delete a Field at this location, than add the
ContentControl. It appears to only be a problem when this happens at
the end of a paragraph.

object obj = rng;
ContentControl ctrl =
rng.ContentControls.Add(WdContentControlType.wdContentControlText, ref
obj);

The error I get is:

System.Runtime.InteropServices.COMException occurred
HelpLink="C:\\Program Files\\Microsoft
Office\\Office12\\1033\\WDMAIN11.CHM#37373"
Message="This method or property is not available because the
current selection partially covers a plain text content control."
Source="Microsoft Word"
ErrorCode=-2146823683


I don't have or delete fields, but I do frequently insert a CC that ends at
a paragraph end. I have found it necessary to select the range before
adding the CC.
 
J

Ji Zhou

Hi Dave,

We need to substract 1 from the ContentControl.Range.Start.

Codes look like,
--------------------------------------------------
object index =1;
int ccpos = doc.ContentControls[1].Start;
Debug.Print(doc.Range(ccpos - 1, ccpos - 1).Font.Name);
--------------------------------------------------

Best regards,
Ji Zhou
Microsoft Online Community Support
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top