Thanks in Advance
Creating a report in .DOC formatCan I include pics?
25 Replies - 25380 Views - Last Post: 15 May 2008 - 08:15 AM
#1
Creating a report in .DOC format
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
Thanks in Advance
Replies To: Creating a report in .DOC format
#2
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):

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
That should at least get you started down the right path. Happy Coding

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
#3
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.
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.
#4
Re: Creating a report in .DOC format
Posted 04 March 2008 - 05:25 AM
Two things:
I get an error saying Type "MemoryStream" Is not defined.
Secondly, should this:
be
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
#5
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.
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.
#6
Re: Creating a report in .DOC format
Posted 04 March 2008 - 07:16 AM
I'm getting these errors in my program:
'Bitmap' is a type and cannot be used as an expression.
'Microsoft.Interop.Word.Application' is not defined.
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.
#7
Re: Creating a report in .DOC format
Posted 04 March 2008 - 09:19 AM
Louisda16th, 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.
#8
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
#9
Re: Creating a report in .DOC format
Posted 04 March 2008 - 09:42 AM
Hmmm, I added
And added a reference to Microsoft Office 12.0 Object Library, Microsoft Word 12.0 Object Library then declared my Word object
And get no errors. Maybe it was the Microsoft Office 12.0 Object Library (replace 12.0 with whatever version you're running)
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)
#10
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
):
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:
lol. I must be bugging you
.
'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
#11
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
I tested this by loading an image into the FileStream, then using the
Then set the Backgroundmage Property of a PictureBox to image and it loaded the image.
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.
#12
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:
gives :range = doc.Bookmarks.Item(imgBookMark).Range
range = doc.Bookmarks.Item(imgBookMark).Range
gives :range = doc.Bookmarks.Item(imgBookMark).Range
#13
Re: Creating a report in .DOC format
Posted 04 March 2008 - 11:11 AM
What is the error it's giving you?
#14
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.
Public member 'Bookmarks' on type 'String' not found.
#15
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.
Is the error because, imgBookmark is being treated as a string? I'm totally lost with this one
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)
-
File.doc (230K)
Number of downloads: 342
This post has been edited by Louisda16th: 06 March 2008 - 02:59 AM

New Topic/Question
Reply


MultiQuote


|