locked
Editing subsection of text in visio via powershell RRS feed

  • Question

  • I'm wondering if this i even possible

    Currently I have a script to create a visio drawing relevant portions of the text being used are:

    $application = New-Object -ComObject Visio.Application
    $application.visible = $false
    $documents = $application.Documents
    $document = $documents.Add(“Basic Diagram.vst”)
    $pages = $application.ActiveDocument.Pages
    $page = $pages.Item(1)    
    New-Variable -Name FolderTextBox$PNumber -Value $page.DrawRectangle($x-0.5, $y-0.25,$x+0.5,$y-0.7)
        (Get-Variable -Name FolderTextBox$PNumber).value.Text = ($accountslisted | Out-String).trim()
        (Get-Variable -Name FolderTextBox$PNumber).value.CellsU("LineColor").FormulaU = "RGB(255,255,255)"
        (Get-Variable -Name FolderTextBox$PNumber).value.CellsSRC(1,3,0).FormulaU = "RGB(255,255,255)"
        (Get-Variable -Name FolderTextBox$PNumber).value.Cells("Char.Color").FormulaU = "RGB(0, 0, 0)"
        (Get-Variable -Name FolderTextBox$PNumber).value.CellsU("Char.Size").FormulaU = "3 pt" 

    A typical line of text being inputted could look like:
    "R: Domain\ADGroup
    M: Domain\ADGroup"

    The "Char.Color" and "Char.Size" lines allow me to set colour and size of the font but what I'd really love to do is also modify the text to display like:
    "R: Domain\ADGroup
    M: Domain\ADGroup"

    note the added bolding and underlining of the start of the lines.  everything I've read seems to show you can only affect the lines of text as a whole and not seek out a few characters within the text to modify it.  does anyone know if there is a way to do this via powershell


    • Edited by YogiJ Thursday, November 14, 2019 12:26 AM
    Wednesday, November 13, 2019 3:51 AM

All replies

  • First fix your post as it is unreadable in most browsers. Use the code posting tool provided to edit and post the code corr4ectly.

    Second there is no need to use Get-Variable.  Just use the variable directly.

    Third - questions on how to use the Visio API should be asked in the Visio developers forum.


    \_(ツ)_/

    Wednesday, November 13, 2019 4:00 AM
  • Example:

    $var = New-Variable -PassThru -Name FolderTextBox$PNumber -Value $page.DrawRectangle($x-0.5, $y-0.25,$x+0.5,$y-0.7)
    $var.Text = $accountslisted
    $var.CellsU('LineColor').FormulaU = 'RGB(255,255,255)'
    $var.CellsSRC(1,3,0).FormulaU = 'RGB(255,255,255)'
    $var.Cells('Char.Color').FormulaU = 'RGB(0, 0, 0)'
    $var.value.CellsU('Char.Size').FormulaU = '3 pt'


    \_(ツ)_/


    • Edited by jrv Wednesday, November 13, 2019 4:08 AM
    Wednesday, November 13, 2019 4:07 AM
  • Wednesday, November 13, 2019 4:11 AM
  • The following is the correct way to set up and access a new document in Visio:

    $visio = New-Object -ComObject Visio.Application
    $doc = $visio.Documents.Add('Basic Diagram.vst')
    
    $shape = $doc.ActiveDocument.Pages[1].DrawRectangle($x-0.5, $y-0.25,$x+0.5,$y-0.7)
    $shape.Text = $accountslisted
    $shape.CellsU('LineColor').FormulaU = 'RGB(255,255,255)'
    $shape.CellsSRC(1,3,0).FormulaU = 'RGB(255,255,255)'
    $shape.Cells('Char.Color').FormulaU = 'RGB(0, 0, 0)'
    $shape.value.CellsU('Char.Size').FormulaU = '3 pt'


    \_(ツ)_/



    • Edited by jrv Wednesday, November 13, 2019 4:33 AM
    Wednesday, November 13, 2019 4:20 AM
  • I'll post it over there, the breakdown in the way it is being created allows me to do more editing to individual components further in the script.

    The use of the get-variable line is because I'm creating variables using a $Pnumber addition as its all automated and could potentially be inputting over a 100 items into the visio diagram this saves me from having to guess and copy paste for calling variable many times over.  the code above is not the whole code just the relevant parts of it for my technical question.

    Thanks I should have paid attention to the code input tools

    Thursday, November 14, 2019 12:33 AM
  • Your solution is due to the fact that you don't understand how to code and that those variables are unnecessary.

    All parts of a document are already variables.  It serves no purpose to not use them as designed.  You can always find them easily once you understand programming with objects and the Visio object model and API.

    If the Visio developers don't know PowerShell you will have to learn the correct way to convert C# to PowerShell which will require learning PowerShell.


    \_(ツ)_/

    Thursday, November 14, 2019 1:26 AM
  • Hi,

    Was your issue resolved?

    If you resolved it using our solution, please "mark it as answer" to help other community members find the helpful reply quickly.

    If you resolve it using your own solution, please share your experience and solution here. It will be very beneficial for other community members who have similar questions.

    If no, please reply and tell us the current situation in order to provide further help.

    Best Regards,

    Lee


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    Friday, December 6, 2019 9:06 AM