Solved

How to instantiate a WordprocessingDocument object in vb.net

Posted on 2010-08-25
7
833 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
[X]
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
  • 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

726 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