使用 C# 填充 Word docx 中的 docvariable
我已经在 VB 6 中完成了一百次,但使用 C# 2008 和 Word 2007 却让我发疯。我创建了一个包含两个 doc 变量的 docx 文件:
Some text here....
{docvariable replace1}
{docvariable replace2}
More text here......
我首先创建了一个宏来执行此操作,它可以工作:
Sub FillDocVariable()
'
' FillDocVariable Macro
'
'
ActiveDocument.Variables("replace1").Value = "This is a test"
ActiveDocument.Variables("replace2").Value = "it is only a test."
ActiveDocument.Fields.Update
End Sub
这是我的 C# 代码(注意你我正在学习这个):
using Microsoft.Office.Interop.Word;
object paramMissing = Type.Missing;
object openfileName = @"C:\testing\Documents\1.docx";
ApplicationClass WordApplication = new ApplicationClass();
Document WordDocument = WordApplication.Documents.Open(ref openfileName,
ref paramMissing, ref paramMissing, ref paramMissing,
ref paramMissing, ref paramMissing, ref paramMissing,
ref paramMissing, ref paramMissing, ref paramMissing,
ref paramMissing, ref paramMissing, ref paramMissing,
ref paramMissing, ref paramMissing, ref paramMissing);
WordDocument.Variables("replace1") = "This is a test";
WordDocument.Variables("replace2").Value = "it's only a test!";
WordDocument.Fields.Update;
这是我得到的错误:
错误 1 不可调用的成员 'Microsoft.Office.Interop.Word._Document.Variables' 不能像方法一样使用。 块引用
I've done this a hundred times in VB 6 but it's driving me nuts using C# 2008 and Word 2007. I created a docx file with two docvariables:
Some text here....
{docvariable replace1}
{docvariable replace2}
More text here......
I created a macro first to do it and it works:
Sub FillDocVariable()
'
' FillDocVariable Macro
'
'
ActiveDocument.Variables("replace1").Value = "This is a test"
ActiveDocument.Variables("replace2").Value = "it is only a test."
ActiveDocument.Fields.Update
End Sub
Here's my C# code (mind you I'm learning this as I go):
using Microsoft.Office.Interop.Word;
object paramMissing = Type.Missing;
object openfileName = @"C:\testing\Documents\1.docx";
ApplicationClass WordApplication = new ApplicationClass();
Document WordDocument = WordApplication.Documents.Open(ref openfileName,
ref paramMissing, ref paramMissing, ref paramMissing,
ref paramMissing, ref paramMissing, ref paramMissing,
ref paramMissing, ref paramMissing, ref paramMissing,
ref paramMissing, ref paramMissing, ref paramMissing,
ref paramMissing, ref paramMissing, ref paramMissing);
WordDocument.Variables("replace1") = "This is a test";
WordDocument.Variables("replace2").Value = "it's only a test!";
WordDocument.Fields.Update;
Here's the error I get:
Error 1 Non-invocable member
'Microsoft.Office.Interop.Word._Document.Variables'
cannot be used like a method.
Blockquote
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您有兴趣,可以通过 VS 2010 & 执行此操作的方法2010年的Word如下:
If you're interested, the way to do this via VS 2010 & Word 2010 is as follows:
我认为您在代码中错过了“.value”...
应该写为:
I think that you missed a ".value" in your code...
should be written as:
第一个猜测:
WordDocument.Variables("replace1")
→WordDocument.Variables["replace1"]
。在 MSDN 中找到后更新:显然,索引器是一个 ref 参数 - 请参阅 MSDN。所以,你必须使用这样的变量:
Strange。也许这样的 API 设计是有原因的。
另外,由于索引器没有定义 setter,因此分配将不起作用。您必须操作 getter 返回的
Variable
实例的内部结构。The first guess:
WordDocument.Variables("replace1")
→WordDocument.Variables["replace1"]
.Update after finding it in MSDN: Apparently, the indexer is a ref parameter — see MSDN. So, you have to use a variable like this:
Strange. Perhaps there's a reason for such an API design.
Also, since the indexer does not define a setter, an assignment won't work. You would have to manipulate the internals of the
Variable
instance returned by the getter.试试这个:
Try this: