Iterating over Word Document Fields using ComAutomationFactory in Silverlight 4

会有一股神秘感。 提交于 2020-01-02 06:27:10

问题


Update: This is a confirmed bug in Silverlight 4 beta. http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=523052

I solved this issue by switching to a full blown WPF application and using regular old Microsoft.Office.Interop.Word. But I'm still very interested in how to get this to work using the dynamic values from ComAutomationFactory.

This may be more of a C# 4.0 question, but what I'm trying to do is leverage the ComAutomationFactory class in a trusted SL4 app to load up a Word doc, change some text, and print it.

Using a regular windows app, it's pretty easy:

  Object oMissing = System.Reflection.Missing.Value;
    Object oTrue = true;
    Object oFalse = false;

    Application oWord = new Application();
    Document oWordDoc = new Document();

    oWord.Visible = false;

    object oTemplatePath = "C:\\Users\\jwest\\Desktop\\DocumentTemplate.dotx";
    oWordDoc = oWord.Documents.Add(ref oTemplatePath, ref oMissing, ref oMissing, ref oMissing);

    foreach (Field myMergeField in oWordDoc.Fields)

However, in SL4 you have to use the dynamic keyword. It works fine until I try to iterate over my fields:

    Object oMissing = System.Reflection.Missing.Value;
    Object oTrue = true;
    Object oFalse = false;

    dynamic oWord = ComAutomationFactory.CreateObject("Word.Application");

    oWord.Visible = false;

    object oTemplatePath = "C:\\Users\\jwest\\Desktop\\DocumentTemplate.dotx";
    dynamic oWordDoc = oWord.Documents.Add(ref oTemplatePath, ref oMissing, ref oMissing, ref oMissing);
    dynamic fields = oWordDoc.Fields;

    foreach (var myMergeField in fields)

In which case I get a runtime error saying that I cannot implicitly convert a ComAutomationMetaObjectProvider to IEnumerable. No matter what I do, any properties related to my Word com object are of type ComAutomationMetaObjectProvider and I cannot iterate over them.

It has been mentioned that I should try getting the field as a String from a member.

        for (int i = 0; i < oWordDoc.Fields.Count; i++)
        {
            String field = oWordDoc.Fields.Item[i].Result.Text;
        }

This results in an interesting exception of: HRESULT: 0x800A16E6 which when Googled, brings up absolutely nothing.


回答1:


It certainly isn't a C# issue - VB.NET has the same issue. There is either a bug here or something not documented, but in either case, it seems impossible to declare collection objects.

There is another way around this however, it is to access the individual members of the collection. Here's a sample (in VB.NET) that allows you to iterate through via Fields.Item. (no error checking or shutting down of Word here; my .dotx has two fields - 1) date and 2) author).

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles Button1.Click
    Dim wrd As Object = ComAutomationFactory.CreateObject("Word.Application")
    Dim path As String = "C:\Users\me\test.dotx"
    Dim wordDoc As Object = wrd.Documents.Add(path)
    Dim fieldsCount As Integer = wordDoc.Fields.Count
    Dim fieldResults As String = Nothing
    For i As Integer = 1 To fieldsCount
        fieldResults = fieldResults & " " & wordDoc.Fields.Item(i).Result.Text & vbNewLine
    Next
    TextBox1.Text = "Field Results: " & fieldResults
End Sub



回答2:


Could be something to do with Silverlight 4.0's implementation of the ComAutomationFactory. I don't have VS2K10 Beta 2 so can't check.

Using "dynamic" types works fine in a console app though ...

dynamic oWord = //ComAutomationFactory.CreateObject("Word.Application");
     Activator.CreateInstance(Type.GetTypeFromProgID("Word.Application", true)); 

oWord.Visible = false;

object oTemplatePath = "c:\\vishal.dotx";
dynamic oWordDoc = oWord.Documents.Add(ref oTemplatePath);
dynamic fields = oWordDoc.Fields;

Console.WriteLine("template has {0} merge flds", fields.Count);

//Method 1
Console.WriteLine(string.Join("\n", ((IEnumerable)oWordDoc.Fields).Cast<dynamic>().Select(x=>(string)x.Result.Text).ToArray()));

//Method 2
foreach (dynamic fld in fields)
 Console.WriteLine(fld.Result.Text);


来源:https://stackoverflow.com/questions/1976503/iterating-over-word-document-fields-using-comautomationfactory-in-silverlight-4

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!