簡體   English   中英

對象模型的 VBA 測試

[英]VBA Test for Object Model

我正在嘗試編寫一個適用於 PPT 2010 和 2013-2016 的 PPT 宏。 2013-16 對象模型 ( ActivateChartDataWindow ) 中有一個對象不在我想要使用的 2010 模型中。 我想到了使用這樣的代碼來測試應用程序版本,然后使用正確的對象:

With theChart.ChartData
    If CInt(Application.Version) >= 15 Then
        .ActivateChartDataWindow
    Else
        .Activate
    End If
....
End With

問題是這不會在 2010 年編譯,因為找不到.ActivateChartDataWindow對象。 所以不會有運行時錯誤,但有編譯時錯誤。

做到這一點的最佳方法是什么? 有沒有辦法在代碼本身中禁用編譯時檢查?

您正在撥打早期綁定的會員電話; 如果代碼包含無法使用類型庫的早期版本編譯的成員調用,則解決方案是切換到后期綁定調用,根據定義,這些調用僅在運行時綁定(即沒有編譯時驗證)。

因此,而不是With theChart.ChartData ,您聲明一個Object變量,並將其SettheChart.ChartData

Dim lateBoundChartData As Object
Set lateBoundChartData = theChart.ChartData

現在,針對該lateBoundChartData進行的任何成員調用都將僅在運行時進行驗證 - 注意拼寫錯誤, Option Explicit無法幫助您!

With lateBoundChartData
    If CInt(Application.Version) >= 15 Then
        'no intellisense & compile-time validation here
        .ActivateChartDataWindow
    Else
        'no intellisense & compile-time validation here
        .Activate
    End If
End With

有趣的是,人們一直在編寫后​​期綁定的代碼,甚至沒有意識到:您針對Object編寫的任何內容總是會被后期綁定。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM