<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Brian D. Baker</title>
    <description>The latest articles on DEV Community by Brian D. Baker (@bakerling).</description>
    <link>https://dev.to/bakerling</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F618705%2F967eed1d-ddcf-46fa-8d0a-fc23f859afa4.jpg</url>
      <title>DEV Community: Brian D. Baker</title>
      <link>https://dev.to/bakerling</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/bakerling"/>
    <language>en</language>
    <item>
      <title>Weirdness with TextRange2 objects inside PowerPoint Tables</title>
      <dc:creator>Brian D. Baker</dc:creator>
      <pubDate>Thu, 22 Apr 2021 13:46:21 +0000</pubDate>
      <link>https://dev.to/bakerling/weirdness-with-textrange2-objects-inside-powerpoint-tables-on9</link>
      <guid>https://dev.to/bakerling/weirdness-with-textrange2-objects-inside-powerpoint-tables-on9</guid>
      <description>&lt;p&gt;I was working with &lt;code&gt;TextRange2.BoundLeft&lt;/code&gt; and &lt;code&gt;TextRange2.BoundTop&lt;/code&gt; when I noticed the behavior of those 2 properties is different depending on whether the &lt;code&gt;TextRange2&lt;/code&gt; is in a normal &lt;code&gt;Shape&lt;/code&gt; object or inside a table. For TR2s inside normal &lt;code&gt;Shape&lt;/code&gt; objects, &lt;code&gt;.BoundTop&lt;/code&gt; and &lt;code&gt;.BoundLeft&lt;/code&gt; give the distance from the top and left sides of the slide, but for TR2s inside tables, they give the distance from the top left corner of the table.&lt;/p&gt;

&lt;p&gt;That should be easy to fix, right? All you have to do is get the &lt;code&gt;Shape&lt;/code&gt; object for the table containing the &lt;code&gt;TextRange2&lt;/code&gt; in question, then add its &lt;code&gt;.Top&lt;/code&gt; and &lt;code&gt;.Left&lt;/code&gt; to the &lt;code&gt;TextRange2&lt;/code&gt;'s &lt;code&gt;.BoundTop&lt;/code&gt; and &lt;code&gt;.BoundLeft&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;But there's another challenge here. Getting the table &lt;code&gt;Shape&lt;/code&gt; object containing the &lt;code&gt;TextRange2&lt;/code&gt; you already have.&lt;/p&gt;

&lt;p&gt;Normally, PowerPoint &lt;code&gt;TextRange2&lt;/code&gt; objects have a property &lt;code&gt;.Parent&lt;/code&gt; that you can use to go up the presentation DOM to the &lt;code&gt;TextFrame2&lt;/code&gt; that contains them, and likewise &lt;code&gt;TextFrame2&lt;/code&gt; objects have a property &lt;code&gt;Parent&lt;/code&gt; that lets you access the shape that contains them ... except if the &lt;code&gt;TextRange2&lt;/code&gt; object is inside of a table. Then it has no &lt;code&gt;Parent&lt;/code&gt; property. Even trying to test if &lt;code&gt;.Parent is Nothing&lt;/code&gt; triggers an error.&lt;/p&gt;

&lt;p&gt;The only workaround I know of is to select the &lt;code&gt;TextRange2&lt;/code&gt; object in question (using the &lt;code&gt;.Select&lt;/code&gt; method), then get the &lt;code&gt;Shape&lt;/code&gt; that contains the &lt;code&gt;TextRange2&lt;/code&gt; from &lt;code&gt;ActiveWindow.Selection.ShapeRange(1)&lt;/code&gt;. Unfortunately, that only gets you halfway there, to the cell &lt;code&gt;Shape&lt;/code&gt; object containing the &lt;code&gt;TextRange2&lt;/code&gt;. Also, while the cell Shape object's &lt;code&gt;.Parent&lt;/code&gt; property works, it behaves counterintuitively; it returns the &lt;code&gt;Shape&lt;/code&gt; object that contains the entire table.&lt;/p&gt;

&lt;p&gt;To get to the &lt;code&gt;Shape&lt;/code&gt; object for the table, you have to use the &lt;code&gt;.Select&lt;/code&gt; trick again, this time calling it on the &lt;code&gt;Shape&lt;/code&gt; object for the cell. Then &lt;code&gt;ActiveWindow.Selection.ShapeRange(1)&lt;/code&gt; gets you to the &lt;code&gt;Shape&lt;/code&gt; object for the table.&lt;/p&gt;

&lt;p&gt;Code looks like something like the following to get the table. In my application I have this split across a couple of functions with error handlers and a selection buffer to restore whatever was originally selected before the function calls.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Public Function GetTableShapeAncestorOfTR2(ByRef tr2 As TextRange2) As Shape
    Dim cellShape as Shape
    tr2.Select
    Set cellShape = ActiveWindow.Selection.ShapeRange.Item(1)
    cellShape.Select
    Set GetTableShapeAncestorOfTR2 = ActiveWindow.Selection.ShapeRange.Item(1)
End Function
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>vba</category>
      <category>powerpoint</category>
    </item>
  </channel>
</rss>
