Insert image in word file in ASP

2019-08-31 04:25发布

问题:

Im trying to automatically make some word file on server using ASP Classic. It works fine but the only problem is when I download the files there is no picture in them instead I get something like place holder. Here is my code:

set fso = createobject("scripting.filesystemobject")
Set act = fso.CreateTextFile(server.mappath("/") & file_being_created, true)

act.WriteLine("<html xmlns:v=""urn:schemas-microsoft-com:vml""")
act.WriteLine("xmlns:o=""urn:schemas-microsoft-com:office:office""")
act.WriteLine("xmlns:w=""urn:schemas-microsoft-com:office:word""")
act.WriteLine("xmlns:m=""http://schemas.microsoft.com/office/2004/12/omml""")
act.WriteLine("xmlns:css=""http://macVmlSchemaUri"" xmlns=""http://www.w3.org/TR/REC-html40"">")
act.WriteLine("<title>testing</title>")
act.WriteLine("<body> " )
act.WriteLine("<img src='http://mysite.com/images/pic.jpg' width='800' height='200'/><br />" )
act.WriteLine(rsInvoices("invoiceClientID") & "<br />" )
act.WriteLine(rsInvoices("invoiceNumber") )
act.WriteLine("</body></html>")"
act.close

Any idea to have picture in word file? Thanks in advance.

回答1:

Okay, what I did to get to this answer was to create a new Word document (using Word 2010), insert a picture from my hard drive, then save the file as an HTML file. Then I looked at the resulting page and tried to deconstruct what Word was doing. What I found was that in addition to the HTML <img> tag, Word also created a <v:shape> element, hidden by a conditional comment. Here is what I came up based on your example:

<!--[if gte vml 1]>
<v:shape id="Picture1" style="width:800px;height:200px;">
 <v:imagedata src="http://mysite.com/images/pic.jpg"/>
</v:shape>
<![endif]-->
<![if !vml]>
<img src='http://mysite.com/images/pic.jpg' width='800' height='200' v:shapes="Picture1"/>
<![endif]>

If you're serving this file as a .doc file, you could save some space and duplication by only including the <v:shape> element and leaving out the conditional comments.

<v:shape id="Picture1" style="width:800px;height:200px;">
 <v:imagedata src="http://mysite.com/images/pic.jpg"/>
</v:shape>