excel - Use vbscript to input data in Excel userform and proceed
问题描述
Thank you in advance for considering my post. I have an Excel file that contains a userform with a username and password field that appears everytime i open the Excel. Also, some of the worksheets are protected. Being new to vbscripting, I wanted to know how I can use it to:
- open the workbook in the background
- input the username and password in the excel userform
- click the submit button from the userform
- run the refresh all macro found in Module1
So far I was able to open the workbook in the backgroung and test it without the userform but since I cannot enter the username and password, I am unable to proceed further.
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
解决方案
First : Your file has an extension with .xlsm
and not .xlsx
check this before proceeding !
Second : You should add this instruction in the vbscript : oWorkbook.Unprotect 1234
to unprotect the WorkBook (The password by default that i found is 1234
in your Excel file)
Third : I added On Error Resume Next
instruction to catch error if it occurs
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
推荐阅读
- javascript - 如何通过 Google 幻灯片上的链接/按钮调用函数?
- docker - 如何找出我已登录的注册表?
- reactjs - 如何调度多个动作
- javascript - Firefox 开发者工具:JavaScript 文件显示为 HTML
- firefox - SVG 过滤器在 Firefox 上导致意外裁剪
- ios - 从 Swift 5 iOS 应用程序进行 Mailchimp API 调用时“缺少 API 密钥”
- office-ui-fabric - 我可以/应该使用 Fabric React Stack 元素来布局我的 React Web 应用程序吗?
- html - Django html表单从按钮获取价值
- c - 矩阵乘法 C
- java - 理解 Java 中的方法/类调用