Copy Images to a word File

S

sandeepkedlaya

Hello,
I need to copy images from a folder and copy to a word document. I
have done as follows.. but It copies only last image. Can any one of
you help me in this..
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Response.Buffer = True
Dim DirPath As String
DirPath = "D:\ASPDotNet\images\"
Dim FileName As String


Response.ContentType = "application/msword"
Response.AddHeader("Content-Disposition",
"inline;filename=TestImage.doc")
Dim FileList As String() = Directory.GetFiles(DirPath)

For Each FileName In FileList
Image1.Style.Item("Height") += 50
Image1.ImageUrl = FileName

Label1.Text = "this is Image No:" & i
Label1.Style.Item("Height") += 50
i = i + 1
Next
End Sub
 
M

Mark Rae

I need to copy images from a folder and copy to a word document. I
have done as follows.. but It copies only last image. Can any one of
you help me in this..
For Each FileName In FileList
Image1.Style.Item("Height") += 50
Image1.ImageUrl = FileName

Label1.Text = "this is Image No:" & i
Label1.Style.Item("Height") += 50
i = i + 1
Next
End Sub

Looks very much like you're populating the same control (Image1) each time
you loop through FileList...
 
S

Sandy

Looks very much like you're populating the same control (Image1) each time
you loop through FileList...

Yes.. But I tried to Dynamically create the Image (dim img as Image)
then assign the values.. img.ImageURL=FileName still the same result..
 
S

Sandy

I have changed the code to dyanamically create the Image. Still No
success.
The Changed code is:
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim DirPath As String
DirPath = "D:\ASPDotNet\images\"
Dim FileName As String
Response.ContentType = "application/msword"
Response.AddHeader("Content-Disposition",
"inline;filename=TestImage.doc")
Dim FileList As String() = Directory.GetFiles(DirPath)
For Each FileName In FileList
image2.ID = i
image2.ImageUrl = FileName
image2.Style.Item("Height") += 50
Next
End Sub
If I do like this only last one is Copied and also Image is not
displayed properly.
 
M

Mark Rae

I have changed the code to dyanamically create the Image.

Not as far as I can see...
For Each FileName In FileList
image2.ID = i
image2.ImageUrl = FileName
image2.Style.Item("Height") += 50
Next

Every time the above For loop iterates, it is overwriting the image2
variable, not creating a new image variable...
 
S

Sandy

If I use like blow it does not copy anything into word.
Dim i As Integer = 0
For Each FileName In FileList

Dim image(i) As System.Web.UI.WebControls.Image
image(i) = New System.Web.UI.WebControls.Image
image(i).ID = i
image(i).ImageUrl = FileName
i = i + 1
Next
 
M

Mark Rae

If I use like blow it does not copy anything into word.
Dim i As Integer = 0
For Each FileName In FileList

Dim image(i) As System.Web.UI.WebControls.Image
image(i) = New System.Web.UI.WebControls.Image
image(i).ID = i
image(i).ImageUrl = FileName
i = i + 1
Next

Well it wouldn't do... For each iteration through the above loop you are:

- dimensioning an image variable
- instantiating a new image variable
- assigning it an ID
- populating its ImageUrl variable
- incrementing a counter

You don't appear to be actually doing anything with the image variables
other than creating them... Please show the code where you are trying to add
them to the Word document...
 
M

Mark Rae

I'm not sure how to add..That is what I's missing...

Ah, right...

Firstly, don't even think about using server automation with ASP.NET - it's
not supported by Microsoft, and will almost certainly cause you huge
problems sooner or later...

Basically, if you want to create and stream Word content with ASP.NET, there
are three ways:

1) Use HTML
Word, like Excel and PowerPoint etc, supports HTML as a file format...

a) Open up Notepad and copy the following text:

<html>

<head>
<title>I'm not really a Word doc, but I look like one</title>
</head>

<body>
Hello world!
</body>

</html>

b) Save the Notepad file with a .doc extension

c) Double-click on the file you just created - what happens...? Word
launches and displays the file just like a "real" Word document. The only
potential problem with this would be the images themselves, because you
can't embed images in HTML - that will mean that they will need to reside on
a webserver somewhere that your users can see...


2) Use XML
Word 2003 & 2007 supports the XML file format:
http://www.microsoft.com/downloads/...52-3547-420a-a412-00a2662442d9&displaylang=en
http://www.microsoft.com/downloads/...80-f2c0-4b80-9ad1-2cb0c300aef9&displaylang=en

ASP.NET and XML are "made for each other", so you can use the standard XML
namespace to create Word documents. However, even if you are fairly familiar
with XML, this can be quite difficult.


3) Use Aspose
http://www.aspose.com/Products/Aspose.Words/Default.aspx

This is easy to use and very powerful, but not free... However, it will
allow you to embed the images into the Word document because it will create
true Word documents in the native Word file format...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top