Ashley Webb Ashley Webb - 1 year ago 191 Question

declaring New instance of WordprocessingDocument using OpenXML

I am attempting to use open XML for the first time. I am following a tutorial found here When I declare a variable as new

I receive the error:

"Type WordprocessingDocument.Create is undefined".

Imports DocumentFormat.OpenXml
Imports DocumentFormat.OpenXml.Wordprocessing
Imports DocumentFormat.OpenXml.Packaging

Partial Class test

Public Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
End Sub

Private Sub makedoc(ByVal documentfilename As String)
Using mydoc As New WordprocessingDocument.Create(documentfilename, WordprocessingDocumentType.Document)
Dim mainPart As MainDocumentPart = mydoc.AddMainDocumentPart()
'Create Document tree for simple document.
mainPart.Document = New Document()
'Create Body (this element contains
'other elements that we want to include
Dim body As New Body()
'Create paragraph
Dim paragraph As New Paragraph()
Dim run_paragraph As New Run()
' we want to put that text into the output document
Dim text_paragraph As New Text("Hello World!")
'Append elements appropriately.
' Save changes to the main document part.
End Using

Answer Source

You can't create a new instance of WordDocumentProcessing like this. The constructor is protected.

You need to call the Create or Open method :

Using mydoc as WordProcessingDocument = WordProcessingDocument.Create(DocumentFileName, WordprocessingDocumentType.Document)
End Using
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download