在Silverlight 4中使用ComAutomationFactory迭代Word文档字段
更新:这是Silverlight 4测试版的已确认的错误。 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问题, m尝试做的是在受信任的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应用程序,它很容易:
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)
您必须使用dynamic关键字。它工作正常,直到我尝试迭代我的字段:
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有同样的问题。这里有一个bug或者没有记录的东西,但是在任何一种情况下,似乎都不可能声明collection对象。
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
迭代。 (这里没有错误检查或关闭Word;我的.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