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 = "listvitem1
vitem2"
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.
//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
Post a Comment