views:

112

answers:

3

I am writing a sample application to convert a DOC file into a PDF. While doing this I'm getting an error.

// Creating the instance of WordApplication
MSDOC = new Microsoft.Office.Interop.Word.ApplicationClass();
try
{
    MSDOC.Visible = false;
    MSDOC.Documents.Open(ref Source, ref Unknown,
     ref Unknown, ref Unknown, ref Unknown,
     ref Unknown, ref Unknown, ref Unknown,
     ref Unknown, ref Unknown, ref Unknown,
     ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown);
    MSDOC.Application.Visible = false;
    MSDOC.WindowState = Microsoft.Office.Interop.Word
                                 .WdWindowState.wdWindowStateMaximize;
    object format = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatPDF;
}
catch (Exception ex)
{
    MessageBox.Show(ex.Message, "Message from Sample");
}

And this is the statement I am getting an error at:

object format = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatPDF;

Error Interop type 'Microsoft.Office.Interop.Word.ApplicationClass' cannot be embedded. Use the applicable interface instead.

+3  A: 

Try MSDOC = new Microsoft.Office.Interop.Word.Application(); instead of .ApplicationClass().

jball
+3  A: 

Have you tried doing what the error message suggests? Replace

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

with

Microsoft.Office.Interop.Word.Application MSDOC;
MSDOC = new Microsoft.Office.Interop.Word.Application();
0xA3
+1  A: 

Starting with .Net 4.0, only COM metadata (such as interfaces, enums or structures) can be embedded (One cannot embed classes because these typically contain code as well).
The solution is to use the [COM] interface which is the case at hand is conveniently exposed in the Microsoft.Office.Interop.Word.Application (cf jball and OxA3 responses)

var MSDOC = new Microsoft.Office.Interop.Word.Application();
mjv