Solved

How to instantiate a WordprocessingDocument object in vb.net

Posted on 2010-08-25
7
827 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
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 …
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

777 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