Solved

How to instantiate a WordprocessingDocument object in vb.net

Posted on 2010-08-25
7
838 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
Turn Insights Into Action

You’ve already invested in ITSM tools, chat applications, automation utilities, and more. Fortify these solutions with intelligent communications so you can drive business processes forward.

With xMatters, you'll never miss a beat.

 
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

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

Question has a verified solution.

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

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…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
This video Micro Tutorial shows how to password-protect PDF files with free software. Many software products can do this, such as Adobe Acrobat (but not Adobe Reader), Nuance PaperPort, and Nuance Power PDF, but they are not free products. This vide…
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…

691 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