Creating a report in .DOC format

Can I include pics?

  • (2 Pages)
  • +
  • 1
  • 2

25 Replies - 17907 Views - Last Post: 15 May 2008 - 08:15 AM Rate Topic: -----

#1 Louisda16th  Icon User is offline

  • dream.in.assembly.code
  • member icon

Reputation: 15
  • View blog
  • Posts: 1,967
  • Joined: 03-August 06

Creating a report in .DOC format

Post icon  Posted 03 March 2008 - 08:25 PM

I have this project in which I need to create some software for a community. Basically, I want to export details such as Member details, which are stored in a database, to a Word document. My question is, how do I include a photograph of a member in the document as well if I have the path of the file lets say in JPEG format. Since I'm using VB.NET Express, I don't have Crystal Reports. I thought exporting it to a file would be the next best thing to do. Any help appreciated.
Thanks in Advance :)

Is This A Good Question/Topic? 1
  • +

Replies To: Creating a report in .DOC format

#2 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1639
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Creating a report in .DOC format

Posted 03 March 2008 - 10:03 PM

Good question Ashwith. To answer your question, yes it can be done. Will it take more than a few lines of code, yes it will. You're going to want to add a Reference to the Microsoft Word 11 Object Library (or whatever version of Word you're using):

Attached Image

Then you need to create a template document, and add a bookmark in the document, I called mine ImageBookmark, save that document and remember it's path. Then from there you will add a reference to the System.Runtime.InteropServices Namespace like Imports System.Runtime.InteropServices.

You can then use code such as this for inserting your image and saving a copy of your template document


'Create your image
Dim stream As New MemoryStream("YourImage")
Dim image As New Bitmap(Image.FromStream(stream),Bitmap)

'Now create your Word Document
Dim word As New Microsoft.Interop.Word.Application
'Make the application invisible
word.Visible = False

'Interop parameters
Dim save As Object = False;

'Now for the document paths
Dim destination As Object = "C:\FileWithImage.doc"
Dim doc As Object = "C\File.doc"

'Create a bookmark in the document
Dim imgBookMark As Object = "ImageBookmark"
'Create a Word range
Dim range As Microsoft.Office.Interop.Word.Range
'set the range value
range = doc.Bookmarks.Item(ImageBookmark).Range

'Now copy the image to the clipboard
Clipboard.SetDataObject(image)

'Now paste the image into the Word document
range.Paste()

'Now save your document
doc.SaveAs(destination)

'Close Word
word.Quit(save)
'Release the Word object
System.Runtime.InteropServices.Marshal.ReleaseComObject(word)




That should at least get you started down the right path. Happy Coding :)
Was This Post Helpful? 0
  • +
  • -

#3 Louisda16th  Icon User is offline

  • dream.in.assembly.code
  • member icon

Reputation: 15
  • View blog
  • Posts: 1,967
  • Joined: 03-August 06

Re: Creating a report in .DOC format

Posted 04 March 2008 - 02:13 AM

Thanks Psychcoder! I'll try it out as soon as I return home. I do have these questions however:
1. What is the difference between the Objects "doc" and "destination"? Is "doc" a temporary file you work with before you save it (you spoke of a template)?
2. I didn't understand the purpose of the bookmark (along with the range part).
3. I'd like to know how to add text as well. Does it follow a similar procedure?
Thanks in Advance.
Was This Post Helpful? 0
  • +
  • -

#4 Louisda16th  Icon User is offline

  • dream.in.assembly.code
  • member icon

Reputation: 15
  • View blog
  • Posts: 1,967
  • Joined: 03-August 06

Re: Creating a report in .DOC format

Posted 04 March 2008 - 05:25 AM

Two things:
Dim stream As New MemoryStream("YourImage")


I get an error saying Type "MemoryStream" Is not defined.

Secondly, should this:
range = doc.Bookmarks.Item(ImageBookMark).Range


be
range = doc.Bookmarks.Item(imgBookMark).Range


Was This Post Helpful? 0
  • +
  • -

#5 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1639
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Creating a report in .DOC format

Posted 04 March 2008 - 06:36 AM

For the MemoryStream you need to add a reference to the System.IO Namespace.

doc is the template I said to create, destination is what the file will be saved at. Bookmark is just a space you reserve to paste your image, you create that when you create your template. To add the text you would do it like you would a text document.
Was This Post Helpful? 0
  • +
  • -

#6 Louisda16th  Icon User is offline

  • dream.in.assembly.code
  • member icon

Reputation: 15
  • View blog
  • Posts: 1,967
  • Joined: 03-August 06

Re: Creating a report in .DOC format

Posted 04 March 2008 - 07:16 AM

I'm getting these errors in my program:

Dim image As New Bitmap(image.FromStream(stream), Bitmap)


'Bitmap' is a type and cannot be used as an expression.

Dim word As New Microsoft.Interop.Word.Application


'Microsoft.Interop.Word.Application' is not defined.
Was This Post Helpful? 0
  • +
  • -

#7 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1639
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Creating a report in .DOC format

Posted 04 March 2008 - 09:19 AM

View PostLouisda16th, on 4 Mar, 2008 - 06:16 AM, said:

Dim image As New Bitmap(image.FromStream(stream), Bitmap)


'Bitmap' is a type and cannot be used as an expression.


You are right, it should be

Dim image As New Bitmap(Drawing.Image.FromStream(stream))




Quote

Dim word As New Microsoft.Interop.Word.Application


'Microsoft.Interop.Word.Application' is not defined.


Did you add the reference to Microsoft.Interop.Word.Application and add Imports Microsoft.Interop.Word.Application. I showed a screenshot of adding the reference to your project, then you need the Imports statement as well.
Was This Post Helpful? 0
  • +
  • -

#8 Louisda16th  Icon User is offline

  • dream.in.assembly.code
  • member icon

Reputation: 15
  • View blog
  • Posts: 1,967
  • Joined: 03-August 06

Re: Creating a report in .DOC format

Posted 04 March 2008 - 09:31 AM

There is a Microsoft.Office.Interop.Word but no Microsoft.Interop.Word.Application or Microsoft.Office.Interop.Word.Application

This post has been edited by Louisda16th: 04 March 2008 - 09:31 AM

Was This Post Helpful? 0
  • +
  • -

#9 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1639
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Creating a report in .DOC format

Posted 04 March 2008 - 09:42 AM

Hmmm, I added

Imports System.Runtime.InteropServices
Imports Microsoft.Office.Interop



And added a reference to Microsoft Office 12.0 Object Library, Microsoft Word 12.0 Object Library then declared my Word object


Dim word As New Microsoft.Office.Interop.Word.Application



And get no errors. Maybe it was the Microsoft Office 12.0 Object Library (replace 12.0 with whatever version you're running)
Was This Post Helpful? 0
  • +
  • -

#10 Louisda16th  Icon User is offline

  • dream.in.assembly.code
  • member icon

Reputation: 15
  • View blog
  • Posts: 1,967
  • Joined: 03-August 06

Re: Creating a report in .DOC format

Posted 04 March 2008 - 10:08 AM

One tiny error pops up when I click run. First here's the entire procedure (hope I not made major mistakes :)):
'Create your image
		Dim stream As New MemoryStream("e:\efylogo.jpg")
		Dim image As New Bitmap(Drawing.Image.FromStream(stream))

		'Now create your Word Document
		Dim word As New Microsoft.Office.Interop.Word.Application
		'Make the application invisible
		word.Visible = False

		'Interop parameters
		Dim save As Object = False

		'Now for the document paths
		Dim destination As Object = "E:\qwer.doc"
		Dim doc As Object = "E:\test.doc"

		'Create a bookmark in the document
		Dim imgBookMark As Object = "ImageBookmark"
		'Create a Word range
		Dim range As Microsoft.Office.Interop.Word.Range
		'set the range value
		range = doc.Bookmarks.Item(imgBookMark).Range

		'Now copy the image to the clipboard
		Clipboard.SetDataObject(image)

		'Now paste the image into the Word document
		range.Paste()

		'Now save your document
		doc.SaveAs(destination)

		'Close Word
		word.Quit(save)
		'Release the Word object
		System.Runtime.InteropServices.Marshal.ReleaseComObject(word)


Now when I run the program, I get this error:
Conversion from string "e:\efylogo.jpg" to type 'Integer' is not valid.
It highlights this statement:
Dim stream As New MemoryStream("e:\efylogo.jpg")


lol. I must be bugging you :).
Was This Post Helpful? 0
  • +
  • -

#11 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1639
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Creating a report in .DOC format

Posted 04 March 2008 - 10:21 AM

Well since the MemoryStream doesn't seem to do what I want it to do, we're going to read it into a FileStream


Dim stream As New System.IO.FileStream("e:\efylogo.jpg", IO.FileMode.Open, IO.FileAccess.Read)



I tested this by loading an image into the FileStream, then using the

Dim image As New  Bitmap(Drawing.Image.FromStream(stream))



Then set the Backgroundmage Property of a PictureBox to image and it loaded the image.
Was This Post Helpful? 0
  • +
  • -

#12 Louisda16th  Icon User is offline

  • dream.in.assembly.code
  • member icon

Reputation: 15
  • View blog
  • Posts: 1,967
  • Joined: 03-August 06

Re: Creating a report in .DOC format

Posted 04 March 2008 - 10:43 AM

The picture gets loaded to the picture box perfectly. But there's another error:
range = doc.Bookmarks.Item(imgBookMark).Range


gives :range = doc.Bookmarks.Item(imgBookMark).Range
Was This Post Helpful? 0
  • +
  • -

#13 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1639
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Creating a report in .DOC format

Posted 04 March 2008 - 11:11 AM

What is the error it's giving you?
Was This Post Helpful? 0
  • +
  • -

#14 Louisda16th  Icon User is offline

  • dream.in.assembly.code
  • member icon

Reputation: 15
  • View blog
  • Posts: 1,967
  • Joined: 03-August 06

Re: Creating a report in .DOC format

Posted 04 March 2008 - 11:14 AM

Oops. My bad. I copied the code instead of the error. Here it is:

Public member 'Bookmarks' on type 'String' not found.
Was This Post Helpful? 0
  • +
  • -

#15 Louisda16th  Icon User is offline

  • dream.in.assembly.code
  • member icon

Reputation: 15
  • View blog
  • Posts: 1,967
  • Joined: 03-August 06

Re: Creating a report in .DOC format

Posted 06 March 2008 - 02:47 AM

*bump* :)

Is the error because, imgBookmark is being treated as a string? I'm totally lost with this one :). Possibly I've got the bookmark thingy all wrong. Here's my template document (attached). A few things I didn't tell before:
I'm using VB.NET 2005 Express, Office 2003 Professional, Windows XP Professional and the Version of Word object library is 11.0.

Attached File(s)

  • Attached File  File.doc (230K)
    Number of downloads: 243

This post has been edited by Louisda16th: 06 March 2008 - 02:59 AM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2