Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

How to instantiate a WordprocessingDocument object in vb.net

Posted on 2010-08-25
7
831 Views
Last Modified: 2012-05-10
Hi,
I've pasted the code below (which I got from http://msdn.microsoft.com/en-us/library/bb656295.aspx) into my application but the object WordprocessingDocument isn't recognised. This sounds like it should be straightforward but I haven't been able to find what imports reference I need to add in my page. Could anyone please let me know?
Thanks ...

Louise
Public Sub CreateNewWordDocument(ByVal document As String)
   Dim wordDoc As WordprocessingDocument = WordprocessingDocument.Create(document, WordprocessingDocumentType.Document)
   Using (wordDoc)
      ' Set the content of the document so that Word can open it.
      Dim mainPart As MainDocumentPart = wordDoc.AddMainDocumentPart
      SetMainDocumentContent(mainPart)
   End Using
End Sub

Public Sub SetMainDocumentContent(ByVal part As MainDocumentPart)
   Const docXml As String = "<?xml version=""1.0"" encoding=""UTF-8"" standalone=""yes""?>" & _
   "<w:document xmlns:w=""http://schemas.openxmlformats.org/wordprocessingml/2006/main"">" & _
   "<w:body><w:p><w:r><w:t>Hello world!</w:t></w:r></w:p></w:body></w:document>"
   Dim stream1 As Stream = part.GetStream
   Dim utf8encoder1 As UTF8Encoding = New UTF8Encoding()
   Dim buf() As Byte = utf8encoder1.GetBytes(docXml)
   stream1.Write(buf, 0, buf.Length)
End Sub

Open in new window

0
Comment
Question by:louise001
  • 3
  • 2
  • 2
7 Comments
 
LVL 38

Expert Comment

by:Tom Beck
ID: 33522024
Reference Microsoft.Office.Interop.Word and Microsoft.Office.Core Then import:
Imports Microsoft.Office.Core
Imports System.Runtime.InteropServices
Imports Microsoft.Office.Interop.Word
0
 

Author Comment

by:louise001
ID: 33522271
Hi,
I've done all that (I'm using Visual Studio 21010 and Microsoft.Office.Core isn't available to add as a reference, I selected Office 12 instead and .Office.Core is available to import in my code page), but the WordprocessingDocument type isn't recognised.

Do you know what the full path for WordprocessingDocument is (e.g. DataTable's full path is System.Data.DataTable)?

Thanks,

Louise
0
 
LVL 38

Expert Comment

by:Tom Beck
ID: 33522328
Actually, I've never used WordprocessingDocument. I've only used this to instantiate:

        Dim word As New Microsoft.Office.Interop.Word.Application
        Dim WrdDoc As New Microsoft.Office.Interop.Word.Document
        WrdDoc = word.Documents.Add()
0
How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

 
LVL 17

Expert Comment

by:Zhaolai
ID: 33522376
0
 

Author Comment

by:louise001
ID: 33522388
That's not quite what I want to do. I need to create an Office Open XML package as described in the link in my question: http://msdn.microsoft.com/en-us/library/bb656295.aspx as advised in http://support.microsoft.com/kb/257757
Thanks,
Louise
0
 
LVL 17

Accepted Solution

by:
Zhaolai earned 500 total points
ID: 33522529
Actually, you want to download the latest 2.0 from here:

http://www.microsoft.com/downloads/details.aspx?FamilyID=c6e744e5-36e9-45f5-8d8c-331df206e0d0&displaylang=en

Then the reference is: DocumentFormat.OpenXml.Packaging

0
 

Author Comment

by:louise001
ID: 33553898
Hi Zhaolai,
That's what I needed.

Thanks,
Louise
0

Featured Post

The New “Normal” in Modern Enterprise Operations

DevOps for the modern enterprise offers many benefits — increased agility, productivity, and more, but digital transformation isn’t easy, especially if you’re not addressing the right issues. Register for the webinar to dive into the “new normal” for enterprise modern ops.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question