Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MS Word Automation in C# - Unable to cast object of type 'System.String[*]' to type 'System.String[]'

I use this code to get a String array of headings used in a MS Word 2007 document (.docx):

dynamic arr = Document.GetCrossReferenceItems(WdReferenceType.wdRefTypeHeading);

Using the debugger, I see that arr is dynamically assigned a String array with titles of all my headings in the document (about 40 entries). So far so good.

Then, I want to access the strings, but no matter how I do it, I get the following exception:

InvalidCastException: 
           Unable to cast object of type 'System.String[*]' to type 'System.String[]'.

I have tried different ways of accessing the strings:

By index:

String arr_elem = arr[1];

By casting to an IEnumerable:

IEnumerable list = (IEnumerable)arr;

By using a simple foreach loop:

foreach (String str in arr)
{
   Console.WriteLine(str);
}

However, no matter what I try, I always end up with the same exception as shown above.

Can anyone explain what I am missing here / what I am doing wrong? And especially String[*] - what does it mean?

like image 604
Lasse Christiansen Avatar asked Aug 16 '11 20:08

Lasse Christiansen


People also ask

Can you automate Microsoft Word?

Using a template with your Word Automation client has two significant advantages over building a document from nothing: You can have greater control over the formatting and placement of objects throughout your documents. You can build your documents with less code.

How do I automate a word in word?

Go to File > Options > Proofing, and select AutoCorrect Options. On the AutoCorrect tab, select the Replace text as you type check box, if it's not already checked. Under Replace, type the characters that you want to trigger the automatic text. The text that you selected in your document should appear under With.

Can you use C# to automate?

Using Selenium, C#, and Visual Studio together provides a unique opportunity to create a robust, scalable, and flexible automation framework.

How do I add C# code to word?

Go to Insert tab, Text section, click Object button (it's on the right) Choose OpenDocument Text which will open a new embedded word document. Copy and paste your code from Visual Studio / Eclipse inside this embedded word page.


2 Answers

string[] is a vector - a 1-d, 0-based array. string[*], however, is a regular array that just happens to have one dimension. Basically, you are going to have to handle it as Array, and either copy the data out, or use the Array API rather than the string[] API.

This is the same as the difference between typeof(string).MakeArrayType() (the vector) and typeof(string).MakeArrayType(1) (a 1-d non-vector).

like image 193
Marc Gravell Avatar answered Oct 01 '22 23:10

Marc Gravell


try

object arr_r = Document.GetCrossReferenceItems(WdReferenceType.wdRefTypeHeading);
Array arr = ((Array) (arr_r));

string myHeading = (string) arr.GetValue(1);
like image 37
Yahia Avatar answered Oct 01 '22 21:10

Yahia