Hi,

I'm getting the error at the doc.close in the last argument; type mismatch error

using object 11 com addin; for which the doc.close takes 3 arguments.

thx

Microsoft.Office.Interop.Word.ApplicationClass wordApp = new ApplicationClass();

            object file = textBox1.Text;
            object nullobj = System.Reflection.Missing.Value;

            Microsoft.Office.Interop.Word.Document doc = wordApp.Documents.Open(ref file, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj);                              

            doc.ActiveWindow.Selection.WholeStory();
            doc.ActiveWindow.Selection.Copy();
            IDataObject data = Clipboard.GetDataObject();

            richTextBox1.Text = data.GetData(DataFormats.Text).ToString();

            doc.Close(ref file, ref nullobj,ref nullobj);

Dani AI

Generated

The Type mismatch at doc.Close is almost always because the parameters are in the wrong order/type. The Document.Close method expects (SaveChanges, OriginalFormat, RouteDocument) — not a filename — so passing your file (a path/string) as the first argument will raise a COM type-mismatch. Use the WdSaveOptions/WdOriginalFormat constants or Missing.Value for those optional args instead. (learn.microsoft.com)

A safe, minimal fix is to pass an explicit save option or Missing for all optional parameters, and read document text via the document range instead of Selection/Clipboard. Example pattern (replace names to match your code):

object missing = System.Type.Missing;
object saveChanges = Microsoft.Office.Interop.Word.WdSaveOptions.wdDoNotSaveChanges;
object originalFormat = Microsoft.Office.Interop.Word.WdOriginalFormat.wdWordDocument;

string text = doc.Content.Text;   // get text without clipboard
doc.Close(ref saveChanges, ref originalFormat, ref missing);

Use doc.Content / Range.Text to get the document body instead of copy/pasting. (learn.microsoft.com)

Note on other replies: pointing to the docs is correct for the signature. ’s wordApp.Documents.Close(...) call closes the whole Documents collection (not a single Document) and still expects SaveChanges as the first argument; if you only want to close one document call doc.Close(...). Finally, always call wordApp.Quit(...) and properly release COM references (or follow Microsoft guidance) to avoid orphaned WINWORD processes. (support.microsoft.com)

Checklist: confirm you did not pass the filename as SaveChanges, prefer explicit WdSaveOptions or Missing.Value, use doc.Content.Text to extract text, call Quit(), and release COM objects.

Recommended Answers

All 2 Replies

object readOnly=false;
wordApp .Documents.Close(ref readOnly, ref readOnly, ref missing);
doc= null;
wordApp .Quit(ref missing, ref missing, ref missing);

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.