Haak3n 01-11-2005, 02:38 PM Anyone know how to export a HTML file to a word document?
HttpContext.Current.Response.Clear()
HttpContext.Current.Response.ContentType = "application/vnd.ms-word"
HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=" & saveAsFile & ".doc")
HttpContext.Current.Response.Write(HttpContext.Current.Server.Htmldeco de("<b>Testing</b>"))
HttpContext.Current.Response.End()
This gives
<b>Testing</b> not Testing Like i want?
As well as, How would I import Images into the document as well?
Basically, is there a way to have a mailmerge type .doc on my server that I can import textboxes into?
Thanks,
wayneph 01-12-2005, 06:45 AM Well, I've got two suggestions. I'm hoping the first one works, because the second one is going to be a lot more work...
You're calling HTMLEncode when you send the text out. That should change a < to <. I would try to see if it works with out the Encode on it.
If it doesn't, you'll probably have to try formatting it using RTF (Rich Text) as opposed to HTML. Or if Office is installed on the machine, you could use Word Automation, actually create an official Word document and then stream that to the browser.
Haak3n 01-12-2005, 07:27 AM Wayne,
First, Love the icon! GO VIKES!
With T.O. out Vikes will def. win! :)
Tried the first option...still nothing :(
How would I go about doing the streaming it to the browser?
wayneph 01-12-2005, 07:37 AM The streaming part is easy. There's only one line to change from your code. I've never actually created a word document, so I don't know what all is envolved in that part.
'Up here you would create a word document and save it to the disk.
'There may be a way to write the document to the browser directly from memory. (without saving it first)
'I'd have to do a little more research to know for sure.
HttpContext.Current.Response.Clear()
HttpContext.Current.Response.ContentType = "application/vnd.ms-word"
HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=" & saveAsFile & ".doc")
'Just make sure that your internet user has permission to read this file.
HttpContext.Current.Response.WriteFile("C:\myfile.doc")
HttpContext.Current.Response.End()
And I wouldn't say the vikes will definately win, but i'd give them a good shot. i gave up on definately in '98 and 2000 when they should have won it all...
Haak3n 01-12-2005, 10:13 AM HttpContext.Current.Response.ContentType = "application/vnd.ms-word"
HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=" & saveAsFile & ".doc")
Dim tw As New System.IO.StringWriter
Dim hw As New System.Web.UI.HtmlTextWriter(tw)
HttpContext.Current.Response.Charset = ""
hw.Write("<html><body>" & _
"<b>Testing</b>" & _
"</body></html>")
HttpContext.Current.Response.Write(tw.ToString)
HttpContext.Current.Response.Flush()
HttpContext.Current.Response.End()
Okay, I got this to output what I want. NOW!, question!..
How would I embed a picture/logo on the file?
wayneph 01-12-2005, 11:24 AM if you're making your word "document" as html, i don't think you can embed the image, since there's no way to embed the image in html. you'd have to create format that supports it.
that's where the rtf or the word automation comes in. (i'll ask a moderator to move this over to the .net office automation forum to see if they have any idea...)
herilane 01-12-2005, 02:12 PM This will be interesting... I know nothing about web programming, and Wayne knows nothing about Word automation... :p
The Word document format is not public, so you cannot really generate a Word document without Word automation, AFAIK. The rtf file format, on the other hand, is public, so you can generate an rtf doc if you want, as Wayne says.
If you want to go down the Word automation route, Word will need to be installed on whatever machine the code is running on. Almost anything that you can do manually in Word can be done with Word automation as well.
Haak3n 01-13-2005, 05:51 AM Word is installed on the server I am running my app on.
I will take a glance at your 'office auto. tutorial' and give that a shot.
I appreciate all your help :)
Once I get it running, i think I am going to put it into a class and post it for other people to use.
herilane 01-13-2005, 07:25 AM There is also a .Net-specific intro to Office Automation, at the top of this forum. The tutorial I link to in my signature is for VB6/VBA.
simflex 01-14-2005, 08:21 AM If I could butt in, I did something similar to what you are trying to do and this is how I did it.
First, as they suggested, you need word running on the server.
Then I created field called logo on my db but have the logo saved on the server (same server where rest of files are) and a filename (name for for the word doc.
Then the code:
First, an insert code that would insert values to the db,
<%
SQLstr="INSERT INTO spills( " & _
"Logo, " & _
"filename) " & _
"values( " & _
" '" & Request.Form("rpt_timeFound") & "'," & _
" 'logo.gif'," & _
" 'EPD') "
just a sample insert code.
Then create a recordset of values inserted into db (again sample code)
' Query the spills table
sql = "select filename, " & _
"logo, " & _
...
"from spills desc"
' Execute the sql
Set rs=myConn.Execute(sql)
'act.WriteLine sql
'response.end
If Not rs.EOF Then
' Creates a text file on the server with the doc abbreviation
' as the name for the ouput document
file_being_created= rs("filename") & ".doc"
'test it to make sure it is working
'act.WriteLine file_being_created
'response.end
' create a file system object
set fso = createobject("scripting.filesystemobject")
' create the text file - true will overwrite any previous files
Set act = fso.CreateTextFile("E:\Inetpub\wwwroot\REPORTS\" & file_being_created, true)
' Writes the db output to a .doc file in the same directory
act.WriteLine("<html><title>Sewer Spills(" & rs("DATE_FOUND") & " Date Info)</title>")
act.WriteLine("<body bgcolor='#FFFFFF'> " )
act.WriteLine("<TABLE BORDER=0 CELLSPACING=0 CELLPADDING=0 HSPACE=0 VSPACE=0 " & _
"WIDTH=786 HEIGHT=150><TR> <TD WIDTH='254' HEIGHT='150'><SPAN STYLE='font-family: " & _
"Times New Roman; font-size: 10pt;'><P> </P><P> </P><P> </P>" & _
"<P><B>Department of Public Works</B><BR> 166 King Street, S.W., Suite 6001<BR>" & _
" MI, IL30303<BR>Telephone: (404) 430-7466<BR> Fax: (571) 284-0978</P></SPAN>" & _
"</TD><TD WIDTH='285'><IMG HEIGHT='178' WIDTH='130' SRC=" & _
trim(rs("PWLogo")) & "></TD><TD WIDTH='247' HEIGHT='150'><SPAN STYLE=" & _
"'font-family: Times New Roman; font-size: 10pt;'><P> " & _
"</P><P> </P><P> </P><P>James Parker<BR> Acting Director<BR> </P>" & _
"</SPAN></TD></TR> </TABLE><br><br><br>")
act.WriteLine("<SPAN STYLE='font-family: Times New Roman; font-size: 12pt;'> " _
& (DisplayDate) & "<br><br>" )
act.WriteLine("" & rs("CONTACTNAME") & "<br>" )
act.WriteLine("Georgia Department of Natural Resources<br>")
act.WriteLine("4244 International Parkway, Suite 110<br>")
act.WriteLine("Atlanta, Georgia 30354 <br><br>")
act.WriteLine("Subject: " & rs("type_of_sp") & " spill at " & rs("spill_loca") & ", " & rs("city") & ", " & rs("state") & ".<br><br>" )
act.WriteLine("</body></html>")
' Writes the links to the newly created pages in the browser
act.WriteLine "<a href='" & file_being_created & "'>" & rs("date_found") & "</a> (.doc) " & now() & "<br>"
act.close
Set act = Nothing
%>
In our own situation, we created an email with attachments to send out the word document with picture in it and here is that email:
<!--METADATA TYPE="typelib" UUID="CD000000-8B95-11D1-82DB-00C04FB1625D" NAME="CDO for Windows XP Library" -->
<%
'sets variables
'email code
Set iMsg = CreateObject("CDO.Message")
Set iConf = CreateObject("CDO.Configuration")
Set Flds = iConf.Fields
Flds(cdoSendUsingMethod) = cdoSendUsingPort
Flds(cdoSMTPServer) = "kss9o"
Flds(cdoSMTPServerPort) = 25
Flds(cdoSMTPAuthenticate) = cdoAnonymous ' 0
Flds.Update
With iMsg
Set .Configuration = iConf
.From = "Spills Admin <webmaster@domain.com>"
.To = "webmaster@domain.com"
.BCC = "webmaster@domain.com"
.AddAttachment("E:\Inetpub\wwwroot\REPORTS\" & file_being_created)
.AddAttachment("E:\Inetpub\wwwroot\REPORTS\logo.gif")
.Sender = "Spills Admin <webmaster@domain.com>"
.Subject = "New Spills Information"
.TextBody = "Attached is a word document of newest spills added to the Spills database. "
.Send
End With
Set iConf=nothing
Set iMsg = Nothing
' Delete The file if it isn't overwritten
'-------------------
fso.DeleteFile("E:\Inetpub\wwwroot\REPORTS\" & file_being_created)
Set fso = Nothing
End If 'rs.EOF
'What you create, you must destroy, ok!
rs.close
Set rs = Nothing
Set myConn=nothing
I have removed real names for privacy.
I hope this is in line with what you are looking for.
If not, sorry for intervening
|