迭代Silverlight 4中使用ComAutomationFactory Word文档域 [英] Iterating over Word Document Fields using ComAutomationFactory in Silverlight 4

查看:181
本文介绍了迭代Silverlight 4中使用ComAutomationFactory Word文档域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更新:这是在Silverlight 4 beta中已确认的错误。的http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=523052

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

我通过切换到一个完全成熟的WPF应用程序,并使用普通的旧的Microsoft.Office.Interop.Word解决了这个问题。但我还是很感兴趣的是如何得到这个从ComAutomationFactory使用动态值工作。

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.

这可能更多的是C#4.0的问题,但我想要做的是利用在受信任SL4应用程序的ComAutomationFactory类加载了一个Word文档,改变一些文字,并打印出来。

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.

使用常规的Windows应用程序,这是pretty的方便:

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)

不过,在SL4你必须使用动态关键字。它正常工作,直到我试图遍历我的领域:

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)

在这种情况下,我得到一个运行时错误说我不能隐式ComAutomationMetaObjectProvider转换为IEnumerable的。无论我做什么,与我的Word COM对象的任何属性的类型ComAutomationMetaObjectProvider的,我不能在它们之间迭代。

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;
        }

这导致了一个有趣的例外:HRESULT:0x800A16E6,当Google搜索,带来了绝对没有

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

推荐答案

这当然不是一个C#的问题 - VB.NET有同样的问题。可能出现了不记录在这里的错误或什么的,但在这两种情况下,这似乎是不可能申报集合对象。

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.

周围有这样的另一种方式但是,它是访问集合的单个成员。下面是一个示例(在VB.NET),使您可以通过 Fields.Item 遍历。 (没有错误检查或关闭字在这里倒;我.DOTX有两个字段 - 1)日期; 2)作者)

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

这篇关于迭代Silverlight 4中使用ComAutomationFactory Word文档域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆