Question

I use this code for compare two word document with ofice word interopt:

object missing = System.Reflection.Missing.Value;
                    object readonlyobj = false;
                    object filename = FirstDocx;

                    //Call assemblys dynamically
                    dynamic objEApp = Activator.CreateInstance(Type.GetTypeFromProgID("Word.Application"));
                    string strAssemblyOff2007 = "Microsoft.Office.Interop.Word, Version=" + objEApp.Version + ".0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c";
                    Assembly xslWordAssembly = Assembly.Load(strAssemblyOff2007); //Load Assembly
                    Type type = xslWordAssembly.GetTypes().Single(t => t.Name == "ApplicationClass");
                    dynamic app = Activator.CreateInstance(type);
                    //var docs = app.Documents;
                    //

                    //Microsoft.Office.Interop.Word.Application app = new Microsoft.Office.Interop.Word.Application();
                    var doc = app.Documents.Open(
                    ref filename, ref missing, ref readonlyobj, ref missing, ref missing,
                    ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);

                    string filenm = SecondDocx;
                    string nm = FirstDocx;
                    object filesavename = nm;

                    doc.TrackRevisions = true;
                    doc.ShowRevisions = true;
                    doc.PrintRevisions = true;
                    doc.Compare(filenm);
                    doc.Close(ref missing, ref missing, ref missing);
                    app.Quit(ref missing, ref missing, ref missing);

First & Second Docx are my files. In code I load assembly dynamically, but when I want open the firstdocx in var doc = app.Documents.Open( I get this error :

Could not convert argument 0 for call to Open.

How can I solve it ?

No correct solution

OTHER TIPS

instead of

var doc = app.Documents.Open(ref filename, ref missing, ref readonlyobj, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);

try simply

var doc = app.Documents.Open(@filename);

It's a problem with the data type. When the COM type library is analyzed and converted into a (P)IA for C# the data types have to be adjusted to what C# understands. C# (originally) does not understand optional parameters - natively it uses overloading, instead. So the VBA optional parameters have to be passed as objects, "by ref". But required parameters retain the data type.

The parameter FileName is not optional and it's typed as a string. So you need:

string filename = FirstDocX;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top