2016-11-12 23 views
0

我正在爲自定義功能區執行添加操作,並且在編寫要由功能區按鈕(回調方法)執行的代碼時,引用屬於Pwp的代碼時出現錯誤API(我在項目的引用中有API,儘管它的「Copy Local」屬性是False,我不能使它成爲True(不知道這是否是一個問題))。如何使用Pwp對象? (無法獲取形狀的寬度和高度)

因此,我不能做任何事......

代碼:

public void SwapPositions(Office.IRibbonControl control, bool isPressed) 
     { 

      _Application myPPT = Globals.ThisAddIn.Application; 
      Slide curSlide = myPPT.ActiveWindow.View.Slide; 

      curSlide.Shapes.Range(0).Height; 

     } 

我這種情況下,錯誤是在最後陳述。 VS說:「只有賦值,調用,增量,減量和新的對象表達式可以用作語句」。

正如您所看到的,代碼剛剛開始,因爲我無法找到解決方案。 最後,我想在C#中複製,在VBA切換形狀位置的代碼我已經有了:

Sub swap_positions() 

Static t1, l1, h1, w1 As Double 
Dim t2, l2, h2, w2 As Double 

t1 = ActiveWindow.Selection.ShapeRange(1).Top 
l1 = ActiveWindow.Selection.ShapeRange(1).Left 
h1 = ActiveWindow.Selection.ShapeRange(1).Height 
w1 = ActiveWindow.Selection.ShapeRange(1).Width 

t2 = ActiveWindow.Selection.ShapeRange(2).Top 
l2 = ActiveWindow.Selection.ShapeRange(2).Left 
h2 = ActiveWindow.Selection.ShapeRange(2).Height 
w2 = ActiveWindow.Selection.ShapeRange(2).Width 

'1 Vertical alignment 
ActiveWindow.Selection.ShapeRange(1).Top = ActiveWindow.Selection.ShapeRange(2).Top + _ 
(ActiveWindow.Selection.ShapeRange(2).Height - ActiveWindow.Selection.ShapeRange(1).Height)/2 

'1 Horizontal alignment 
ActiveWindow.Selection.ShapeRange(1).Left = ActiveWindow.Selection.ShapeRange(2).Left + _ 
(ActiveWindow.Selection.ShapeRange(2).Width - ActiveWindow.Selection.ShapeRange(1).Width)/2 

'2 Vertical alignment 
ActiveWindow.Selection.ShapeRange(2).Top = t1 + _ 
(h1 - h2)/2 

'2 Horizontal alignment 
ActiveWindow.Selection.ShapeRange(2).Left = l1 + _ 
(w1 - w2)/2 

End Sub 

問:與形狀在C#在這種情況下,我怎麼能工作????

回答

0

解決:

public void SwapPositions(Office.IRibbonControl control) 
    { 
     //Defining shape variables: Height, Width, Top and Left 

     float[] heigthArray; 
     heigthArray = new float[30]; 
     float[] widthArray; 
     widthArray = new float[30]; 
     float[] topArray; 
     topArray = new float[30]; 
     float[] leftArray; 
     leftArray = new float[30]; 

     //Finding Height, Width, Top and Left of shapes 
     int i = 1; 
     foreach (Shape sh in Globals.ThisAddIn.Application.ActiveWindow.Selection.ShapeRange) 
     { 
      heigthArray[i] = sh.Height; 
      widthArray[i] = sh.Width; 
      topArray[i] = sh.Top; 
      leftArray[i] = sh.Left; 

      i = i + 1; 
     } 

     //Vetical alignment 

     int x = 1; 
     float shapeTop = 1; 
     float shapeHeight = 1; 
     foreach (Shape sh in Globals.ThisAddIn.Application.ActiveWindow.Selection.ShapeRange) 
     { 

      if (x == 1) 
      { 
       shapeTop = sh.Top; 
       shapeHeight = sh.Height; 

       sh.Top = topArray[x + 1] + (heigthArray[x + 1] - heigthArray[x])/2; 
      } 
      else if (x == i - 1) 
      { 
       sh.Top = shapeTop + (shapeHeight - heigthArray[x])/2; 

      } 
      else 
      { 
       sh.Top = topArray[x + 1] + (heigthArray[x + 1] - heigthArray[x])/2; 
      } 

      x = x + 1; 

     } 

     //Horizontal alignment 
     x = 1; 
     float shapeLeft = 1; 
     float shapeWidth = 1; 
     foreach (Shape sh in Globals.ThisAddIn.Application.ActiveWindow.Selection.ShapeRange) 
     { 
      if (x == 1) 
      { 
       shapeLeft = sh.Left; 
       shapeWidth = sh.Width; 

       sh.Left = leftArray[x + 1] + (widthArray[x + 1] - widthArray[x])/2; 
      } 
      else if (x == i - 1) 
      { 
       sh.Left = shapeLeft + (shapeWidth - widthArray[x])/2; 

      } 
      else 
      { 
       sh.Left = leftArray[x + 1] + (widthArray[x + 1] - widthArray[x])/2; 
      } 


      x = x + 1; 
     } 

    }