【问题标题】:VBScript converting files and locking it with an unknown passwordVBScript 转换文件并用未知密码锁定文件
【发布时间】:2021-04-09 14:36:52
【问题描述】:

我正在使用 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 可以编写吗?

【问题讨论】:

  • 查看Workbook.SaveAs的参数。
  • 即您使用0 作为Call Wb.SaveAs(nPath, convNumber, 0, 0, 0, 0, 0, 0, 0, 0, 0, True) 中的密码。
  • 解决了!谢谢@BigBen,我想这是漫长的一天!

标签: excel csv vbscript


【解决方案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

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.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-01-07
    • 2018-08-28
    • 1970-01-01
    • 2021-04-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多