12

I tried to Fill out Form Fields in Microsoft Word using C# Interop Assemblies with the following Code

string filename = @"N:\mehler\Vorlage2.dotx";

Microsoft.Office.Interop.Word.Application word = new Microsoft.Office.Interop.Word.Application();
Microsoft.Office.Interop.Word.Document doc = new Microsoft.Office.Interop.Word.Document();

doc = word.Documents.Open(filename);
doc.Activate();

foreach (Microsoft.Office.Interop.Word.FormField field in doc.FormFields)
{
    switch (field.Name)
    {
        case "Text2":
            field.Range.Text = "1";
            break;

        default:
            break;
    }
}

doc.SaveAs2(@"N:\mehler\Ausgefuellt.docx");
doc.Close();
word.Quit();
System.Diagnostics.Process.Start(@"N:\mehler\Ausgefuellt.docx");

Microsoft Word can not open the File Ausgefuellt.docx and Shows a Message saying that an unknown Error has occured.

I created a simple Word Document with a bit of unformated text and 2 Text-Form-Fields

can anyone tell me, what went wrong or if ia have an Error in my Source Code

Edit: I managed to specify the Problem. I created an Document only conaining one Text Form Field. In Word 2013 this is found unter the Topic "Formulare aus Vorversionen" (I would translate this to "Formfields from former Versions") If I comment out the whole foreach Block so that I would only open and save the Document, I get the same result.

If I open the Source File directly in Word it is no Problem. I also tried to load the Document and make Word Visible. The Result looked like an empty Word instance with no Document loaded.

sebastianmehler
  • 1,033
  • 1
  • 11
  • 23
  • the execution breaks at `open method`? – Kazimierz Jawor Aug 30 '13 at 13:15
  • I also found this Topic http://stackoverflow.com/q/17383430/1307426 may it be an Word Interop Problem? – sebastianmehler Sep 02 '13 at 06:52
  • Why are you trying to update the template? Maybe you should issue a `Word.Documents.Add(filename);` so Word will create a **new** document based on your template... – Nir Kornfeld Sep 02 '13 at 07:20
  • @Nir Kornfeld: Nice "Word.Documents.Add(filename) did the Trick. Using this instead of Word.Document.Open(filename) Word does not Crash. BTW: field.Range.Text removed the Textfield and replaces it with the given Text, but setting field.Result does the Trick... – sebastianmehler Sep 02 '13 at 08:58
  • @sebastianmehler - I will write my solution as an answer. Can you please mark it as accepted? – Nir Kornfeld Sep 02 '13 at 11:44
  • @sebastianmehler You to offered a bounty for this question, and now you need to mark the answer as accepted. Those are the rules for this site. – Nir Kornfeld Sep 08 '13 at 08:21

2 Answers2

15

You should use:

doc = Word.Documents.Add(filename);

Instead of:

doc = Word.Documents.Open(filename);

So Word will use the template to create a document file, and not open the template itself. It seems Word behaves differently when the active document is a Template.

Nir Kornfeld
  • 827
  • 7
  • 11
2

Use that, it should work:

Word.Application WordApp;
Word.Document WordDoc;

object misValue = System.Reflection.Missing.Value;

WordApp = new Word.ApplicationClass();
WordDoc = WordApp.Documents.Open(filePath2, misValue, misValue, misValue, misValue, misValue,
        misValue, misValue, misValue, misValue, misValue, misValue, misValue, misValue, misValue);
WordDoc.Activate();
Stephan Bauer
  • 9,120
  • 5
  • 36
  • 58
Asieh hojatoleslami
  • 3,240
  • 7
  • 31
  • 45