简体   繁体   中英

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);
    }   

}

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
)

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM