Chat LIVE With Programming Experts! There Are 23 Online Right Now...

Welcome to Dream.In.Code
Become a VB.NET Expert!

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!




Creating a report in .DOC format

2 Pages V  1 2 >  
Reply to this topicStart new topic

Creating a report in .DOC format, Can I include pics?

Louisda16th
3 Mar, 2008 - 07:25 PM
Post #1

dream.in.assembly.code
Group Icon

Joined: 3 Aug, 2006
Posts: 1,848



Thanked: 3 times
Dream Kudos: 755
My Contributions
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 smile.gif

User is offlineProfile CardPM
+Quote Post


PsychoCoder
RE: Creating A Report In .DOC Format
3 Mar, 2008 - 09:03 PM
Post #2

loves.Coding(this);
Group Icon

Joined: 26 Jul, 2007
Posts: 12,265



Thanked: 372 times
Dream Kudos: 10775
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net, jQuery

My Contributions
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


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 smile.gif
User is offlineProfile CardPM
+Quote Post

Louisda16th
RE: Creating A Report In .DOC Format
4 Mar, 2008 - 01:13 AM
Post #3

dream.in.assembly.code
Group Icon

Joined: 3 Aug, 2006
Posts: 1,848



Thanked: 3 times
Dream Kudos: 755
My Contributions
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.
User is offlineProfile CardPM
+Quote Post

Louisda16th
RE: Creating A Report In .DOC Format
4 Mar, 2008 - 04:25 AM
Post #4

dream.in.assembly.code
Group Icon

Joined: 3 Aug, 2006
Posts: 1,848



Thanked: 3 times
Dream Kudos: 755
My Contributions
Two things:
CODE

Dim stream As New MemoryStream("YourImage")

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

Secondly, should this:
CODE

range = doc.Bookmarks.Item(ImageBookMark).Range

be
CODE

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



User is offlineProfile CardPM
+Quote Post

PsychoCoder
RE: Creating A Report In .DOC Format
4 Mar, 2008 - 05:36 AM
Post #5

loves.Coding(this);
Group Icon

Joined: 26 Jul, 2007
Posts: 12,265



Thanked: 372 times
Dream Kudos: 10775
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net, jQuery

My Contributions
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.
User is offlineProfile CardPM
+Quote Post

Louisda16th
RE: Creating A Report In .DOC Format
4 Mar, 2008 - 06:16 AM
Post #6

dream.in.assembly.code
Group Icon

Joined: 3 Aug, 2006
Posts: 1,848



Thanked: 3 times
Dream Kudos: 755
My Contributions
I'm getting these errors in my program:

CODE

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

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

CODE

Dim word As New Microsoft.Interop.Word.Application

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

User is offlineProfile CardPM
+Quote Post

PsychoCoder
RE: Creating A Report In .DOC Format
4 Mar, 2008 - 08:19 AM
Post #7

loves.Coding(this);
Group Icon

Joined: 26 Jul, 2007
Posts: 12,265



Thanked: 372 times
Dream Kudos: 10775
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net, jQuery

My Contributions
QUOTE(Louisda16th @ 4 Mar, 2008 - 06:16 AM) *

CODE

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.

User is offlineProfile CardPM
+Quote Post

Louisda16th
RE: Creating A Report In .DOC Format
4 Mar, 2008 - 08:31 AM
Post #8

dream.in.assembly.code
Group Icon

Joined: 3 Aug, 2006
Posts: 1,848



Thanked: 3 times
Dream Kudos: 755
My Contributions
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: 4 Mar, 2008 - 08:31 AM
User is offlineProfile CardPM
+Quote Post

PsychoCoder
RE: Creating A Report In .DOC Format
4 Mar, 2008 - 08:42 AM
Post #9

loves.Coding(this);
Group Icon

Joined: 26 Jul, 2007
Posts: 12,265



Thanked: 372 times
Dream Kudos: 10775
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net, jQuery

My Contributions
Hmmm, I added

vb

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


vb

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)
User is offlineProfile CardPM
+Quote Post

Louisda16th
RE: Creating A Report In .DOC Format
4 Mar, 2008 - 09:08 AM
Post #10

dream.in.assembly.code
Group Icon

Joined: 3 Aug, 2006
Posts: 1,848



Thanked: 3 times
Dream Kudos: 755
My Contributions
One tiny error pops up when I click run. First here's the entire procedure (hope I not made major mistakes smile.gif):
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:
CODE

Dim stream As New MemoryStream("e:\efylogo.jpg")

lol. I must be bugging you smile.gif.
User is offlineProfile CardPM
+Quote Post

PsychoCoder
RE: Creating A Report In .DOC Format
4 Mar, 2008 - 09:21 AM
Post #11

loves.Coding(this);
Group Icon

Joined: 26 Jul, 2007
Posts: 12,265



Thanked: 372 times
Dream Kudos: 10775
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net, jQuery

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


vb

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

vb

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


Then set the Backgroundmage Property of a PictureBox to image and it loaded the image.
User is offlineProfile CardPM
+Quote Post

Louisda16th
RE: Creating A Report In .DOC Format
4 Mar, 2008 - 09:43 AM
Post #12

dream.in.assembly.code
Group Icon

Joined: 3 Aug, 2006
Posts: 1,848



Thanked: 3 times
Dream Kudos: 755
My Contributions
The picture gets loaded to the picture box perfectly. But there's another error:
CODE

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

gives :range = doc.Bookmarks.Item(imgBookMark).Range
User is offlineProfile CardPM
+Quote Post

PsychoCoder
RE: Creating A Report In .DOC Format
4 Mar, 2008 - 10:11 AM
Post #13

loves.Coding(this);
Group Icon

Joined: 26 Jul, 2007
Posts: 12,265



Thanked: 372 times
Dream Kudos: 10775
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net, jQuery

My Contributions
What is the error it's giving you?
User is offlineProfile CardPM
+Quote Post

Louisda16th
RE: Creating A Report In .DOC Format
4 Mar, 2008 - 10:14 AM
Post #14

dream.in.assembly.code
Group Icon

Joined: 3 Aug, 2006
Posts: 1,848



Thanked: 3 times
Dream Kudos: 755
My Contributions
Oops. My bad. I copied the code instead of the error. Here it is:

Public member 'Bookmarks' on type 'String' not found.
User is offlineProfile CardPM
+Quote Post

Louisda16th
RE: Creating A Report In .DOC Format
6 Mar, 2008 - 01:47 AM
Post #15

dream.in.assembly.code
Group Icon

Joined: 3 Aug, 2006
Posts: 1,848



Thanked: 3 times
Dream Kudos: 755
My Contributions
*bump* smile.gif

Is the error because, imgBookmark is being treated as a string? I'm totally lost with this one smile.gif. 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)
Attached File  File.doc ( 230k ) Number of downloads: 22
User is offlineProfile CardPM
+Quote Post

Louisda16th
RE: Creating A Report In .DOC Format
6 Mar, 2008 - 02:35 AM
Post #16

dream.in.assembly.code
Group Icon

Joined: 3 Aug, 2006
Posts: 1,848



Thanked: 3 times
Dream Kudos: 755
My Contributions
Let me add that the Bookmark I tried to use is "IMG".
User is offlineProfile CardPM
+Quote Post

Louisda16th
RE: Creating A Report In .DOC Format
6 Mar, 2008 - 03:04 AM
Post #17

dream.in.assembly.code
Group Icon

Joined: 3 Aug, 2006
Posts: 1,848



Thanked: 3 times
Dream Kudos: 755
My Contributions
Alrighty. I did some searching and found this nice little set of MSDN articles. Let me check them out and see if they help. Here's the link in case anyone is interested smile.gif:
http://msdn2.microsoft.com/en-us/library/7...7s6(VS.80).aspx

User is offlineProfile CardPM
+Quote Post

Louisda16th
RE: Creating A Report In .DOC Format
6 Mar, 2008 - 04:14 AM
Post #18

dream.in.assembly.code
Group Icon

Joined: 3 Aug, 2006
Posts: 1,848



Thanked: 3 times
Dream Kudos: 755
My Contributions
I think I found something else but I'm getting close.
User is offlineProfile CardPM
+Quote Post

Louisda16th
RE: Creating A Report In .DOC Format
6 Mar, 2008 - 05:05 AM
Post #19

dream.in.assembly.code
Group Icon

Joined: 3 Aug, 2006
Posts: 1,848



Thanked: 3 times
Dream Kudos: 755
My Contributions
Yeah! Success at last smile.gif. Here's the code that worked:
CODE

Dim docword As New Word.Application
        docword.Documents.Open("C:\File.doc")
        Dim rng As Word.Range
        rng = docword.ActiveDocument.Range(Start:=10, End:=92)
        rng.Text = "Name: ****" + vbNewLine + vbNewLine _
                    + "Contact No.: ****" + vbNewLine + vbNewLine _
                    + "Address: ****" _
                    + "*****" + vbNewLine + vbNewLine _
                    + "Email ID:****" + vbNewLine + vbNewLine _
                    + "Birthday: ****" + vbNewLine + vbNewLine _
                    + "Designation: ****" + vbNewLine + vbNewLine _
                    + "Photograph:" + vbNewLine
        docword.ActiveDocument.SaveAs("C:\qwerty.doc")
        Dim startend As Integer
        startend = docword.ActiveDocument.Range.End - 1
        rng = docword.ActiveDocument.Range(Start:=startend, End:=startend)
        rng.MoveStart(Unit:=Word.WdUnits.wdCharacter, Count:=1)
        rng.MoveEnd(Unit:=Word.WdUnits.wdCharacter, Count:=1)
        rng.Select()
        docword.Selection.InlineShapes.AddPicture("E:\memberpic.jpg")
        docword.ActiveDocument.Save()
        docword.Quit()

User is offlineProfile CardPM
+Quote Post

Louisda16th
RE: Creating A Report In .DOC Format
6 Mar, 2008 - 08:54 AM
Post #20

dream.in.assembly.code
Group Icon

Joined: 3 Aug, 2006
Posts: 1,848



Thanked: 3 times
Dream Kudos: 755
My Contributions
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
User is offlineProfile CardPM
+Quote Post

2 Pages V  1 2 >
Fast ReplyReply to this topicStart new topic

Time is now: 7/4/09 02:38AM

Live VB.NET Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

VB.NET Tutorials

Reference Sheets

VB.NET Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month