【问题标题】:VBS that triggers a macro via HTA (Cannot run the macro)通过 HTA 触发宏的 VBS(无法运行宏)
【发布时间】:2016-05-24 19:53:42
【问题描述】:

我有一个 HTA 脚本,它最终会触发代码的 vbs 部分来调用 VBA 宏,但由于某种原因,我收到了这个错误:

我已经确保启用对 VBA 的信任访问,所以我觉得我的代码中的某些内容与 HTA 不兼容。这是代码:提前感谢您的时间。

<html>
 <title>Report Generation</title>
<head>
<HTA:APPLICATION 
 APPLICATIONNAME="Report Generation"
 ID="objHTA"  
 SCROLL="yes"
 SINGLEINSTANCE="yes"
 WINDOWSTATE="normal">
</head>

<style>
BODY
{
background-color: buttonface;
Font: arial,sans-serif
margin-top: 10px;
margin-left: 20px;
margin-right: 20px;
margin-bottom: 5px;
}
.button
{
width: 91px;
height: 25px;
font-family: arial,sans-serif;
font-size: 8pt;
}
td
{
font-family: arial,sans-serif;
font-size: 10pt;
}                     
#scroll
{
height:100%;
overflow:auto;
}
SELECT.FixedWidth 
{
width: 17em;  /* maybe use px for pixels or pt for points here */
}
</style>

<script language="vbscript">
Option Explicit    

Dim WinWidth : WinWidth = 350
Dim WinHeight : WinHeight = 250
Window.ResizeTo WinWidth, WinHeight


Sub Sleep(lngDelay)
  CreateObject("WScript.Shell").Run "Timeout /T " & lngDelay & " /nobreak", 0, True
End Sub

Sub CheckBoxChange
  If CheckBox(0).Checked Then
  ExecuteScoreCard
  Else
  MsgBox "CheckBox is not checked"
  End If
End Sub

Sub ExecuteScoreCard() 
  Dim sitecode
  Dim objExcel  
  Dim objWorkbook
  Dim objSheet

  dim fso: set fso = CreateObject("Scripting.FileSystemObject")
  dim path: path = fso.GetAbsolutePathName(".")

  Set objExcel = CreateObject("Excel.Application")
  Set objWorkbook = objExcel.Workbooks.Open(path & "\Scorecard.xlsm")
  Set objSheet = objWorkbook.Worksheets("Cover Tab") 

  sitecode = document.getElementById("sitecode").value

  objSheet.Cells(4, 2) = sitecode
  objExcel.Run "RefreshConns"
  Sleep 75 

  objExcel.ActiveWorkbook.SaveAs path & "\Scorecards\" & "Scorecard_" & sitecode & "_" & Year(Now()) & Month(Now()) & Day(Now()) & "_" & Hour(Now()) & Minute(Now()) &".xlsm", 52
  objExcel.ActiveWorkbook.Close
  objExcel.Quit   

  MsgBox("Successfully generated scorecard.")

End Sub 

 Sub ExitProgram
  window.close()
 End Sub

</script>

<body>
  Site Code: <input type="inputbox" name="sitecode" id="sitecode">
<br>
  <input type="checkbox" name="CheckBox"> Scorecard
<br>
  <input type="checkbox" name="CheckBox"> Report2
<br>
  <input type="checkbox" name="CheckBox"> Report3
<br>
<br>
  <input type="submit" name="accept" value="Submit" onclick="CheckBoxChange">
  <input type="button" value="Exit" onClick="ExitProgram">
</body>
</html> 

【问题讨论】:

  • RefreshConns 例程在工作簿中确切的位置?
  • @Rory 它位于工作簿 VBAProject 文件夹中的一个模块中
  • @Rory 抱歉,这是“Module2”
  • 使用objExcel.Run "Scorecard.xlsm!Module2.RefreshConns"是否有效?
  • @Rory 不,它认为工作簿在“文档”中。我试过: "objExcel.Run "objWorkbook!Module2.RefreshConns" 它引用了我的直接路径,但也没有用。

标签: vba excel vbscript macros hta


【解决方案1】:

objWorkbook.RunAutoMacros(1)

在 objExcel.Run "RefreshConns" 行之前将上述代码行包含在 HTA 代码中。

【讨论】:

  • 据我所知,我不再可以访问 VBS,我无法测试这是否有效,但如果我被要求再次更新它,我会记下它。非常感谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-11-06
相关资源
最近更新 更多