會設定一個或多個儲存格的結果或公式。
語法
運算式。
SetResults( _SID_SRCStream()_ , _UnitsNamesOrCodes()_ , _resultArray()_ , _Flags_ )
運算式 代表 Master 物件的變數。
參數
| 名稱 | 必要/選用 | 資料類型 | 描述 |
|---|---|---|---|
| SID_SRCStream () | 必要 | 整數 | 識別要修改之儲存格的陣列。 |
| UnitsNamesOrCodes () | 必要 | Variant | 歸類結果陣列中的項目所用的度量單位。 |
| resultArray () | 必要 | Variant | 要指派給識別的儲存格之結果或公式。 |
| Flags | 必要 | 整數 | 會影響 SetResults 行為的旗標。 |
傳回值
整數
註解
SetResults 方法就像 Cell 物件的 Result 方法,但是有一點例外,就是可以使用這個方法一次設定很多儲存格的結果 (值),而不是一次設定一個儲存格的結果 (值)。
針對 主物件 ,您可以使用 SetResults 方法來設定頁面或主片的任何圖形集中任何一組儲存格的結果。 您可以透過在 _SID_SRCStream () 中傳遞整數陣列,告訴 SetResults 方法要設定哪些儲存格。 SID_SRCStream () 是 2 位元組整數的一維陣列。
針對主物件,SID_SRCStream () 應該是 n>= 1 的 4 n 個 2 位元組整數的一維陣列。 SetResults 方法會將資料流程解譯為:
{sheetID, sectionIdx, rowIdx, cellIdx }n
其中的 sheetID 為頁面或主圖形上的 Shape 物件的 ID 屬性 (要修改此頁面或主圖形的儲存格結果)。
如果項目中的 sheetID 為 visInvalShapeID (-1) ,或 sectionIdx 的底部位元組為 visSectionInval (255) ,則 SetResults 方法會忽略該項目。 這樣做的動機是相同的 SID_SRCStream () 陣列可用於對 SetResults、 GetResults 和類似方法的數次呼叫,而呼叫端只需要在呼叫之間對資料流程進行次要變更。
UnitsNamesOrCodes 陣列 () 控制結果中個別項目的度量單位。 此陣列中的每一個項目都可以是類似 "inches"、"inch"、"in." 或 "i" 等字串。 字串可用於所有支援的 Microsoft Office Visio 單位,例如公分、公尺、英里等。 您也可以使用 VisUnitCodes 中 Visio 型別程式庫所宣告的整數常數 (visCentimeters、visInches 等來表示所需的單位) 。 如需用於測量單位的常數清單,請參閱 關於測量單位。 請注意,如果在 [旗標] 中設定了 visSetFormulas,則 () UnitsNamesOrCodes 陣列中指定的值沒有作用。
如果 UnitsNamesOrCodes () 不是空,我們預期它是包含 1 <= u 個變體的一維陣列。 每個項目都可以是字串或整數代碼,或空白 (沒有任何) 。 如果第 i 個項目是空的,則 resultArray () 中的第 i 個項目會在 UnitsNamesOrCodes (j) 所指定的單位中,其中 j 是最新的非空前一個項目。 因此,如果您想要以相同的單位解譯 resultArray () 中的所有項目,則只需要傳遞具有一個項目的 UnitsNamesOrCodes () 陣列。 如果沒有非空的先前項目,或沒有提供 UnitsNamesOrCodes () 陣列,則會使用 visNumber (0x20) 。 這會導致應用程式預設為內部單位 (就像 Cell 物件) 的 ResultIU 屬性一樣。
resultArray () 參數應該是包含 1 <= m 個變體的一維陣列。 結果可以傳遞為 Double、 Integer、 String 或字 串參照。 只有在 [旗標] 中設定 visSetFormulas 時,才會接受字串,在這種情況下,字串會解譯為公式。 如果 resultArray (i) 為空,則第 i 個儲存格將設定為 resultArray 中的值 (j) ,其中 j 是最近的非空前一個項目的索引。 如果沒有非空的先前項目,則不會變更對應的儲存格。 如果指定的結果少於儲存格 (如果 m < n ) ,則第 i 個儲存格 i < m 會設定為與第 m 個儲存格相同的值。 因此,要將多個單元格設置為相同的值,您只需傳遞一個值副本。
Flags 參數應是下列值的位元遮罩。
| 常數 | 值 | 描述 |
|---|---|---|
| visSetFormulas | &H1 | 將結果中的字串視為公式。 |
| visSetBlastGuards | &H2 | 覆寫目前的儲存格值 (即使這些值有受到保護)。 |
| visSetTestCircular | &H4 | 測試儲存格循環參考的建立。 |
| visSetUniversalSyntax | &H8 | 公式在通用語法中。 |
SetResults 方法傳回的值是SID_SRCStream () 中已成功處理的項目數。 如果 i < n 個項目已正確處理,但 i + 第一個項目發生錯誤, SetResults 方法會引發例外狀況並傳回 i。 否則會傳回 n 。
範例
下列範例顯示如何使用 SetResults 方法。 此範例假設有一個至少有 3 個圖形的作用中頁面。 它使用 GetResults 方法來取得圖形 1 的寬度、圖形 2 的高度,以及圖形 3 的角度。 接著,它會使用 SetResults 將圖形 1 的寬度設定為圖形 2 的高度,並將圖形 2 的高度設定為圖形 1 的寬度。圖形 3 的角度不會改變。
這個範例會使用 Page 物件的 GetResults 方法來取得 3 個儲存格公式,並使用相同物件的 SetResults 方法來設定公式。 輸入陣列對於每一個儲存格會有 4 個空位,Master 物件也是同樣的情形。 如果是 Shape 或 Style 物件,則每一個儲存格只需要 3 個空位 (區段、資料列及儲存格)。
Public Sub Set Results_Example()
On Error GoTo HandleError
Dim aintSheetSectionRowColumn(1 To (3 * 4)) As Integer
aintSheetSectionRowColumn(1) = ActivePage.Shapes(1).ID
aintSheetSectionRowColumn(2) = visSectionObject
aintSheetSectionRowColumn(3) = visRowXFormOut
aintSheetSectionRowColumn(4) = visXFormWidth
aintSheetSectionRowColumn(5) = ActivePage.Shapes(2).ID
aintSheetSectionRowColumn(6) = visSectionObject
aintSheetSectionRowColumn(7) = visRowXFormOut
aintSheetSectionRowColumn(8) = visXFormHeight
aintSheetSectionRowColumn(9) = ActivePage.Shapes(3).ID
aintSheetSectionRowColumn(10) = visSectionObject
aintSheetSectionRowColumn(11) = visRowXFormOut
aintSheetSectionRowColumn(12) = visXFormAngle
'Get the first two values in inches. The second element in
'the units array is left uninitialized (empty) because we
'want the second result in the same units as the first
'result. The third result is initialized in degrees. Note that
'units can be expressed as a string or an integer constant.
Dim avarUnits(1 To 3) As Variant
avarUnits(1) = "in."
avarUnits(3) = visDegrees
'Return results of cells as an array of floating point numbers.
Dim avarResults() As Variant
ActivePage.GetResults aintSheetSectionRowColumn, visGetFloats, _
avarUnits, avarResults
'Use SetResults to:
' - Set the width of shape 1 to the height of shape 2.
' - Set the height of shape 2 to the width of shape 1.
'NOTE: avarResults() is indexed from 0 to 2.
Dim varTemp As variant
varTemp = avarResults(0)
avarResults(0) = avarResults(1)
avarResults(1) = varTemp
'Pass the same array back to SetResults that we
'just passed to GetResults, but leave the angle
'alone. By setting the sheet ID entry in the third
'slot of the aintSheetSectionRowColumn array to
'visInvalShapeID, we tell SetResults to ignore that slot.
aintSheetSectionRowColumn(9) = visInvalShapeID
'Set the results of the cells.
ActivePage.SetResults aintSheetSectionRowColumn, avarUnits, avarResults, 0
Exit Sub
HandleError:
MsgBox "Error"
Exit Sub
End Sub
支援和意見反應
有關於 Office VBA 或這份文件的問題或意見反應嗎? 如需取得支援服務並提供意見反應的相關指導,請參閱 Office VBA 支援與意見反應。