Search code examples
c#novacode-docx

'DocX' does not contain a constructor that takes 0 arguments


I want to inherit from DocX, to integrate InterOp printing of Word documents:

public class WordDoc : DocX {
    static private Microsoft.Office.Interop.Word.Application wordApp;

    static WordDoc() {
    }
}

but when I try that, I get this error:

'DocX' does not contain a constructor that takes 0 arguments

From metadata I can see that no constructors are defined at all, so I reckon the empty constructor should be generated. I also do not provide a constructor for my base class.

I do have the static constructor, but when I remove it, I still get the same error.


Solution

  • If you look at the source code, you will see that a constructor is indeed declared:

    internal DocX(DocX document, XElement xml)
            : base(document, xml)
    {
    }
    

    That said, considering that it is marked internal, it would seem that this class is NOT intended to be the public API and as such, you should neither attempt to instantiate it directly or inherit from it.

    Rather, all of the examples seem to be relying on the static Create method in that class:

    public static DocX Create(string filename, 
        DocumentTypes documentType = DocumentTypes.Document)
    {
        // Store this document in memory
        MemoryStream ms = new MemoryStream();
    
        // Create the docx package
        //WordprocessingDocument wdDoc = WordprocessingDocument.Create(ms, 
            DocumentFormat.OpenXml.WordprocessingDocumentType.Document);
        Package package = Package.Open(ms, FileMode.Create, FileAccess.ReadWrite);
    
        PostCreation(package, documentType);
        DocX document = DocX.Load(ms);
        document.filename = filename;
        return document;
    }