首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Silverlight 4中使用ComAutomationFactory迭代Word文档字段

在Silverlight 4中使用ComAutomationFactory迭代Word文档字段
EN

Stack Overflow用户
提问于 2009-12-29 19:39:30
回答 2查看 6.3K关注 0票数 4

更新:这是Silverlight 4测试版中的一个确认错误。http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=523052

我通过切换到一个成熟的WPF应用程序并使用常规的旧Microsoft.Office.Interop.Word来解决这个问题。但是,我仍然对如何使用ComAutomationFactory的动态值来工作非常感兴趣。

这可能更像是一个C# 4.0问题,但我想要做的是利用可信的SL4应用程序中的ComAutomationFactory类加载一个Word文档,修改一些文本,然后打印出来。

使用常规的windows应用程序非常容易:

代码语言:javascript
复制
  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中,您必须使用dynamic关键字。它可以正常工作,直到我尝试迭代我的字段:

代码语言:javascript
复制
    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类型的,我不能对它们进行迭代。

有人提到,我应该尝试将字段作为字符串从成员处获取。

代码语言:javascript
复制
        for (int i = 0; i < oWordDoc.Fields.Count; i++)
        {
            String field = oWordDoc.Fields.Item[i].Result.Text;
        }

这导致了一个有趣的例外: HRESULT: 0x800A16E6,当谷歌搜索时,绝对不会带来任何内容。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2010-01-01 16:50:08

这当然不是C#问题-- VB.NET也有同样的问题。这里要么有bug,要么没有文档,但在这两种情况下,似乎都不可能声明集合对象。

但是,还有另一种方法,即访问集合的各个成员。这里有一个示例(在VB.NET中),它允许您通过Fields.Item进行迭代。(这里没有检查或关闭Word的错误;我的.dotx有两个字段: 1)日期和2)作者)。

代码语言:javascript
复制
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
票数 4
EN

Stack Overflow用户

发布于 2010-01-06 18:18:30

可能与Silverlight 4.0的ComAutomationFactory实现有关。我没有VS2K10 Beta 2,所以不能检查。

在控制台应用程序中,使用“动态”类型很好.

代码语言:javascript
复制
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);
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/1976503

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档