使用 C# 填充 Word docx 中的 docvariable

发布于 2024-09-17 23:11:01 字数 1437 浏览 3 评论 0原文

我已经在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

牵你的手,一向走下去 2024-09-24 23:11:01

如果您有兴趣,可以通过 VS 2010 & 执行此操作的方法2010年的Word如下:

Application app = new Application();
Document doc = word.Documents.Add(filepath);
doc.Variables["var_name"].Value = your_value_here;
doc.Fields.Update();
doc.Save();
doc.Close();
app.Quit();

If you're interested, the way to do this via VS 2010 & Word 2010 is as follows:

Application app = new Application();
Document doc = word.Documents.Add(filepath);
doc.Variables["var_name"].Value = your_value_here;
doc.Fields.Update();
doc.Save();
doc.Close();
app.Quit();
青衫负雪 2024-09-24 23:11:01

我认为您在代码中错过了“.value”...

WordDocument.Variables("replace1") = "This is a test";

应该写为:

WordDocument.Variables("replace1").Value = "This is a test";

I think that you missed a ".value" in your code...

WordDocument.Variables("replace1") = "This is a test";

should be written as:

WordDocument.Variables("replace1").Value = "This is a test";
神仙妹妹 2024-09-24 23:11:01

第一个猜测:WordDocument.Variables("replace1")WordDocument.Variables["replace1"]

在 MSDN 中找到后更新:显然,索引器是一个 ref 参数 - 请参阅 MSDN。所以,你必须使用这样的变量:

string replace = "replace1";
WordDocument.Variables[ref replace] = ...;

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:

string replace = "replace1";
WordDocument.Variables[ref replace] = ...;

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.

昔梦 2024-09-24 23:11:01

试试这个:

object variable1 = "This is a test";
object variable2 = "it's only a test!";
Variable var1 = WordDocument.Variables.Add("replace1", ref variable1);
Variable var2 = WordDocument.Variables.Add("replace2", ref variable1);
WordDocument.Fields.Update();

Try this:

object variable1 = "This is a test";
object variable2 = "it's only a test!";
Variable var1 = WordDocument.Variables.Add("replace1", ref variable1);
Variable var2 = WordDocument.Variables.Add("replace2", ref variable1);
WordDocument.Fields.Update();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文