我在 excel 檔案中有一個宏,每天使用任務計劃程式和我直接從這里復制的 vbs 腳本自動觸發一次: https ://www.thespreadsheetguru.com/blog/how-to-automatically-run- excel-vba-宏-每日
問題是,用戶通常已經打開了檔案,這意味著自動化實體無法打開和運行。一種想法是默認將 excel 檔案設為只讀(用戶大多只想查看和復制檔案中的資料)。但是現在,vbs 腳本無法在編輯模式下打開檔案。我在“打開 Excel 檔案”步驟中嘗試了一些不同的操作,如下所示,但沒有一個選項有效 - 每個選項都運行并完成,但檔案未保存 - 我猜是因為 excel 仍以讀取方式打開-僅模式。有什么想法可以讓宏能夠編輯檔案,同時仍然使用戶更難在編輯模式下打開檔案?
'Input Excel File's Full Path
ExcelFilePath = "C:\Users\User1\Documents\ReadOnlyTest.xlsm"
'Input Module/Macro name within the Excel File
MacroPath = "Module1.ATest"
'Create an instance of Excel
Set ExcelApp = CreateObject("Excel.Application")
'Do you want this Excel instance to be visible?
ExcelApp.Visible = True 'or "False"
'Prevent any App Launch Alerts (ie Update External Links)
ExcelApp.DisplayAlerts = False
'Open Excel File
Set wb = ExcelApp.Workbooks.Open(ExcelFilePath, ReadOnly=False)
' Set wb = ExcelApp.Workbooks.Open(ExcelFilePath, IgnoreReadOnlyRecommended=True)
'If wb.Application.ProtectedViewWindows.Count > 0 Then ' this one taken from here: https://stackoverflow.com/questions/52110678/vbscript-enable-editing-in-excel-file-save-as.
'wb.Application.ActiveProtectedViewWindow.Edit
'End If
'Execute Macro Code
ExcelApp.Run MacroPath
'Save Excel File (if applicable)
wb.Save
'Reset Display Alerts Before Closing
ExcelApp.DisplayAlerts = True
'Close Excel File
wb.Close
'End instance of Excel
ExcelApp.Quit
uj5u.com熱心網友回復:
請測驗下一個適應的方式:
Dim ExcelFilePath, MacroPath, wb
'Input Excel File's Full Path
ExcelFilePath = "C:\Users\User1\Documents\ReadOnlyTest.xlsm"
'Change ReadOnly attribute
CreateObject("Scripting.FileSystemObject").GetFile(ExcelFilePath).Attributes = 0
'Input Module/Macro name within the Excel File
MacroPath = "Module1.ATest"
'Create an instance of Excel
Set ExcelApp = CreateObject("Excel.Application")
'Do you want this Excel instance to be visible?
ExcelApp.Visible = True 'or "False"
'Open Excel File
Set wb = ExcelApp.Workbooks.Open(ExcelFilePath)
'Execute Macro Code
ExcelApp.run "'" & ExcelFilePath & "'" & "!" & MacroPath
'Close Excel File (and save it)
wb.Close True
'End instance of Excel
ExcelApp.Quit
'Change the workbook ReadOnly attribute to True
CreateObject("Scripting.FileSystemObject").GetFile(ExcelFilePath).Attributes = 1
如果仍然沒有運行宏,則應將要打開的作業簿路徑添加到 Excel Trusted Locations( Options - Trust Center - Trust Center Settings... - Trusted Locations - Add New location...)。
General如果仍然有問題,請嘗試在您創建的任務的選項卡上檢查“以最高權限運行” (在任務計劃程式中)。
uj5u.com熱心網友回復:
如果您在作業系統級別將作業簿設定為只讀,您將無法保存檔案 - 因為作業系統阻止了此操作(不是 Excel)。
一種解決方案可能是在腳本中打開檔案之前重置 ReadOnly-Flag。使用該SetAttr陳述句;0 代表正常,1 代表只讀。在 VBA 中,您可以使用 vbNormal 和 vbReadOnly,但它們沒有在 VBScript 中定義。請注意,這是未經測驗的。
SetAttr ExcelFilePath, 0
Set wb = ExcelApp.Workbooks.Open(ExcelFilePath)
(...)
wb.Close
SetAttr ExcelFilePath, 1
另一種選擇是ThisWorkbook.ChangeFileAccess xlReadOnly在作業簿的 Open-Trigger 中使用。在這種情況下,您需要在使用腳本 ( ) 打開檔案之前禁用事件ExcelApp.EnableEvents = False。
然而,這有點危險:除非您手動禁用事件(例如通過即時視窗),否則您將無法自己以寫入模式打開檔案。或者您可以為當前用戶添加一個檢查,environ("Username")如果是您正在打開檔案,請跳過 ChangeFileAccess-statement
更新
(1)似乎SetAttr在 VBS 中不起作用,但您可以使用 FileSystemObject
dim fso, f
set fso = createObject("Scripting.FileSystemObject")
set f = fso.GetFile(ExcelFilePath)
Const ReadOnly = 1
' Remove Readonly-Flag
if (f.attributes AND ReadOnly) <> 0 Then f.attributes = f.attributes - ReadOnly
(...)
' Reset Readonly-Flag
if (f.attributes AND ReadOnly) = 0 Then f.attributes = f.attributes ReadOnly
(2) 如果你想去 VBA 解決方案:當你使用 ChangeFileAccess to 時xlReadWrite,Excel 將關閉當前檔案并重新打開它。但重新打開將觸發 Workbook_Open 事件,該事件再次將檔案設定為只讀。您需要禁用事件以防止執行打開觸發器。
Sub SetReadOnly()
If Not ThisWorkbook.ReadOnly Then
ThisWorkbook.ChangeFileAccess xlReadOnly
End If
End Sub
Sub SetReadWrite()
If ThisWorkbook.ReadOnly Then
Application.EnableEvents = False
On Error GoTo SetReadWrite_Exit
ThisWorkbook.ChangeFileAccess xlReadWrite
End If
SetReadWrite_Exit:
Application.EnableEvents = True
End Sub
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/410930.html
標籤:
