使用 vbscript 在 Excel 用户窗体中输入数据并继续
Use vbscript to input data in Excel userform and proceed
提前感谢您考虑我的 post。
我有一个 Excel 文件,其中包含一个带有用户名和密码字段的用户表单,每次我打开 Excel 时都会出现该字段。此外,一些工作表受到保护。
作为 vbscripting 的新手,我想知道如何使用它来:
- 在后台打开工作簿
- 在excel用户表单中输入用户名和密码
- 点击用户表单中的提交按钮
- 运行 刷新模块 1 中找到的所有宏
到目前为止,我能够在后台打开工作簿并在没有用户表单的情况下对其进行测试,但由于我无法输入用户名和密码,因此无法继续。
Dim oExcel
Set oExcel = CreateObject("Excel.Application")
oExcel.Visible = False
oExcel.DisplayAlerts = False
oExcel.AskToUpdateLinks = False
oExcel.AlertBeforeOverwriting = False
Set oWorkbook = oExcel.Workbooks.Open("filepath\file.xlsx")
oWorkbook.RefreshAll
oWorkbook.Save
Msgbox "Excel file has been refreshed", VBOKOnly
oExcel.Quit
Set oWorkbook = Nothing
Set oExcel = Nothing
首先: 您的文件的扩展名是 .xlsm
而不是 .xlsx
在继续之前检查一下!
其次: 您应该在 vbscript 中添加此指令:oWorkbook.Unprotect 1234
以取消保护工作簿(默认密码我发现 1234
在你的 Excel 文件中)
第三个: 我添加了 On Error Resume Next
指令以在发生错误时捕获错误
Option Explicit
Dim Title,oExcel,oWorkbook
Title = "Use vbscript to input data in Excel userform and proceed"
Set oExcel = CreateObject("Excel.Application")
oExcel.Visible = False
oExcel.DisplayAlerts = False
oExcel.AskToUpdateLinks = False
oExcel.AlertBeforeOverwriting = False
On Error Resume Next
Set oWorkbook = oExcel.Workbooks.Open("C:\Stack\Data.xlsm")
oWorkbook.Unprotect 1234 ' The password to unprotect the WorkBook is 1234
oWorkbook.RefreshAll
oWorkbook.Save
If Err Then
MsgBox "Error Source : " & Err.Source & vbCrlf &_
"Error Description : "& Err.Description,vbCritical,Title
Else
Msgbox "Excel file has been refreshed",vbInformation,Title
End If
oExcel.Quit
Set oWorkbook = Nothing
Set oExcel = Nothing
提前感谢您考虑我的 post。 我有一个 Excel 文件,其中包含一个带有用户名和密码字段的用户表单,每次我打开 Excel 时都会出现该字段。此外,一些工作表受到保护。 作为 vbscripting 的新手,我想知道如何使用它来:
- 在后台打开工作簿
- 在excel用户表单中输入用户名和密码
- 点击用户表单中的提交按钮
- 运行 刷新模块 1 中找到的所有宏
到目前为止,我能够在后台打开工作簿并在没有用户表单的情况下对其进行测试,但由于我无法输入用户名和密码,因此无法继续。
Dim oExcel
Set oExcel = CreateObject("Excel.Application")
oExcel.Visible = False
oExcel.DisplayAlerts = False
oExcel.AskToUpdateLinks = False
oExcel.AlertBeforeOverwriting = False
Set oWorkbook = oExcel.Workbooks.Open("filepath\file.xlsx")
oWorkbook.RefreshAll
oWorkbook.Save
Msgbox "Excel file has been refreshed", VBOKOnly
oExcel.Quit
Set oWorkbook = Nothing
Set oExcel = Nothing
首先: 您的文件的扩展名是 .xlsm
而不是 .xlsx
在继续之前检查一下!
其次: 您应该在 vbscript 中添加此指令:oWorkbook.Unprotect 1234
以取消保护工作簿(默认密码我发现 1234
在你的 Excel 文件中)
第三个: 我添加了 On Error Resume Next
指令以在发生错误时捕获错误
Option Explicit
Dim Title,oExcel,oWorkbook
Title = "Use vbscript to input data in Excel userform and proceed"
Set oExcel = CreateObject("Excel.Application")
oExcel.Visible = False
oExcel.DisplayAlerts = False
oExcel.AskToUpdateLinks = False
oExcel.AlertBeforeOverwriting = False
On Error Resume Next
Set oWorkbook = oExcel.Workbooks.Open("C:\Stack\Data.xlsm")
oWorkbook.Unprotect 1234 ' The password to unprotect the WorkBook is 1234
oWorkbook.RefreshAll
oWorkbook.Save
If Err Then
MsgBox "Error Source : " & Err.Source & vbCrlf &_
"Error Description : "& Err.Description,vbCritical,Title
Else
Msgbox "Excel file has been refreshed",vbInformation,Title
End If
oExcel.Quit
Set oWorkbook = Nothing
Set oExcel = Nothing