COMException when editing range and HRESULT: 0x80010105

J

joniba

Hi all, I'm not sure if this is the right place to post this, but:

I am trying to work with the PIA for microsoft word from VS2008, framework
3.5. I am working in a class library with a reference to the Office 12 PIA.
I have Office 2003 (only) installed.

I am getting a lot of exceptions I do not understand and I cannot find
documentation to explain, so any help would be greatly appreciated (as well
as documentation, if anyone knows of any).

First, trying to edit a range is always throwing an exception, whether it in
this format (I'm just pasting some of the relevant lines from the code):

oTable = doc.Tables[iTable]
oRange = oTable.Rows[iRow].Cells[3].Range;
oRange.Text = ""; // exception thrown here

Or if the last line is written like this instead:

object count = oRange.Characters.Count;
object charUnit = WdUnits.wdCharacter;
oRange.Delete(ref charUnit , ref count); // throws exception

In the second case the exception message is:

"Cannot edit Range."

In the first case I get HRESULT: 0x80010105 exception where the code stack
shows that the exception occurred in "set_Range" method in the RPC server.


Here are two more examples that return this same HRESULT: 0x80010105:

The last line in this code that is working with a Document word object
(called "doc") throws an exception:

Range oRange = doc.Content;
oRange.Find.ClearFormatting();
oRange.Find.MatchWildcards = true;
oRange.Find.ParagraphFormat.Alignment =
WdParagraphAlignment.wdAlignParagraphCenter; // throws exception

The following line also throws an exception:

oTable.Cell(iSide1Row, 2).Range.Text = strOriginal;

Where I've checked that both "strOriginal" and "oTable.Cell(iSide1Row,
2).Range.Text" evaluate to proper strings.

Tell me if you need to see more code.
 
J

Jean-Guy Marcil

joniba said:
Hi all, I'm not sure if this is the right place to post this, but:

I am trying to work with the PIA for microsoft word from VS2008, framework
3.5. I am working in a class library with a reference to the Office 12 PIA.
I have Office 2003 (only) installed.

I am getting a lot of exceptions I do not understand and I cannot find
documentation to explain, so any help would be greatly appreciated (as well
as documentation, if anyone knows of any).

First, trying to edit a range is always throwing an exception, whether it in
this format (I'm just pasting some of the relevant lines from the code):

oTable = doc.Tables[iTable]
oRange = oTable.Rows[iRow].Cells[3].Range;
oRange.Text = ""; // exception thrown here

Or if the last line is written like this instead:

object count = oRange.Characters.Count;
object charUnit = WdUnits.wdCharacter;
oRange.Delete(ref charUnit , ref count); // throws exception

In the second case the exception message is:

"Cannot edit Range."

In the first case I get HRESULT: 0x80010105 exception where the code stack
shows that the exception occurred in "set_Range" method in the RPC server.


Here are two more examples that return this same HRESULT: 0x80010105:

The last line in this code that is working with a Document word object
(called "doc") throws an exception:

Range oRange = doc.Content;
oRange.Find.ClearFormatting();
oRange.Find.MatchWildcards = true;
oRange.Find.ParagraphFormat.Alignment =
WdParagraphAlignment.wdAlignParagraphCenter; // throws exception

The following line also throws an exception:

oTable.Cell(iSide1Row, 2).Range.Text = strOriginal;

Where I've checked that both "strOriginal" and "oTable.Cell(iSide1Row,
2).Range.Text" evaluate to proper strings.

I have not worked with VS2008, but it seems to me that the errors you get
are probabaly caused by your code trying to act on obejcts the code cannot
modify.

For example, you have a few examples involving table cells. All table cells
include a cell marker "¤". This marker cannot be removed or replaced. If your
code is trying to delete such a marker, you may have problems. Try making
sure that the cell marker (always the last character in a cell range) is not
part of your range objects.

Also, "oRange.Delete(ref charUnit , ref count)" will delete whatever (ref
charUnit , ref count) represents. If oRange is not collapsed, oRange will be
deleted as well. You either need to collapse oRange to the end, or need to
debug your code and see exactly what range you are trying to delete. For
example, you cannot delete the last ¶ in the document, ¶'s before or after
tables may be problematic... But, really, it seems that you are setting "ref
charUnit , ref count" to be exaclty whatever oRange is, why not just use
"oRange.Delete()" instead of the 3 lines of code you have?

I am not sure why you are getting an error with the Find part, but try using
constant values intead of constant names... For instance,
"wdAlignParagraphCenter" is 1. So, try:
oRange.Find.ParagraphFormat.Alignment = 1
 
J

joniba

Hello Marcil, thanks for the quick response,
"Reply" doesn't seem to work in these forums when logged in with my account,
so I am using a friend's.

Responses inline:

Jean-Guy Marcil said:
I have not worked with VS2008, but it seems to me that the errors you get
are probabaly caused by your code trying to act on obejcts the code cannot
modify.

For example, you have a few examples involving table cells. All table cells
include a cell marker "¤". This marker cannot be removed or replaced. If your
code is trying to delete such a marker, you may have problems. Try making
sure that the cell marker (always the last character in a cell range) is not
part of your range objects.

I have checked the value of Range.Text before trying to set it to
string.Empty. The value is always a short text followed by "\r\a". I'm
assuming that's not the symbol you mean. I tried deleting all but the last
character and still the same error.
Also, "oRange.Delete(ref charUnit , ref count)" will delete whatever (ref
charUnit , ref count) represents. If oRange is not collapsed, oRange will be
deleted as well. You either need to collapse oRange to the end, or need to
debug your code and see exactly what range you are trying to delete. For
example, you cannot delete the last ¶ in the document, ¶'s before or after
tables may be problematic... But, really, it seems that you are setting "ref
charUnit , ref count" to be exaclty whatever oRange is, why not just use
"oRange.Delete()" instead of the 3 lines of code you have?

There is no other overload method available to me. Trying to call Delete()
without parameters doesn't compile.
I am not sure why you are getting an error with the Find part, but try using
constant values intead of constant names... For instance,
"wdAlignParagraphCenter" is 1. So, try:
oRange.Find.ParagraphFormat.Alignment = 1

Setting the Alignment to an int value as you suggest doesn't compile (no
implicit conversion exists etc. etc.).

I'd really appreciate any other suggestions, because if I cannot get around
these seemingly small problems I will have at least 3 weeks of code-rewriting
to do (to use a different interface than Word for what I need).
 
J

joniba

In case anyone else has had this problem, by absolute luck we discovered
that adding:


oRange.Select();


Before the:


oRange.Text = "";


Fixes that part of the bizarre problem.

Now I just need to figure out why



oRange.Find.ParagraphFormat.Alignment =
WdParagraphAlignment.wdAlignParagraphCenter;
 
J

Jean-Guy Marcil

joniba said:
In case anyone else has had this problem, by absolute luck we discovered
that adding:


oRange.Select();


Before the:


oRange.Text = "";


Fixes that part of the bizarre problem.

I guess that this is a C# problem because the following code works as
expected in VBA:

Dim doc As Document
Dim oTable As Table
Dim oRange As Range
Dim iRow As Long

Set doc = ActiveDocument
iRow = 2

Set oTable = doc.Tables(1)
Set oRange = oTable.Rows(iRow).Cells(3).Range
oRange.Text = ""
 

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