2015-12-02 114 views
0

我在Mac上爲MS powerpoint 2011製作了一個VBA宏。我真的沒有進入VBA,但以某種方式管理讓12個形狀出現在屏幕上,但我基本上是以此爲出發點。我的修改可能不是實現這一目標的最佳方式,但它可以完成這項工作。VBA刪除特定形狀

但是現在我想要創建另一個宏,它允許從宏指令執行後,從Dim Shp As ShapeDim Shp11 As Shape命名的形狀消失或刪除。我嘗試了很多東西,但總是有錯誤。這裏的一些幫助是高度讚賞..

我也無法知道如何我可以在MS PowerPoint 2011 for Mac中使用一個宏的附加。有人可能有想法嗎?

非常感謝!

澄清:

這個宏背後的想法是,以顯示應在PPT中使用的顏色值,我想這可能是不錯的開啓和關閉這些顏色 - 不管在哪個幻燈片中。

因此,再次:代碼看起來可怕,但我不是一個很大的程序員;)

Sub show_ci_colors() 

'PURPOSE:Create a Text Box Shape and Reformat it 
'SOURCE: www.TheSpreadsheetGuru.com 

Dim Sld As Slide 
Dim Shp As Shape 
Dim Shp1 As Shape 
Dim Shp2 As Shape 
Dim Shp3 As Shape 
Dim Shp4 As Shape 
Dim Shp5 As Shape 
Dim Shp6 As Shape 
Dim Shp7 As Shape 
Dim Shp8 As Shape 
Dim Shp9 As Shape 
Dim Shp10 As Shape 
Dim Shp11 As Shape 

'ERROR HANDLING 
If ActivePresentation.Slides.Count = 0 Then 
    MsgBox "You do not have any slides in your PowerPoint project." 
    Exit Sub 
End If 

Set Sld = Application.ActiveWindow.View.Slide 

'Create shape with Specified Dimensions and Slide Position 
    Set Shp = Sld.Shapes.AddShape(Type:=msoShapeRectangle, _ 
    Left:=-80, Top:=20, Width:=60, Height:=40) 

    Set Shp1 = Sld.Shapes.AddShape(Type:=msoShapeRectangle, _ 
    Left:=-80, Top:=62, Width:=60, Height:=40) 

    Set Shp2 = Sld.Shapes.AddShape(Type:=msoShapeRectangle, _ 
    Left:=-80, Top:=104, Width:=60, Height:=40) 

    Set Shp3 = Sld.Shapes.AddShape(Type:=msoShapeRectangle, _ 
    Left:=-80, Top:=146, Width:=60, Height:=40) 

    Set Shp4 = Sld.Shapes.AddShape(Type:=msoShapeRectangle, _ 
    Left:=-80, Top:=186, Width:=60, Height:=40) 

    Set Shp5 = Sld.Shapes.AddShape(Type:=msoShapeRectangle, _ 
    Left:=-80, Top:=230, Width:=60, Height:=40) 

    Set Shp6 = Sld.Shapes.AddShape(Type:=msoShapeRectangle, _ 
    Left:=-80, Top:=272, Width:=60, Height:=40) 

    Set Shp7 = Sld.Shapes.AddShape(Type:=msoShapeRectangle, _ 
    Left:=-80, Top:=314, Width:=60, Height:=40) 

    Set Shp8 = Sld.Shapes.AddShape(Type:=msoShapeRectangle, _ 
    Left:=-80, Top:=356, Width:=60, Height:=40) 

    Set Shp9 = Sld.Shapes.AddShape(Type:=msoShapeRectangle, _ 
    Left:=-80, Top:=398, Width:=60, Height:=40) 

    Set Shp10 = Sld.Shapes.AddShape(Type:=msoShapeRectangle, _ 
    Left:=-80, Top:=440, Width:=60, Height:=40) 

    Set Shp11 = Sld.Shapes.AddShape(Type:=msoShapeRectangle, _ 
    Left:=-80, Top:=482, Width:=60, Height:=40) 



'FORMAT SHAPE 
'Shape Name 
    Shp.Name = "My Header" 

'No Shape Border 
    Shp.Line.Visible = msoFalse 
    Shp.Shadow.Visible = msoFalse 
    Shp1.Line.Visible = msoFalse 
    Shp1.Shadow.Visible = msoFalse 
    Shp2.Line.Visible = msoFalse 
    Shp2.Shadow.Visible = msoFalse 
    Shp3.Line.Visible = msoFalse 
    Shp3.Shadow.Visible = msoFalse 
    Shp4.Line.Visible = msoFalse 
    Shp4.Shadow.Visible = msoFalse 
    Shp5.Line.Visible = msoFalse 
    Shp5.Shadow.Visible = msoFalse 
    Shp6.Line.Visible = msoFalse 
    Shp6.Shadow.Visible = msoFalse 
    Shp7.Line.Visible = msoFalse 
    Shp7.Shadow.Visible = msoFalse 
    Shp8.Line.Visible = msoFalse 
    Shp8.Shadow.Visible = msoFalse 
    Shp9.Line.Visible = msoFalse 
    Shp9.Shadow.Visible = msoFalse 
    Shp10.Line.Visible = msoFalse 
    Shp10.Shadow.Visible = msoFalse 
    Shp11.Line.Visible = msoFalse 
    Shp11.Shadow.Visible = msoFalse 

'Shape Fill Color 
    Shp.Fill.ForeColor.RGB = RGB(4, 110, 151)     'BLUE 700 
    Shp.Fill.BackColor.RGB = RGB(4, 110, 151)     'BLUE 700 

    Shp1.Fill.ForeColor.RGB = RGB(6, 166, 227)     'BLUE 300 
    Shp1.Fill.BackColor.RGB = RGB(6, 166, 227)     'BLUE 300 

    Shp2.Fill.ForeColor.RGB = RGB(133, 199, 226)    'BLUE 100 
    Shp2.Fill.BackColor.RGB = RGB(133, 199, 226)    'BLUE 100 

    Shp3.Fill.ForeColor.RGB = RGB(23, 152, 131)     'GREEN 
    Shp3.Fill.BackColor.RGB = RGB(23, 152, 131)     'GREEN 

    Shp4.Fill.ForeColor.RGB = RGB(254, 201, 5)      'YELLOW 
    Shp4.Fill.BackColor.RGB = RGB(254, 201, 5)      'YELLOW 

    Shp5.Fill.ForeColor.RGB = RGB(189, 57, 47) 'RED 700 
    Shp5.Fill.BackColor.RGB = RGB(189, 57, 47) 'RED 700 

    Shp6.Fill.ForeColor.RGB = RGB(225, 92, 80) 'RED 300 
    Shp6.Fill.BackColor.RGB = RGB(225, 92, 80) 'RED 300 

    Shp7.Fill.ForeColor.RGB = RGB(237, 140, 52) 'ORANGE 
    Shp7.Fill.BackColor.RGB = RGB(237, 140, 52) 'ORANGE 

    Shp8.Fill.ForeColor.RGB = RGB(64, 64, 64) 'GREY 700 
    Shp8.Fill.BackColor.RGB = RGB(64, 64, 64) 'GREY 700 

    Shp9.Fill.ForeColor.RGB = RGB(84, 84, 84) 'GREY 600 
    Shp9.Fill.BackColor.RGB = RGB(84, 84, 84) 'GREY 600 

    Shp10.Fill.ForeColor.RGB = RGB(189, 189, 198) 'GREY 300 
    Shp10.Fill.BackColor.RGB = RGB(189, 189, 198) 'GREY 300 

    Shp10.Fill.ForeColor.RGB = RGB(189, 189, 198) 'GREY 300 
    Shp10.Fill.BackColor.RGB = RGB(189, 189, 198) 'GREY 300 

    Shp11.Fill.ForeColor.RGB = RGB(238, 238, 238) 'GREY 200 
    Shp11.Fill.BackColor.RGB = RGB(238, 238, 238) 'GREY 200 


'Shape Text Color 
    Shp.TextFrame.TextRange.Font.Color.RGB = RGB(255, 255, 255) 
    Shp11.TextFrame.TextRange.Font.Color.RGB = RGB(64, 65, 65) 

'Text inside Shape 
    Shp.TextFrame.TextRange.Characters.Text = "Blue 700" & Chr(10) & "4/110/151" 
    Shp1.TextFrame.TextRange.Characters.Text = "Blue 300" & Chr(10) & "6/166/227" 
    Shp2.TextFrame.TextRange.Characters.Text = "Blue 100 " & Chr(10) & "133/199/226" 
    Shp3.TextFrame.TextRange.Characters.Text = "Green" & Chr(10) & "23/152 /131" 
    Shp4.TextFrame.TextRange.Characters.Text = "Yellow" & Chr(10) & "254/201/5" 
    Shp5.TextFrame.TextRange.Characters.Text = "Red 700" & Chr(10) & "189/57/47" 
    Shp6.TextFrame.TextRange.Characters.Text = "Red 300" & Chr(10) & "225/92/80" 
    Shp7.TextFrame.TextRange.Characters.Text = "Orange" & Chr(10) & "237/140 52" 
    Shp8.TextFrame.TextRange.Characters.Text = "Grey 700" & Chr(10) & "64/65/65" 
    Shp9.TextFrame.TextRange.Characters.Text = "Grey 600" & Chr(10) & "84/84/84" 
    Shp10.TextFrame.TextRange.Characters.Text = "Grey 300" & Chr(10) & "189/189/189" 
    Shp11.TextFrame.TextRange.Characters.Text = "Grey 200" & Chr(10) & "238/238/238" 


'Center Align Text 
    Shp.TextFrame.TextRange.Paragraphs.ParagraphFormat.Alignment = msoAlignCenter 
    Shp1.TextFrame.TextRange.Paragraphs.ParagraphFormat.Alignment = msoAlignCenter 
    Shp2.TextFrame.TextRange.Paragraphs.ParagraphFormat.Alignment = msoAlignCenter 
    Shp3.TextFrame.TextRange.Paragraphs.ParagraphFormat.Alignment = msoAlignCenter 
    Shp4.TextFrame.TextRange.Paragraphs.ParagraphFormat.Alignment = msoAlignCenter 
    Shp5.TextFrame.TextRange.Paragraphs.ParagraphFormat.Alignment = msoAlignCenter 
    Shp6.TextFrame.TextRange.Paragraphs.ParagraphFormat.Alignment = msoAlignCenter 
    Shp7.TextFrame.TextRange.Paragraphs.ParagraphFormat.Alignment = msoAlignCenter 
    Shp8.TextFrame.TextRange.Paragraphs.ParagraphFormat.Alignment = msoAlignCenter 
    Shp9.TextFrame.TextRange.Paragraphs.ParagraphFormat.Alignment = msoAlignCenter 
    Shp10.TextFrame.TextRange.Paragraphs.ParagraphFormat.Alignment = msoAlignCenter 
    Shp11.TextFrame.TextRange.Paragraphs.ParagraphFormat.Alignment = msoAlignCenter 

'Vertically Align Text to Middle 
    'Shp.TextFrame.VerticalAnchor = msoAnchorMiddle 

'Adjust Font Size 
    Shp.TextFrame.TextRange.Font.Size = 8 
    Shp1.TextFrame.TextRange.Font.Size = 8 
    Shp2.TextFrame.TextRange.Font.Size = 8 
    Shp3.TextFrame.TextRange.Font.Size = 8 
    Shp4.TextFrame.TextRange.Font.Size = 8 
    Shp5.TextFrame.TextRange.Font.Size = 8 
    Shp6.TextFrame.TextRange.Font.Size = 8 
    Shp7.TextFrame.TextRange.Font.Size = 8 
    Shp8.TextFrame.TextRange.Font.Size = 8 
    Shp9.TextFrame.TextRange.Font.Size = 8 
    Shp10.TextFrame.TextRange.Font.Size = 8 
    Shp11.TextFrame.TextRange.Font.Size = 8 


'FONT STYLE DELETED 
'Adjust Font Style 
' Shp.TextFrame.TextRange.Font.Name = "Verdana"L 

End Sub 

回答

0

要刪除的形狀集合中,你需要那麼這個例子說明如何刪除所有形狀倒數從有源演示文稿的幻燈片1:

Dim iCounter As Integer 
For iCounter = ActivePresentation.Slides(1).Shapes.Count to 1 Step -1 
    ActivePresentation.Slides(1).Shapes(iCounter).Delete 
Next 

可以當然加檢查,以決定哪些類型/形狀的名稱將被刪除。

接下來,如果你想使用宏的加載項在PowerPoint:MAC 2011年,將其保存爲一個文件.ppam,然後使用工具/加載項加載...你可能也想添加菜單能夠訪問您的加載項的功能,你就需要在PowerPoint中使用的CommandBars 是:Mac 2011中這將讓你開始:

Dim oMenuBar 
Dim oMenu As CommandBarPopup 
Dim oCtrl As CommandBarControl 
Dim iTools As Integer 

' Get the position of the native PowerPoint tools menu (English only) 
iTools = Application.CommandBars.ActiveMenuBar.Controls("Tools").Index 

' Create a new menu 
Set oMenuBar = Application.CommandBars.ActiveMenuBar 
Set oMenu = oMenuBar.Controls.Add(type:=msoControlPopup, Before:=iTools, Temporary:=True) 
With oMenu 
    .Caption = "My Menu" 
    .Enabled = True 
End With 

' Add a control 
Set oCtrl = oMenu.Controls.Add(type:=msoControlButton) 
With oCtrl 
    .Caption = "My Tool" 
    .OnAction = "MyMacro" 
End With 

如果這幫助,請投票解決方案:-)

+0

謝謝你。它的作品,但只有當我在第一張幻燈片加上,它也刪除一個文本框。 難道不可能做某事嗎?像Shp1。刪除並重復所有其他形狀呢? :) – Kinda

+0

如果您設置對Shp1的引用,則Shp1.Delete將起作用。雖然沒有看到你使用的代碼,但很難說如何修復它。您可以在創建形狀時爲其指定一個名稱,然後再幻燈片(x).Shapes(「YourShapeName」)。刪除(其中x是您要使用的幻燈片的索引) –

+0

謝謝。那麼,代碼看起來很可怕。我不是一個真正的程序員:)這個宏背後的想法是,要顯示應該在ppt中使用的顏色值,我認爲可以很好地切換這些顏色 - 無論您在哪個幻燈片中。我將發佈我上面的代碼.. – Kinda