VBScript 转换文件并使用未知密码将其锁定

时间:2021-04-09 14:36:52

标签: excel csv vbscript

我正在使用 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)

额外的问题:你有没有其他你认为有用的函数\sub来编写?

1 个答案:

答案 0 :(得分:-1)

在这种情况下,当您调用以下代码时,您似乎为所有可选值分配了 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

<头>
姓名 必需/可选 数据类型 说明
密码 可选 变体 区分大小写的字符串(不超过 15 个字符),表示要提供给文件的保护密码。
相关问题