以读写方式打开工作簿并在关闭时保存

时间:2015-02-11 18:04:45

标签: excel vba

所以我得到了这个代码,但是它在目录中以只读方式打开我的所有工作簿。我需要保存它,因为它已经破坏宏然后关闭它...我相信这是我想改变的路线,但我不知道它的哪一部分

oExcel.Workbooks.Open(oFile, 0, True, , , , True, , , , False, , False)

以下是所有代码

Dim oFSO
Dim oShell, oExcel, oFile, oSheet
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oShell = CreateObject("WScript.Shell")
Set oExcel = CreateObject("Excel.Application")
Set wb2 = oExcel.Workbooks.Open ("C:\Users\kp.AD\AppData\Roaming      \Microsoft\Excel\XLSTART\CoventryStoplossMacroBook.xlsb") 'Specify foldername     here


oExcel.DisplayAlerts = True


For Each oFile In oFSO.GetFolder("C:\Users\kpacheco.AD\Desktop\MSO_PR    \macro_test").Files
  If LCase(oFSO.GetExtensionName(oFile)) = "xls" Then
With oExcel.Workbooks.Open(oFile, 0, True, , , , True, , , , False, , False)


  'calling macro for MSO Eligibility reports
     oExcel.Run wb2.Name & "!MSOElig2"

 'I think im suppose to change the below to true but since it is read only    'it asks me to save a copy
     .Close False, , False
    End With
  End If



Next


wb2.Save

wb2.Close


oExcel.Quit
oShell.Popup "Conversion complete", 10

1 个答案:

答案 0 :(得分:1)

ReadOnly属性在第三个位置标记为True。 它应该是False,以便以读写方式打开:

oExcel.Workbooks.Open(oFile, 0, False, , , , True, , , , False, , False)

详细了解official documentation上的Workbooks.Open()方法。

要保存工作簿,您可以单独使用SaveClose方法;而不是:

.Close False, , False

为了更清楚我会使用:

Application.DisplayAlerts = False '<-- if you don't want the alert
wb2.Save 
Application.DisplayAlerts = True '<-- set it back
wb2.Close