计算不是 Application 或 ActiveWorkbook 的方法

Calculation not a method of Application or ActiveWorkbook

我在 MS-Project 中编写此代码,但我认为我包含了与 Excel 接口所需的所有库。

这是我尝试过的方法:

Sub OpenBackupFile()
    Titler = ActiveProject.CustomDocumentProperties("Title").Value
    BackupFile = "C:\POAMLogs\" & Titler & ".xlsx"


    'Set ExcelBackerp = CreateObject("Excel.Application")
    Set ExcelBackerp = New Excel.Application
    With ExcelBackerp

        .Calculation = xlCalculationManual

        .EnableEvents = False
        .ScreenUpdating = False
        .DisplayAlerts = False
        .Workbooks.Open BackupFile

    End With

End Sub

无论我做什么,我都得到了,对象“_Application”的方法"Calculation"失败 我可以让 intellisense 给我第一个作为选项,但在第二个 'calculation' 中没有出现在 intellisense 中。

I can get intellisense to give me the first one as an option, but in the second one 'calculation' doesn't show up in intellisense.

那是因为 .Calculation 不是 Workbook 的 属性。这是 Application

您可以使用 ExcelBackerp.Calculation = xlCalculationManual 但不能使用 ExcelBackerp.ActiveWorkbook.Calculation = xlCalculationManual

另外请记住,如果您曾经使用过后期绑定,请将 xlCalculationManual 更改为 -4135

您遇到错误,因为您在没有工作簿的情况下尝试设置计算。

试试这个,它会起作用

Set ExcelBackerp = New Excel.Application
ExcelBackerp.Workbooks.Add   
ExcelBackerp.Calculation = xlCalculationManual

所以在你的代码中先打开工作簿然后设置计算。 :)

Sub OpenBackupFile()
    Titler = ActiveProject.CustomDocumentProperties("Title").Value
    BackupFile = "C:\POAMLogs\" & Titler & ".xlsx"

    'Set ExcelBackerp = CreateObject("Excel.Application")
    Set ExcelBackerp = New Excel.Application
    With ExcelBackerp
        .Workbooks.Open BackupFile
        .Calculation = xlCalculationManual
        .EnableEvents = False
        .ScreenUpdating = False
        .DisplayAlerts = False
    End With
End Sub

或更好,声明您的 variables/objects。会让你的生活更轻松:)

Sub OpenBackupFile()
    Titler = ActiveProject.CustomDocumentProperties("Title").Value
    BackupFile = "C:\POAMLogs\" & Titler & ".xlsx"

    Dim ExcelBackerp As Excel.Application
    Dim wb As Excel.Workbook

    Set ExcelBackerp = New Excel.Application

    With ExcelBackerp
        Set wb = .Workbooks.Open(BackupFile)

        .Calculation = xlCalculationManual
        .EnableEvents = False
        .ScreenUpdating = False
        .DisplayAlerts = False
    End With

    '~~> Work with the workbook here
    With wb

    End With
End Sub