2010-05-18 83 views
3

這是一個Excel模板的VBA代碼,我試圖在我正在處理的VSTO項目中將其轉換爲C#。順便說一句,這是一個VSTO加載項:什麼是此Excel Excel VBA代碼的形狀的C#等效?

Dim addedShapes() As Variant 
ReDim addedShapes(1) 
addedShapes(1) = aBracket.Name 

ReDim Preserve addedShapes(UBound(addedShapes) + 1) 
addedShapes(UBound(addedShapes)) = "unique2" 

Set tmpShape = Me.Shapes.Range(addedShapes).Group 

在這一點上,我被難倒addedShapes(),不知道這是怎麼一回事。

更新: Matti提到addedShapes()代表VBA中的變體數組。所以現在我想知道addedShapes()的內容應該是什麼。這是否是在C#中調用Shapes.Range()調用的正確方法?

List<string> addedShapes = new List<string>(); 
... 
Shape tmpShape = worksheet.Shapes.get_Range 
    (addedShapes.Cast<object>().ToArray()).Group(); 

我會很感激的人誰與VBA和C#願意合作,就我的問題&問題評論!

+0

是最後一位爲我工作很好...感謝分享 – gotmike 2016-12-13 23:38:08

回答

2

我不確定你的實際問題應該是什麼,但addedShapes是一個數組。在VB及其變體中,使用()而不是[]來聲明和訪問數組。

而且,你的代碼看起來它只是做一個真正的長篇大論方式:

object[] addedShapes = new object[] { aBracket.Name, "unique2" }; 
Shape tmpShape = worksheet.Shapes.get_Range(addedShapes).Group(); 

最後一部分可能替代地

Shape tmpShape = worksheet.Shapes[addedShapes].Group(); 

看到哪些曾經工作。我無法弄清楚MSDN建議哪一個。

+0

這肯定是有幫助的信息。我要用這些信息更新我的帖子......也許有人會進一步啓發我。 – code4life 2010-05-18 20:35:33

+0

+1幫助我在路上... – code4life 2010-05-18 20:42:20

2

請原諒c風格的評論,vb風格不會很好的語法hilight。

//This declares an array of variants but does not initialize it. 
Dim addedshapes() As Variant 

//Initializes the array with a max index of 1. (insert vb index rant here) 
ReDim addedShapes(1) 

//assigns the contents of aBracket.Name to element 1 of the array. 
addedShapes(1) = aBracket.Name 

//increases the size of addedShapes by 1, retaining any values. 
ReDim Preserve addedShapes(UBound(addedShapes) + 1) 

//sets the last element to the string literal 
addedShapes(UBOund(addedShapes)) = "unique2" 

//Not sure here because I havent done any VBA in a loooong time, 
//but anyway it's passing the array. 
set tmpShape = Me.Shapes.Range(addedShapes).Group 
在VB

Variant僅僅是一個懶惰的結構,可以容納任何數據類型,整型,浮點型,對象等,所以在.NET中的最直接的比較是某些對象collection /陣列。但是,如果你知道那裏發生了什麼,那麼將收藏集限制在那裏會好得多。因此,而不是List<object>你會使用List<Class>List<BaseClass>List<ISomeInterface>