6

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

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
Norm
  • 61
  • 1
  • 1
  • 2

4 Answers4

2

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();
Jay
  • 75
  • 2
  • 8
  • 1
    In my experience, the Update() call is not necessary. In fact, for the purposes of this question, nothing after the third line is necessary (unless you want to close Word). – Gullbyrd Jan 27 '12 at 18:21
1

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";
0

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.

Ondrej Tucny
  • 27,626
  • 6
  • 70
  • 90
  • Thanks for the try, Ondrej, but that produced the following error: "Error 1 The best overloaded method match for 'Microsoft.Office.Interop.Word.Variables.this[ref object]' has some invalid argument." – Norm Sep 05 '10 at 18:41
0

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();
Alex Mendez
  • 5,120
  • 1
  • 25
  • 23