View Full Version : loading images i n word

07-20-2007, 01:49 PM
:confused: Heya all, Im doing an asp page where i can select a button and then display the data in a word file, however I need to be able to display an image next to the data im displaying? how do i do this in asp code?

ive looked on w3schools and on the internet and cant find anything out there?

Only thing w3schools gives is but this wont work when Im loading the asp as a word file.

<td width="20%"><img border=0 name="pic" src="thebrick.bmp"></td>
anyyy helpp please??

07-20-2007, 05:53 PM
Post your code.

07-20-2007, 05:56 PM
Before you post your code ---

Are you trying to write the image INTO the Word file using ASP, or just next to it, as in a link to a Word file with an image?

If you are trying to write an image into the Word file, there is a very long and complicated process needed to do this.

07-23-2007, 10:19 AM
Yes Im trying to get the image loaded in to the word file when I run the asp code.
I click a button, it loads a word file and it should display the image in that word file-
how do I do this? Any ideas or code, please????

07-23-2007, 02:43 PM
The rub is that you cannot do this easily!

You have to use the Scripting.FileObject and build your Word file using the RTF Specifications from Microsoft.

Here are the basics:

1) Build your Word file locally as you want it seen on the web, including at least a placeholder for the image. Save it as an RTF.

2) Open said RTF in Notepad (not Wordpad) and SELECT ALL > COPY. NOTE: RTF tables are some of the hardest things to code. If you can, leave them out of the equation.

3) In your ASP, PASTE the information.

4) Create a new RTF file with just the image in it.

5) Save the RTF, open it in Notepad, copy out the binary image information.

6) Find the image or placeholder that you want to replace in the RTF, and use the replace() function to put your image BINARY text into the RTF.

7) At the bottom, put a Response.Write("<META HTTP-EQUIV=""REFRESH"" CONTENT=""0;URL=yourfilename.doc"">"). This will force the page to refresh and open your Word document.

That all sounds really complicated, right? Well, it's even worse when you try to implement it. If you have 1 "}" out of place, the whole thing won't work. Keep Notepad open to look at your RTF to see what is wrong.

My thoughts having lived through this:

1) Use OpenOffice to build the RTF for you. Its syntax is a lot less difficult, and since RTF is known by most text-editing softwares (MSWord, WordPad, OpenOffice, StarOffice, and WordPerfect), you won't be stuck fighting with MS specific code.

2) Print this to the Web, then use something like ASP2PDF to convert the Web page into a PDF file. Little more work, but it saves a lot of the RTF headache.

3) Use XML. Downsides? It only really works in StarOffice, OpenOffice, and Word '07. You can make it work for Word '03, but nothing earlier than that.

Hope this helps... Let me know if you need more information.

07-24-2007, 01:30 AM
There is an easier way. I recently had to do something very similar.

The part to embed the image is courtesy of http://www.sitepoint.com/forums/showthread.php?t=210521

The code I have below is an example of the image being added entirely through code. it includes the rtf code to create the document. The document seen here is generally created, by my client and sent to them for editing. However by adding the line to write out the Meta refresh tag it will display it on the webpage.

FYI this code is grabbing info from a database to fill in the variables.

Dim ApplicantName
Dim ApplicantAddress
Dim ApplicantCityStateZip
Dim PositionName
Dim Location
Dim Txt
Dim sRTF

'set the font family table we will be using
sRTF = "{\fonttbl {\f0\froman\fcharset0 Times New Roman;}}"

'name the attachment with the applicants name so that if 2 people are sending at same time there will be no mistakes
Page = Server.MapPath("Temp")& "\offerletter.doc"

'create the attachment
Set FSO = Server.CreateObject("Scripting.FileSystemObject")
Set Txt = FSO.CreateTextFile(Page, True)
'open a rich text file
Txt.WriteLine("{\info {\title Employment Offer Letter}{\author Miranda}}")
'set the top and bottom margins
Txt.WriteLine("\margt2160 \margb2160")

'set the font size and font family, each # in the font size = 1/2 pt
'JPEG Header thing
Txt.WriteLine "{\*\shppict{\pict\jpegblip"

'Our JPEG file to read thsi says to go to the root directory of the website and then work up from there
Set oPic = FSO.OpenTextFile(Server.MapPath("\") & "\images\banner.jpg", 1, False)

'Transform binary to a BLIP
sData = ""
Do While Not oPic.AtEndOfStream
sData = sData & LCase(ZeroPad(Hex(Asc(oPic.Read(1))),2))
If Len(sData) Mod 128 = 0 Then
Txt.WriteLine sData
sData = ""
End If

If Len(sData) > 0 Then
Txt.WriteLine sData
End If

'End of JPEG Header
Txt.WriteLine "}}"
Txt.WriteLine("\par " & ApplicantName & " \tab \tab \tab \tab \tab \tab \tab " & DATE())
Txt.WriteLine("\par " & ApplicantAddress)
Txt.WriteLine("\par " & ApplicantCityStateZip)
Txt.WriteLine("\par Dear " & ApplicantName & ":")
'set the paragraph and justify the text
Txt.WriteLine("\par \qj After careful review of the candidates who applied for the position of " & PositionName & " at the " & Location & ", you have been selected as the applicant whose qualifications best match our current needs. Therefore, I am pleased to offer you this part-time non-exempt position at an hourly wage of <pay>. Your first day in this position will be <Date>, and you will be scheduled (for approximately <#> hours per week/from <time> to <time> on <days>/on an as-needed basis).")
Txt.WriteLine("\par \qj If you are a seasonal employee, please note that after the end of the season, you will need to discuss the re-application process for the next season with your supervisor, as we cannot guarantee future seasonal employment.")
Txt.WriteLine("\par \qj You will be provided with a job description for " & PositionName & ", a Part-Time Employee Handbook, payroll schedule, dress code and other information.")
Txt.WriteLine("\par We look forward to working with you! ")
Txt.WriteLine("\par Sincerely,")
Txt.WriteLine("\par <name>")
Txt.WriteLine("\par <title>")
'close the rich text file
Set Txt = Nothing
Set FSO = Nothing

Response.Write("<META HTTP-EQUIV=""REFRESH"" CONTENT=""0;URL=temp/offerletter.doc"">")

'create email and send email with the attachment
Set oMail = Server.CreateObject("CDO.Message")
With oMail
.From = sFrom
.To = sAdminUser
.Subject = "Offer Letter For " & ApplicantName
.TextBody = "An offer letter has been generated for " & ApplicantName & " for the " & PositionName & " job."
.AddAttachment Page
End With

Set oMail = Nothing

'add a delay to make sure the email is sent before we delete the attachment
'for i = 1 to 100000
' Timer = i

'delete the attachment
'If FSO.FileExists(Page) Then
' FSO.DeleteFile Page,True
'End If

Set FSO = Nothing

'notify the user that the file was generated and sent
'Response.Write "Offer letter has been generated and sent to Human Resources."

07-24-2007, 02:35 PM

Your way is a little easier than mine, but I incorporate things like First Page Headers and Footers, tables, and images with "OTHER" layout specifications, so your way isn't going to [easily] address those more advanced techniques. As I wasn't sure what new214's Word file looked like, I wanted to be able to provide as much Word functionality as I could.

However, I will admit that your way of grabbing the image from the server and building it out that way is much better!

Thanks for that snippet!