VBScript 转换文件并用未知密码锁定
VBScript converting files and locking it with an unknown password
我正在使用 VBScript 编写和测试一系列有用的(?)脚本。这个脚本有效,它确实将文件从 CSV 转换为 XLSX,等等......但是:它会在转换时使用未知密码锁定每个 XLS* 文件。我目前没有任何解释,你呢? :)
Option Explicit
'------------------------------------------------------------------
' Author: Fabio Craig Wimmer Florey (@robomancy)
' Last Modified: 2021 - 04 - 09 (yyyy - MM - dd)
' Modifica:
'------------------------------------------------------------------
' 0.1 FUNCTIONS
' - IIf: Ternary operator for VBScript
' - Contains: Just like InStr(), but it return booleans
' - GetFiles: Returns a list of files in a given directory
'------------------------------------------------------------------
Function IIf(bClause, vTrue, vFalse)
If CBool(bClause) Then
IIf = vTrue
Else
IIf = vFalse
End If
End Function
Function Contains(sText, sFind)
Contains = IIf (InStr(1, sText, sFind, 0) > 0, True, False)
End Function
Function GetFiles(sPath)
Dim oFileSystem, oFolder, oFiles
Set oFileSystem = CreateObject("Scripting.FileSystemObject")
Set oFolder = oFileSystem.GetFolder(sPath)
Set GetFiles = oFolder.Files
Set oFileSystem = Nothing
End Function
'-------------------------------------------------------------------
' 0.2 SUBROUTINES
' - ReFormatExcel: Converts each file with a given extension
' in another file with a given extesion.
'-------------------------------------------------------------------
Sub ReFormatExcel(sPath,firstExt,secondExt, convNumber)
' Check the right convNumber at:
' https://docs.microsoft.com/en-us/office/vba/api/excel.xlfileformat
Dim oFileSystem, oFile
Set oFileSystem = CreateObject("Scripting.FileSystemObject")
For Each oFile in GetFiles(sPath)
If LCase(oFileSystem.GetExtensionName(oFile.Path)) = firstExt Then
Dim nPath
Dim oExcel, Wb
Set oExcel = CreateObject("Excel.Application")
oExcel.DisplayAlerts = False
oExcel.Visible = False
Set Wb = oExcel.Workbooks.Open(oFile.Path)
nPath = Replace(oFile.Path,"."&firstExt,"."&secondExt)
Call Wb.SaveAs(nPath, convNumber, 0, 0, 0, 0, 0, 0, 0, 0, 0, True)
Wb.Close False
oExcel.Quit
Set Wb = Nothing
Set oExcel = Nothing
oFileSystem.DeleteFile oFile.Path
End If
Next
Set oFileSystem = Nothing
End Sub
'-------------------------------------------------------------------
' 1.0 ARGUMENTS
'
'-------------------------------------------------------------------
'-------------------------------------------------------------------
' 1.1 SCRIPT
'-------------------------------------------------------------------
Call ReFormatExcel("myPath\test\","csv","xlsx",51)
奖金问题:您还有其他function\sub您认为有用的问题吗?
在这种情况下,当您调用以下命令时,您似乎将 0 分配给了所有可选值。
Call Wb.SaveAs(nPath, convNumber, 0, 0, 0, 0, 0, 0, 0, 0, 0, True)
https://docs.microsoft.com/en-us/office/vba/api/excel.workbook.saveas
Name
Required/Optional
Data type
Description
Password
Optional
Variant
A case-sensitive string (no more than 15 characters) that indicates the protection password to be given to the file.
我正在使用 VBScript 编写和测试一系列有用的(?)脚本。这个脚本有效,它确实将文件从 CSV 转换为 XLSX,等等......但是:它会在转换时使用未知密码锁定每个 XLS* 文件。我目前没有任何解释,你呢? :)
Option Explicit
'------------------------------------------------------------------
' Author: Fabio Craig Wimmer Florey (@robomancy)
' Last Modified: 2021 - 04 - 09 (yyyy - MM - dd)
' Modifica:
'------------------------------------------------------------------
' 0.1 FUNCTIONS
' - IIf: Ternary operator for VBScript
' - Contains: Just like InStr(), but it return booleans
' - GetFiles: Returns a list of files in a given directory
'------------------------------------------------------------------
Function IIf(bClause, vTrue, vFalse)
If CBool(bClause) Then
IIf = vTrue
Else
IIf = vFalse
End If
End Function
Function Contains(sText, sFind)
Contains = IIf (InStr(1, sText, sFind, 0) > 0, True, False)
End Function
Function GetFiles(sPath)
Dim oFileSystem, oFolder, oFiles
Set oFileSystem = CreateObject("Scripting.FileSystemObject")
Set oFolder = oFileSystem.GetFolder(sPath)
Set GetFiles = oFolder.Files
Set oFileSystem = Nothing
End Function
'-------------------------------------------------------------------
' 0.2 SUBROUTINES
' - ReFormatExcel: Converts each file with a given extension
' in another file with a given extesion.
'-------------------------------------------------------------------
Sub ReFormatExcel(sPath,firstExt,secondExt, convNumber)
' Check the right convNumber at:
' https://docs.microsoft.com/en-us/office/vba/api/excel.xlfileformat
Dim oFileSystem, oFile
Set oFileSystem = CreateObject("Scripting.FileSystemObject")
For Each oFile in GetFiles(sPath)
If LCase(oFileSystem.GetExtensionName(oFile.Path)) = firstExt Then
Dim nPath
Dim oExcel, Wb
Set oExcel = CreateObject("Excel.Application")
oExcel.DisplayAlerts = False
oExcel.Visible = False
Set Wb = oExcel.Workbooks.Open(oFile.Path)
nPath = Replace(oFile.Path,"."&firstExt,"."&secondExt)
Call Wb.SaveAs(nPath, convNumber, 0, 0, 0, 0, 0, 0, 0, 0, 0, True)
Wb.Close False
oExcel.Quit
Set Wb = Nothing
Set oExcel = Nothing
oFileSystem.DeleteFile oFile.Path
End If
Next
Set oFileSystem = Nothing
End Sub
'-------------------------------------------------------------------
' 1.0 ARGUMENTS
'
'-------------------------------------------------------------------
'-------------------------------------------------------------------
' 1.1 SCRIPT
'-------------------------------------------------------------------
Call ReFormatExcel("myPath\test\","csv","xlsx",51)
奖金问题:您还有其他function\sub您认为有用的问题吗?
在这种情况下,当您调用以下命令时,您似乎将 0 分配给了所有可选值。
Call Wb.SaveAs(nPath, convNumber, 0, 0, 0, 0, 0, 0, 0, 0, 0, True)
https://docs.microsoft.com/en-us/office/vba/api/excel.workbook.saveas
Name | Required/Optional | Data type | Description |
---|---|---|---|
Password | Optional | Variant | A case-sensitive string (no more than 15 characters) that indicates the protection password to be given to the file. |