views:

831

answers:

3

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  A: 

I don't know the answer to your question, but just wanted to point out that in C# 4, you shouldn't need the ref oMissing parameters.

Dennis Palmer
Yeah, I ran across that today, it's very nice.
James
+5  A: 

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
Otaku
Interesting that you mention that. I tried the same thing, but ended up getting a different error message, something rather generic. I'll update the question.
James
If you tried to set wordDoc.Fields.Item(i).Result.Text using 'dynamic' in C# or Dim As Object in VB, you'd get the same error you mentioned above. Dimensioning it as a String though works. So I could also say Dim sixthField as String = wordDoc.Fields.Item(6).Result.Text. Oh, one other thing - when you say "It works fine until I try to iterate over my fields", it actually breaks on "dynamic fields = oWordDoc.Fields;" because that is when fields is assigned to a ComAutomationMetaObjectProvider type. I've tried things like "Dim fields as Ienumerable(of Object)", but that fails too.
Otaku
I still get the exception HRESULT: 0x800A16E6 when trying to access it via Fields.Item[i].Result.Text. Of course, I could be mistranslating my VB to C#. I've updated the question.
James
Try setting oWordDoc.Fields.Count to an integer variable like "int fieldsCount = oWordDoc.Fields.Count;" and using that in the "for (int i = 0; i < fieldsCount; i++) " statement.
Otaku
Same thing. It just does not want me to access the collection members. I've submitted a bug report to Microsoft on this since it's interesting. https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=523052
James
oh man, i'm sorry to hear that. if i knew anything at all about C#, i'd try testing it there on my box for you.
Otaku
+1  A: 

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);
Grynn