Jak vytvářet a vkládat tvary v PowerPointu s VBA?

Před pár dny jsem chtěl zautomatizovat poměrně všední úkol vytváření několika snímků v PowerPointu. Při automatizaci PowerPointu obvykle pracujete s kolekcí Snímky a obrazce. Slides jsou spíše samozřejmé a tvary jsou v podstatě všechno ostatní, včetně textových polí, tlačítek akcí, mediálních objektů, štítků, obrázků, popisků, vývojových diagramů a tak dále a tak dále. Ačkoli budu předvádět techniky s PowerPointem, některé syntaxe jsou ve skutečnosti docela použitelné pro Microsoft Word a Excel.

Abychom to zrekapitulovali, než začnete s vývojem maker VBA v PowerPointu, musíte mít povolenou kartu Vývoj .

Nejprve začněme automatickým vytvořením prezentace s několika snímky, jak je uvedeno v tomto tutoriálu: vytvoření prezentace ve VBA .

Nyní můžeme začít pracovat s Shapes. Za prvé, pojďme do toho a otevřete editor VBA stisknutím Alt+ F11. Pokud jste postupovali podle předchozího návodu, měli byste mít modul 1 v seznamu modulů (jak je uvedeno níže)

Práce s tvary ve VBA

Přidání textového pole pomocí VBA

Začneme přidáním svislého textového pole na náš první snímek v prezentaci. Přidejte následující kód do Module1, pak pokračujte a stiskněte tlačítko Uložit (Ctrl+s) a spusťte své makro (stiskněte F5 nebo stiskněte Spustit sub/uživatelský formulář).

Sub CreateTextBox()
Set MySlide = ActivePresentation.Slides(2)

    With MySlide.Shapes
        .AddTextbox(Orientation:=msoTextOrientationVertical, _
         Left:=90, Top:=200, Width:=80, _
         Height:=200).TextFrame.TextRange.Text _
        = ("This is my vertical text box")

End With

End Sub

Všimněte si, že pomocí VBA můžete poměrně snadno změnit velikost rozměrů textového pole. V tomto případě změníme velikost prvního tvaru na druhém snímku, můžete jej upravit podle potřeby.

Sub ResizeText()
Set MyShape = ActivePresentation.Slides(2).Shapes(1)

'Add your required dimensions as needed below
   With MyShape
         .Width = 200
         .Height = 35

End With
End Sub

Textové efekty s VBA

Předpokládejme nyní, že chceme ke všem snímkům prezentace přidat textové pole, tentokrát o něco vizuálně přitažlivější. K tomu použijeme VBA k vytvoření vlastních textových efektů, které jsme schopni vložit do jednoho nebo více snímků v prezentaci. Nejprve projdeme snímky prezentace a poté podle potřeby přidáme textový efekt.

Sub SetEffects()

Dim i As Integer

For i = 1 To ActivePresentation.Slides.Count
    ActivePresentation.Slides(i) _
    .Shapes.AddTextEffect msoTextEffect12, "Draft for Review", _
    "Segoe UI", 32, msoTrue, msoTrue, 650, 50
Next

End Sub

Tady je výsledek ;-):

Jak vytvářet a vkládat tvary v PowerPointu s VBA?

Všimněte si, že k dosažení tohoto konkrétního výsledku můžete také použít jednoduchý vodoznak.

Popisky aplikace PowerPoint s VBA

Naším dalším příkladem bude přidání popisků do vaší prezentace. V tomto příkladu přidáme popisek na druhý snímek.

Sub CreateCallout()

ActivePresentation.Slides(2).Shapes.AddCallout(Type:=msoCalloutTwo, Left:=200, Top:=50, _  Width:=300, Height:=100).TextFrame.TextRange.Text = "My Callout"

End Sub

Setting properties for a shape

Many formatting properties of shapes are not set by properties that apply directly to the Shape or ShapeRange object. Instead, related shape attributes are grouped under secondary objects, such as the FillFormat object, which contains all the properties that relate to the shape's fill, or the LinkFormat object, which contains all the properties that are unique to linked OLE objects. To set properties for a shape, you must first return the object that represents the set of related shape attributes and then set properties of that returned object. For example, you use the Fill property to return the FillFormat object, and then you set the ForeColor property of the FillFormat object to set the fill foreground color for the specified shape, as shown in the following example.

VB

Set myDocument = ActivePresentation.Slides(1)
myDocument.Shapes(1).Fill.ForeColor.RGB = RGB(255, 0, 0)

Applying a property or method to several shapes at the same time

In the user interface, there are some operations you can perform with several shapes selected; for example, you can select several shapes and set all their individual fills at once. There are other operations you can only perform with a single shape selected; for example, you can only edit the text in a shape if a single shape is selected.

In Visual Basic, there are two ways to apply properties and methods to a set of shapes. These two ways allow you to perform any operation that you can perform on a single shape on a range of shapes, whether or not you can perform the same operation in the user interface.

  • If the operation works on multiple selected shapes in the user interface, you can perform the same operation in Visual Basic by constructing a ShapeRange collection that contains the shapes you want to work with, and applying the appropriate properties and methods directly to the ShapeRange collection.

  • If the operation does not work on multiple selected shapes in the user interface, you can still perform the operation in Visual Basic by looping through the Shapes collection or through a ShapeRange collection that contains the shapes you want to work with, and applying the appropriate properties and methods to the individual Shape objects in the collection.

Many properties and methods that apply to the Shape object and ShapeRange collection fail if applied to certain kinds of shapes. For example, the TextFrame property fails if applied to a shape that cannot contain text. If you are not positive that each shape in a ShapeRange collection can have a certain property or method applied to it, don't apply the property or method to the ShapeRange collection. If you want to apply one of these properties or methods to a collection of shapes, you must loop through the collection and test each individual shape to make sure it is an appropriate type of shape before applying the property or method to it.

Applying a property or method to a ShapeRange collection

If you can perform an operation on multiple selected shapes in the user interface at the same time, you can do the programmatic equivalent by constructing a ShapeRange collection and then applying the appropriate properties or methods to it. The following example constructs a shape range that contains the AutoShapes named "Big Star" and "Little Star" on and applies a gradient fill to them and applies a gradient fill to them.myDocument

VB

Set myDocument = ActivePresentation.Slides(1) 
Set myRange = myDocument.Shapes _ 
    .Range(Array("Big Star", "Little Star")) 
myRange.Fill.PresetGradient msoGradientHorizontal, _ 
    1, msoGradientBrass

The following are general guidelines for how properties and methods behave when they are applied to a ShapeRange collection.

  • Applying a method to the collection is equivalent to applying the method to each individual Shape object in that collection.

  • Setting the value of a property of the collection is equivalent to setting the value of the property of each individual shape in that range.

  • A property of the collection that returns a constant returns the value of the property for an individual shape in the collection if all shapes in the collection have the same value for that property. If not all shapes in the collection have the same value for the property, it returns the "mixed" constant.

  • A property of the collection that returns a simple data type (such as LongSingle, or String) returns the value of the property for an individual shape if all shapes in the collection have the same value for that property.

  • The value of some properties can be returned or set only if there is exactly one shape in the collection. If there is more than one shape in the collection, a run-time error occurs. This is generally the case for returning or setting properties when the equivalent action in the user interface is possible only with a single shape (actions such as editing text in a shape or editing the points of a freeform).

The preceding guidelines also apply when you are setting properties of shapes that are grouped under secondary objects of the ShapeRange collection, such as the FillFormat object. If the secondary object represents operations that can be performed on multiple selected objects in the user interface, you'll be able to return the object from a ShapeRange collection and set its properties. For example, you can use the Fill property to return the FillFormat object that represents the fills of all the shapes in the ShapeRange collection. Setting the properties of this FillFormat object will set the same properties for all the individual shapes in the ShapeRange collection.

Looping through a Shapes or ShapeRange collection

Even if you cannot perform an operation on several shapes in the user interface at the same time by selecting them and then using a command, you can perform the equivalent action programmatically by looping through the Shapes collection or through a ShapeRange collection that contains the shapes you want to work with, and applying the appropriate properties and methods to the individual Shape objects in the collection. The following example loops through all the shapes on and adds text to each shape that is an AutoShape. and adds text to each shape that is an AutoShape.myDocument

VB

Set myDocument = ActivePresentation.Slides(1) 
For Each sh In myDocument.Shapes 
    If sh.Type = msoAutoShape Then 
        sh.TextFrame.TextRange.InsertAfter " (version 1)" 
    End If 
Next

The following example constructs ShapeRange collection that contains all the currently selected shapes in the active window and sets the text in each shape in the collection that can contain text.

VB

For Each sh in ActiveWindow.Selection.ShapeRange
    If sh.HasTextFrame Then
        sh.TextFrame.TextRange = "Initially selected"
    End If
Next

Aligning, distributing, and grouping shapes in a shape range

Use the Align and Distribute methods to position a set of shapes relative to one another or relative to the document that contains them. Use the Group method or the Regroup method to form a single grouped shape from a set of shapes.


[100 % vyriešené] Ako opraviť hlásenie „Chyba pri tlači“ v systéme Windows 10?

[100 % vyriešené] Ako opraviť hlásenie „Chyba pri tlači“ v systéme Windows 10?

V systéme Windows 10 sa zobrazuje správa o chybe tlače, potom postupujte podľa opráv uvedených v článku a uveďte tlačiareň do správnej cesty...

Jak zachytit a přehrát záznam Microsoft Teams

Jak zachytit a přehrát záznam Microsoft Teams

Schůzky můžete snadno znovu navštívit, pokud je zaznamenáte. Zde je návod, jak zachytit a přehrát záznam Microsoft Teams pro vaši příští schůzku.

Ako obnoviť predvolené aplikácie v systéme Android

Ako obnoviť predvolené aplikácie v systéme Android

Keď otvoríte súbor alebo kliknete na odkaz, vaše zariadenie so systémom Android vyberie predvolenú aplikáciu na jeho otvorenie. Pomocou tohto sprievodcu môžete obnoviť svoje predvolené aplikácie v systéme Android.

OPRAVENO: Chyba certifikátu Entitlement.diagnostics.office.com

OPRAVENO: Chyba certifikátu Entitlement.diagnostics.office.com

OPRAVENO: Chyba certifikátu Entitlement.diagnostics.office.com

Top 10 herních webů, které škola v roce 2022 neblokuje

Top 10 herních webů, které škola v roce 2022 neblokuje

Chcete-li zjistit nejlepší herní weby, které školy neblokují, přečtěte si článek a vyberte si nejlepší web s neblokovanými hrami pro školy, vysoké školy a práce

OPRAVENÉ: Tlačiareň v chybovom stave [HP, Canon, Epson, Zebra & Brother]

OPRAVENÉ: Tlačiareň v chybovom stave [HP, Canon, Epson, Zebra & Brother]

Ak čelíte problému s chybovým stavom tlačiarne na počítači so systémom Windows 10 a neviete, ako s ním zaobchádzať, opravte ho podľa týchto riešení.

Ako zálohovať Chromebook (2022)

Ako zálohovať Chromebook (2022)

Ak vás zaujíma, ako zálohovať Chromebook, máme pre vás riešenie. Viac o tom, čo sa zálohuje automaticky a čo nie, nájdete tu

Ako opraviť aplikáciu Xbox, ktorá sa neotvorí v systéme Windows 10 [RÝCHLE GUIDE]

Ako opraviť aplikáciu Xbox, ktorá sa neotvorí v systéme Windows 10 [RÝCHLE GUIDE]

Chcete opraviť aplikáciu Xbox, ktorá sa neotvorí v systéme Windows 10, potom postupujte podľa opráv, ako je povoliť aplikáciu Xbox zo služieb, resetovať aplikáciu Xbox, obnoviť balík aplikácií pre Xbox a ďalšie.

Co je LogiOptions.exe (UNICODE) a je proces bezpečný?

Co je LogiOptions.exe (UNICODE) a je proces bezpečný?

Pokud máte klávesnici a myš Logitech, uvidíte, že tento proces běží. Není to malware, ale není to nezbytný spustitelný soubor pro operační systém Windows.

Opravená chyba Java Update/Install Error 1603 ve Windows 10

Opravená chyba Java Update/Install Error 1603 ve Windows 10

Přečtěte si článek a zjistěte, jak opravit chybu Java Update 1603 ve Windows 10, vyzkoušejte opravy uvedené jednu po druhé a snadno opravte chybu 1603…