Solved

How to instantiate a WordprocessingDocument object in vb.net

Posted on 2010-08-25
7
825 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 17

Expert Comment

by:Zhaolai
Comment Utility
0
 

Author Comment

by:louise001
Comment Utility
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
Comment Utility
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
Comment Utility
Hi Zhaolai,
That's what I needed.

Thanks,
Louise
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
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…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

771 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

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now