ProgramStyle
ProgramStyle

Reputation: 3

Open an Word Document

i try to open an Word Document with the name "Borecheck.docx"! But when i start my programm and get the error message: No overload for method "Open" takes "1" arguments (CA1501) Can someone help me?

This is my source code:

using System.IO;
using System.Runtime.InteropServices;   
using Word = Microsoft.Office.Interop.Word;

{


public partial class MainForm
{
    void BoreCheckToolStripMenuItemClick(object sender, EventArgs e)
    {
        object wordObject = null;

        try
        {
            wordObject = Marshal.GetActiveObject("Word.Application");               
        }
        catch {}

        Word.Application word = null;
        bool wordInstanceCreated = false;

        if (wordObject != null)
        {
            word = (Word.Application)wordObject;
        }
        else
        {
            wordInstanceCreated = true;
            word = new Word.Application();
        }


        word.Visible = true;

        string fileName = Path.Combine(Application.StartupPath, "Borecheck.docx");
        word.Documents.Open(ref fileName);
    }   

}

Upvotes: 0

Views: 1569

Answers (2)

CodeCaster
CodeCaster

Reputation: 151588

The message is pretty clear. You're not passing the right number of arguments. From Interop.Word Documents.Open is null:

object oMissing = System.Reflection.Missing.Value;

Document doc = word.Documents.Open(filename, ref oMissing,
        ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
        ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
        ref oMissing, ref oMissing, ref oMissing, ref oMissing);

Upvotes: 2

Heinzi
Heinzi

Reputation: 172200

Well, Documents.Open takes more than one parameter:

Document Open(
    ref Object FileName,
    ref Object ConfirmConversions,
    ref Object ReadOnly,
    ref Object AddToRecentFiles,
    ref Object PasswordDocument,
    ref Object PasswordTemplate,
    ref Object Revert,
    ref Object WritePasswordDocument,
    ref Object WritePasswordTemplate,
    ref Object Format,
    ref Object Encoding,
    ref Object Visible,
    ref Object OpenAndRepair,
    ref Object DocumentDirection,
    ref Object NoEncodingDialog,
    ref Object XMLTransform
)

Upvotes: 2

Related Questions