Join 244,117 VB.NET Programmers for FREE! Get instant access to thousands of VB.NET experts, tutorials, code snippets, and more! There are 1,403 people online right now. Registration is fast and FREE... Join Now!
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
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
vb
'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
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.
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.
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
vb
Dim image As New Bitmap(Drawing.Image.FromStream(stream))
QUOTE
CODE
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.
One tiny error pops up when I click run. First here's the entire procedure (hope I not made major mistakes ):
CODE
'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:
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.
This post has been edited by Louisda16th: 6 Mar, 2008 - 01:59 AM
Attached File(s) File.doc ( 230k )
Number of downloads: 22
I realised that parts of the above code are unnecessary. Here's how my final code looks in my project:
vb
Private Sub btnExport_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExport.Click Dim path As String Dim docword As New Word.Application Dim rng As Word.Range Dim LastPos As Integer Dim startend As Integer SaveFileDialog1.ShowDialog() path = SaveFileDialog1.FileName docword.Documents.Open(My.Application.Info.DirectoryPath + "\File.doc") startend = docword.ActiveDocument.Range.End - 1 rng = docword.ActiveDocument.Range(Start:=startend, End:=startend) rng.Text = "Date:" + DateDateTimePicker.Text + vbNewLine + vbNewLine _ + "Time:" + TimeDateTimePicker.Text + vbNewLine + vbNewLine Meeting_AgendaBindingSource.MoveLast() LastPos = Meeting_AgendaBindingSource.Position Meeting_AgendaBindingSource.MoveFirst() rng.Text += "Things To Do:" + vbNewLine + vbNewLine + vbNewLine While (1) rng.Text += vbTab + Agenda_TitleTextBox.Text + ": " + DescriptionTextBox.Text + vbNewLine + vbNewLine If Meeting_AgendaBindingSource.Position = LastPos Then Exit While Meeting_AgendaBindingSource.MoveNext() End While Meeting_AgendaBindingSource.MoveFirst() docword.ActiveDocument.SaveAs(path) docword.Quit() End Sub
This post has been edited by Louisda16th: 6 Mar, 2008 - 08:55 AM