VBscript在word中添加图片!!!

这篇博客介绍了一种使用VBScript在Word文档中批量添加图片的方法。通过JavaScript获取图片数据,然后利用VBScript创建Word应用并插入图片,最终生成包含图片的Word文档。用户在网页端操作后,数据处理完毕会弹出提示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<HTML>
<HEAD>
<meta HTTP-EQUIV="Content-Type" content="text/html; charset=gb_2312-80">
<link rel="stylesheet" type="text/css" href="edit.css">
<title>保存为Word文档</title>
</HEAD>
<script language="javascript">
window.returnValue=0
</script>
<BODY>
<font color="#000080"><strong><div id="strHint" align="center">数据处理中,请等待...<br>
Data processing, please wait...</div></strong></font><font color="#0080C0">
<!--#include file="../inc/function.asp"-->
<%
Function GetUrl()
On Error Resume Next
Dim strTemp
If LCase(Request.ServerVariables("HTTPS")) = "off" Then
 strTemp = "http://"
Else
 strTemp = "https://"
End If
strTemp = strTemp & Request.ServerVariables("SERVER_NAME")
If Request.ServerVariables("SERVER_PORT") <> 80 Then
 strTemp = strTemp & ":" & Request.ServerVariables("SERVER_PORT")
end if
GetUrl = strTemp
End Function
set objcn = server.CreateObject("gdcom.database")
set rs = server.CreateObject("ADODB.Recordset")
set fso = Server.Createobject("Scripting.FileSystemObject")
set stm=server.createobject("ADODB.Stream")
kch=trans(request.QueryString("kch"))
kch="5201000100002005120001"
links=GetUrl
'response.Write(Server.mappath("."))
'response.end
strsql="select * from xcpic where kch='"&kch&"' and qtype='现场照片' order by lrsj"

'strSQL="select unitname from tblunit where unitid='"&session("unitid")&"'"
set rs = objcn.RunSQLReturnRS(strSQL)
if rs.recordcount=0 then
response.Write("<script language=javascript>alert('暂无现场图片!');window.opener=null;window.close();</script>")
response.end
end if
tablerow=rs.recordcount*2
execute "dim tu("&rs.recordcount&","&rs.recordcount&")"
rs.movefirst
for i=1 to tablerow/2
 tu(i,1)=rs("title")
 If Not IsNull(rs("wjlj")) Then
  AttachmentFile = Server.mappath(".") & "/photo/" & Session.SessionID & "_PICTURE[" & i & "].JPG"
  stm.Mode = 3
  stm.Type = 1
  stm.Open
  stm.Write rs("wjlj")
  stm.SaveToFile AttachmentFile, 2
  stm.Close
 End If
 AttachmentFile1=links&"/xk/photo/" & Session.SessionID & "_PICTURE[" & i & "].JPG"
 'If fso.FileExists(AttachmentFile) Then
 ' fso.DeleteFile AttachmentFile
 'End If
 tu(i,2)=AttachmentFile1
 rs.movenext
next
rs.close
%>

<script language="vbscript">
Dim wApp
Set wApp = CreateObject("Word.Application")
If Err.number > 0 Then
Alert "没法保存为Word文件,请正确安装Word"
else
wApp.visible = True
wApp.Documents.add
wApp.Selection.ParagraphFormat.LineSpacing = 30
wApp.Selection.TypeParagraph
With wApp.Selection
 .Font.Bold = False
    .ParagraphFormat.Alignment = 1
    .Font.Name = "仿宋_GB2312"
    .Font.Size = 16
End With

Set rngCurrent = wApp.Application.ActiveDocument.Paragraphs(1).Range
Set tabCurrent =  wApp.Application.ActiveDocument.Tables.Add(rngCurrent,<%=tablerow%>,1)
<%for i=1 to tablerow/2%>
wApp.Application.ActiveDocument.Tables(1).Rows(<%=(i*2-1)%>).Cells(1).Range.InlineShapes.AddPicture "<%=tu(i,2)%>", False, True
wApp.Application.ActiveDocument.Tables(1).Rows(<%=(i*2-1)%>).Cells(1).Range.ParagraphFormat.alignment=1
wApp.Application.ActiveDocument.Tables(1).Rows(<%=(i*2)%>).Cells(1).Range.InsertAfter "<%=tu(i,1)%>"
wApp.Application.ActiveDocument.Tables(1).Rows(<%=(i*2)%>).Cells(1).Range.ParagraphFormat.alignment=1
<%next%>
end if
</script>

/////最后的删除不要了,是异步的,这么快删除,图片还没导入到word中...
<%
apppath=server.mappath(".")
apppath=apppath&"/photo/"
links1=links&"/xk/photo/"
for i=1 to tablerow/2 
 filepic=replace(tu(i,2),links1,apppath)
 If fso.FileExists(filepic) Then
  fso.DeleteFile filepic
 End If
next
%>
<center><br><strong>数据处理完毕!</strong></center>

<p>
</p><div align='center'><center><input type=button class=buttonface name='cmdOK' onclick="window.returnValue=0;window.close();" value=" 确 定 " ></center></div>

</BODY>
</HTML>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值