ms word - Powershell msword table format cell text -


using powershell able add text in table cell this, want format cell text.

table.cell(1, 1).range.text = "listvitem1vitem2"

eg. want bold , align center "list" string. apply ordered item style item1 , iteam2 string. how can using powershell?

edit: found code in c#, not able make work in powershell.

https://social.msdn.microsoft.com/forums/vstudio/en-us/4d7fa718-29d7-4b71-848e-11d7aafac5b7/multiple-format-into-one-cell-of-table-in-word-2007-using-c?forum=worddev

 //boldrange_1 "green", boldrange_2 "inch".             word.range boldrange_1 = table.cell(1, 2).range;//assign whole cell range boldrange, adjust setrange method.             boldrange_1.setrange(table.cell(1, 2).range.start, table.cell(1, 2).range.words[1].end);             boldrange_1.bold= 1;              word.range boldrange_2 = table.cell(2, 2).range;             boldrange_2.setrange(table.cell(2, 2).range.words[4].end, table.cell(2, 2).range.words[5].end);             boldrange_2.font.bold = 1;             //i've found boldrange_2.font.bold = 1; , boldrange_2.bold = 1; have same effect. 

setting font bold or normal not hard, , yes requires use of set-range. changing alignment or setting words list bit trickier have set elements in separate paragraphs , typical method (using typeparagraph typetext) not work in table cell. method works me, though paragraph settings , table setting need altered based on specific needs.

#this creating simple table testing $w = new-object -comobject "word.application" $w.visible = $true $doc = $w.documents.add() $r = $doc.range() $table = $doc.tables.add($r, 5, 5) $list = $table.cell(1,1).range #so sel doesn't have rewritten hyperlinks $sel = $table.cell(1,1).range  #this because hyperlinks break indexof $hyper = $table.cell(1,1).range #doing assign way emphasis easier  #define full text spliting ranges , setup paragraphs $sel.insertparagraph() $w.selection.typetext("hyperlink`n`rlist`r`nitem1`r`nitem2")  #set hyperlink (use -2 keep `n`r paragraph) $hyper.setrange(0, $sel.text.indexof("list") - 2) $list.setrange($sel.text.indexof("list"), $sel.text.length) $sel.setrange($sel.text.indexof("item"), $sel.text.length)  $doc.hyperlinks.add($hyper, 'www.google.com', $null, $null, "link google")  #set list range of cell bold , alignment center $list.bold = $true $list.paragraphformat.alignment = 'wdalignparagraphcenter'  #now using sel range: add paragraph break (so list applies this) #then set alignment , apply number list $sel.insertparagraphbefore() $sel.paragraphformat.alignment = 'wdalignparagraphright' $sel.listformat.applynumberdefault() 

update: cleaned code , added hyperlink building method


Comments

Popular posts from this blog

google chrome - Developer tools - How to inspect the elements which are added momentarily (by JQuery)? -

angularjs - Showing an empty as first option in select tag -

php - Cloud9 cloud IDE and CakePHP -