3

I've seen in VBA examples, that partially formatting a Visio shape-text makes use of the characters.begin and characters.end properties to select the part of the text to format.

Now I'm having issues in adapting this to my Powershell script - the begin property seems to be read only:

[DBG]: PS C:\Windows\system32>>> $visioObjects.($_.key).getType()
IsPublic IsSerial Name         BaseType
-------- -------- ----         --------                  
True     False    ShapeClass                               System.__ComObject
[DBG]: PS C:\Windows\system32>>> $visioObjects.($_.key).Text
Hello World
[DBG]: PS C:\Windows\system32>>> $visioObjects.($_.key).Characters.Begin
0
[DBG]: PS C:\Windows\system32>>> $visioObjects.($_.key).Characters.End
11
[DBG]: PS C:\Windows\system32>>> $visioObjects.($_.key).Characters.CharCount
11
[DBG]: PS C:\Windows\system32>>> $visioObjects.($_.key).Characters.End = 5
[DBG]: PS C:\Windows\system32>>> $visioObjects.($_.key).Characters.End 
11

Here is the code in short for reproduction:

$appVisio = New-Object -ComObject Visio.Application
$docsObj = $appVisio.Documents
$docObj = $docsObj.Add("")

# Set the active page of the document to page 1
$pagsObj = $appVisio.ActiveDocument.Pages
$pagObj = $pagsObj.Item(1)
$shpObj = $pagObj.DrawRectangle(0, 0, 1, 1)
$shpObj.text = 'Hello World'
$shpObj.Characters.Begin = 5 # <======================== Won't accept
$shpObj.Characters.Begin # <======================== Returns 0, makes me cry

Can anyone explain why this is not working?

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Joel
  • 474
  • 1
  • 5
  • 15
  • Here's [the property in question](http://msdn.microsoft.com/en-us/library/office/ff767252.aspx) for anyone that wants to take a shake at this. – Anthony Neace Jan 09 '14 at 15:10

1 Answers1

2

The problem is actually one level up the chain from where you're working. Begin is a read-write property, but Shape.Characters is read-only (MSDN link).

While you can't modify $shpObj.Characters.Begin directly, you can do something like this:

$chars = $shpObj.Characters
$chars.Begin = 5

And then proceed to use $chars the way you were going to use $shpObj.Characters.

ajk
  • 4,473
  • 2
  • 19
  • 24