当前位置: 代码迷 >> vbScript >> 高手来看看这段VBS操作EXCEL的代码,JAVA没搞定,来这里拜山了~解决方案
  详细解决方案

高手来看看这段VBS操作EXCEL的代码,JAVA没搞定,来这里拜山了~解决方案

热度:10141   发布时间:2013-02-26 00:00:00.0
高手来看看这段VBS操作EXCEL的代码,JAVA没搞定,来这里拜山了~
例子是一个VBS操纵EXCEL的例子

想达到的效果是,用VBS打开EXCEL给用户看,当用户看完后自动删除这个EXCEL文件,由于操作都在HTA的VBA里进行,所以在用户未关闭EXCEL文件时,强制冻结HTA,使之无法关闭。
大意就是这样,期待高手来帮忙

在JAVA那边问了两个帖子合计150分,没有搞定,所以来这边,小弟是JAVA新人,VBS前天才开始看,不会的地方请不吝赐教,分少了点,不过真心求解,如果能有别的解决方法也可以

想要的效果就是当用户关闭EXCEL文件后,将此EXCEL文件删除。

VBScript code
Option Explicit    Sub Window_OnLoadon error resume next    Dim m_Excel    Dim m_FSO    Const m_FILENAME = "要删除的文件.xls"Set m_FSO = CreateObject("Scripting.FileSystemObject")Set m_Excel = CreateObject("Excel.Application")With m_Excel        .Workbooks.Open(m_FILENAME)        .Visible = True        .DisplayAlerts=False        .WindowState = -4137    'xlMaximized        .ActiveWindow.WindowState = -4137    'xlMaximized            .CommandBars("Worksheet Menu Bar").Enabled = TrueEnd With' 等待EXCEL结束        Dim m_WindowCount        m_WindowCount = m_Excel.Workbooks.count        Do Until m_WindowCount = 0            Sleep 2000  '此处未执行...            m_WindowCount = m_Excel.Workbooks.Count        Loop' 结束进程    Set m_Excel = Nothing' 删除文件    if m_FSO.FileExists(m_FILENAME) = True Then        m_FSO.DeleteFile m_FILENAME    End if' 关闭HTA    Set m_FSO = Nothing    Self.CloseEnd Sub


------解决方案--------------------------------------------------------
去掉sleep就可以了.

------解决方案--------------------------------------------------------
HTML code
<!doctype html public "-//W3C//DTD HTML 4.0//EN"><html lang="ja"><head><meta http-equiv="Content-Type" content="text/html; charset=Shift_JIS"><script type="text/vbscript" language="vbscript">Option ExplicitDim m_ExcelDim m_FSODim tidConst m_FILENAME = "d:\xx.xls"tid = NullSub check   If Not IsNull(tid) Then       window.clearTimeout tid       tid = Null   End If   Dim m_WindowCount   m_WindowCount = m_Excel.Workbooks.count   If m_WindowCount = 0 Then    ' 结束进程        Set m_Excel = Nothing        ' 删除文件        if m_FSO.FileExists(m_FILENAME) = True Then            m_FSO.DeleteFile m_FILENAME        End if        ' 关闭HTA        Set m_FSO = Nothing        Self.Close   Else       tid = window.setTimeout("check", 2000)   End IfEnd SubSub Window_OnLoad    'on error resume nextSet m_FSO = CreateObject("Scripting.FileSystemObject")Set m_Excel = CreateObject("Excel.Application")With m_Excel        .Workbooks.Open(m_FILENAME)        .Visible = True        .DisplayAlerts=False        .WindowState = -4137    'xlMaximized        .ActiveWindow.WindowState = -4137    'xlMaximized            .CommandBars("Worksheet Menu Bar").Enabled = TrueEnd Withtid = window.setTimeout("check", 2000)End Sub</script></head><body bgcolor="#eeeeee"><center><br><a>请等待...</a></center></body></html>
  相关解决方案