【问题标题】:Vbscript can't refresh excel workbook contentVBScript 无法刷新 Excel 工作簿内容
【发布时间】:2019-11-12 22:47:50
【问题描述】:

我想创建一个脚本来更改给定文件夹中每个 excel 文件中的一个单元格以及子文件夹中的所有文件。

我认为我的代码应该可以工作,但由于安全问题而无法刷新工作表(我认为该文件上有一个启用内容锁定,即使我在 excel 中禁用了锁定)。我想知道是否有人可以帮助我弄清楚发生了什么并帮助我找到解决方案?

strDate = "20190831"
strPath = "C:\lefuras_test"

Dim objFSO, objRootFolder, objFil, objXl, objWb, objExcel

Set objExcel = CreateObject("Excel.Application")
Set objFSO = CreateObject("scripting.filesystemobject")
Set objRootFolder = objFSO.getfolder(strPath)

SearchSubFolder objRootFolder

Sub SearchSubFolder(objRootFolder)
    For Each objFil In objRootFolder.Files
        If InStr (objFil.Type, "Excel") > 0 Then
            Set Wb = objExcel.Workbooks.Open(objFil.Path)
            Wb.Sheets("CONFIG").Cells(3,2).Value=strDate
            DisableBackgroundConncections Wb
            Wb.RefreshAll
            p = objFil.Path
            Wb.SaveAs Replace(p, "C:\lefuras_test", "C:\lefuras_test_output")
            Wb.Saved = True
            wscript.echo Wb.name&" elkeszult!"
            Wb.Close True
        End If
    Next
    For Each objFolder in objRootFolder.SubFolders
        SearchSubFolder objFolder
    Next
End Sub

Sub DisableBackgroundConncections(Wb)
    For Each connection in Wb.Connections
        If connection.Type = xlConnectionTypeOLEDB Then
            connection.OLEDBConnection.BackgroundQuery = False
        End If
    Next
End Sub

objExcel.Quit
wscript.echo "Folyamat vege."

【问题讨论】:

  • 对于初学者来说,xlConnectionTypeOLEDB 在 VB 脚本中没有任何意义。将其替换为其文字值 1
  • 谢谢,我更改了值,但问题仍然存在。
  • 您是否收到错误消息?如果有,是什么?

标签: excel vba vbscript


【解决方案1】:

我发现我错了,因为我检查的是 OLEDB 连接而不是 ODBC,但 refreshAll() 仍然不起作用,所以我通过单独刷新所有内容来解决问题。

strDate = "20190830"
strPath = "C:\lefuras_test"
strOutputPath = "C:\lefuras_test_output"

Dim objFSO, objRootFolder, objFil, objXl, objWb, objExcel

Set objExcel = CreateObject("Excel.Application")
Set objFSO = CreateObject("scripting.filesystemobject")
Set objRootFolder = objFSO.getfolder(strPath)

objExcel.AutomationSecurity = 3
objExcel.DisplayAlerts = False
objExcel.AskToUpdateLinks = False
objExcel.AlertBeforeOverwriting = False

SearchSubFolder objRootFolder

Sub SearchSubFolder(objRootFolder)
    For Each objFil In objRootFolder.Files
        If InStr (objFil.Type, "Excel") > 0 Then
            Set Wb = objExcel.Workbooks.Open(objFil.Path)
            Wb.Sheets("CONFIG").Cells(3,2).Value=strDate
            DisableBackgroundConncections Wb
            p = objFil.Path
            Wb.SaveAs Replace(p, strPath, strOutputPath)
            Wb.Saved = True
            wscript.echo Wb.name&" elkeszult!"
            Wb.Close True
        End If
    Next
    For Each objFolder in objRootFolder.SubFolders
        SearchSubFolder objFolder
    Next
End Sub

Sub DisableBackgroundConncections(Wb)
    For Each connection in Wb.Connections
        If connection.Type = 2 Then
            connection.ODBCConnection.BackgroundQuery = False
            connection.ODBCConnection.backgroundquery = false
            connection.ODBCConnection.EnableRefresh = true
            connection.Refresh
        End If
    Next
End Sub

objExcel.Quit
wscript.echo "Folyamat vege."

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-11-26
    • 1970-01-01
    • 2021-05-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-11
    相关资源
    最近更新 更多