Visio) (Style.SetFormulas 方法

會設定一個或多個儲存格的公式。

語法

expression。 SetFormulas( _SRCStream()_ , _formulaArray()_ , _Flags_ )

運算式 代表 Style 物件的變數。

參數

名稱 必要/選用 資料類型 描述
SID_SRCStream () 必要 整數 識別要修改之儲存格的資料流。
formulaArray () 必要 Variant 要指派給識別的儲存格之公式。
Flags 必要 整數 會影響 SetFormulas 行為的旗標。

傳回值

整數

註解

SetFormulas 方法的行為就像 Formula 屬性,但是有一點例外,就是可以使用這個方法一次設定很多儲存格的公式,而不是一次設定一個儲存格的公式。

針對 樣式 物件,您可以使用 SetFormulas 方法來設定任意一組儲存格的結果。 您可以透過在 SID_SRCStream () 中傳遞整數陣列,告訴 SetFormulas 方法要設定哪些儲存格。 SID_SRCStream () 是 2 位元組整數的一維陣列。

對於 Style 物件,SID_SRCStream () 應該是 n>= 1 的 3 n 個 2 位元組整數的一維陣列。 SetFormulas 方法會將資料流程解譯為:

{sectionIdx, rowIdx, cellIdx }n

其中 sectionIdx 是所要儲存格的區段索引,rowIdx 是它的資料列索引,而 cellIdx 則是它的儲存格索引。

formulaArray () 參數應該是包含 1 <= m 個變體的一維陣列。 每個 變體 都應該是字 串、 字串的參照或空白。 如果 formulaArray (i) 是空的,則第 i 個儲存格會設定為 formulaArray (j) 中的公式,其中 j 是最近一個項目的索引,不是空白。 如果沒有非空的先前項目,則不會變更對應的儲存格。 如果指定的公式少於儲存格 ( m<n ) ,則第 i 個儲存格 i>m 會設定為所選擇設定第 m 個儲存格的相同公式。 因此,要將多個單元格設置為同一公式,您只需傳遞一個公式副本。

Flags 引數應是下列值的位元遮罩。

常數 值 描述
visSetBlastGuards &H2 覆寫目前的儲存格值 (即使這些值有受到保護)。
visSetTestCircular &H4 測試儲存格循環參考的建立。
visSetUniversalSyntax &H8 公式在通用語法中。

SetFormulas 方法傳回的值是SID_SRCStream () 中成功處理的項目數。 如果 i<n 個項目正確處理,但 i + 第一個項目發生錯誤, SetFormulas 方法就會引發例外狀況並傳回 i。 否則會傳回 n 。

範例

下列巨集示範如何使用 SetFormulas 方法。 它假設有一個使用中的 Microsoft Office Visio 頁面,該頁面上至少有三個圖形。 它使用 GetFormulas 方法來取得圖形 1 的寬度、圖形 2 的高度,以及圖形 3 的角度。 接著,它會使用 SetFormulas 將圖形 1 的寬度設定為圖形 2 的高度,並將圖形 2 的高度設定為圖形 1 的寬度。 圖形 3 的角度保持不變。

這個範例會使用 Page 物件的 GetFormulas 方法來取得三個儲存格公式,並使用相同物件的 SetFormulas 方法來設定公式。 輸入陣列對於每一個儲存格會有四個空位,Master 物件也是同樣的情形。 如果是 Shape 或 Style 物件,則每一個儲存格只需要三個空位 (區段、資料列及儲存格)。

 
Public Sub SetFormulas_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 
 
 'Return the formulas of the cells. 
 Dim avarFormulaArray() As Variant 
 ActivePage.GetFormulas aintSheetSectionRowColumn, avarFormulaArray 
 
 'Use SetFormulas to: 
 ' - Set the width of shape 1 to height of shape 2. 
 ' - Set height of shape 2 to width of shape 1. 
 ' Note: avarFormulaArray() is indexed from 0 to 2. 
 Dim varTemp As variant 
 varTemp = avarFormulaArray(0) 
 avarFormulaArray(0) = avarFormulaArray(1) 
 avarFormulaArray(1) = varTemp 
 
 'Pass the same array back to SetFormulas that we 
 'just passed to GetFormulas, leaving angle alone. By setting 
 'the sheet ID entry in the third slot of the 
 'aintSheetSectionRowColumn array to visInvalShapeID, 
 'we tell SetFormulas to ignore that slot. 
 aintSheetSectionRowColumn (9) = visInvalShapeID 
 
 'Tell Microsoft Visio to set the formulas of the cells. 
 ActivePage.SetFormulas aintSheetSectionRowColumn, avarFormulaArray, 0 
 
 Exit Sub 
 
HandleError: 
 
 MsgBox "Error" 
 
 Exit Sub 
 
End Sub

支援和意見反應

有關於 Office VBA 或這份文件的問題或意見反應嗎? 如需取得支援服務並提供意見反應的相關指導,請參閱 Office VBA 支援與意見反應。