微软Word自动化在C#中 - 无法转换类型的对象System.String [*]'输入'System.String []'
我用这个code获得标题的String数组在MS Word 2007文档中使用(.DOCX):
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);
使用调试器,我看到改编
动态分配的String数组我的文档(约40项)中的所有标题的标题。到目前为止好。
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:
通过指数:
String arr_elem = arr[1];
通过转换成一个IEnumerable
By casting to an IEnumerable:
IEnumerable list = (IEnumerable)arr;
通过使用一个简单的foreach循环:
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.
谁能解释我在这里缺少什么/我在做什么错了?特别是的String [*]
- ?这是什么意思
Can anyone explain what I am missing here / what I am doing wrong? And especially String[*]
- what does it mean?
尝试
object arr_r = Document.GetCrossReferenceItems(WdReferenceType.wdRefTypeHeading);
Array arr = ((Array) (arr_r));
string myHeading = (string) arr.GetValue(1);