Author Posts

August 17, 2016 at 2:14 pm

Hello,

I want to add for each line of the text some chars in front of it.
Here is my code:

$InputFile = $content2+$content3+$content4
ForEach($Obj in $InputFile)
{
$everyLineChar = "* "
$combine = $end+$Obj
Write-Host $InputFile
}

I am a newbie when it comes to PowerShell. I am a self-study guy so.. 🙂

August 17, 2016 at 2:47 pm

$a = get-content c:\temp\somefile.txt
foreach($b in $a){$char = 'ok '; $c= "$char$b" ; $c}

$c has the contents of $char and $b that contains each line in the file you got for $a

August 17, 2016 at 2:57 pm

Thanks for your fast reply!

However, I've tried to incorporate this logic into my code but, it has been written just in the first line, but the others stayed intact.
Here is my full code, to get a bigger picture – I am pulling out some data from some cells, and I want to add into another document and to add asterix for each row.

$objWord = New-Object -ComObject Word.Application
$objWord.Visible = $true
$filename = 'D:\test.docx'
$objDocument = $objWord.Documents.Open($filename)

$LETable = $objDocument.Tables.Item(1)
$LETableCols = $LETable.Columns.Count
$LETableRows = $LETable.Rows.Count

#Write-output "Starting to write... "

#Write-output "Starting to write... "

#$asterix = "* "
#Write-Host $LETable.Cell(6,2).Range.Text|foreach {$_ + "*"}
$content2 = $LETable.Cell(6,2).Range.Text -replace '\r\a'," -creplace '(?m)^\s*\r?\n',"
$hl = "`n"
$content3 = $LETable.Cell(7,1).Range.Text -replace '\r\a'," -creplace '(?m)^\s*\r?\n',"
$content4 = $LETable.Cell(7,2).Range.Text -replace '\r\a'," -creplace '(?m)^\s*\r?\n',"
$doc2 = $objWord.Documents.Add()
$InputFile = $content2+$hl+$content3+$content4
foreach($b in $InputFile){$char = '* '; $c= "$char$b" ; $c}
$objWord.Selection.typetext("$c")

#foreach ($Line in $objWord.Selection.typetext("$content2$hl$content3$content4")) {
#$Line = $line.Insert(0,'* ')
#$Line += '"'}
#$ende = $content2+$hl+$content3+$content4
#$ende|foreach {$_ + "*"}
#$objWord.Selection.typetext("$ende")
#Write-Host $ende

#$objWord.Selection.typetext("$content3")
#$objWord.Selection.typetext("$content4")
#$objDocument.Close()
#$objWord.Quit()
# Stop Winword Process
#$rc = [System.Runtime.Interopservices.Marshal]::ReleaseComObject($objWord)

August 17, 2016 at 3:28 pm

Not sure if this is what you are looking for but I think It might be close:

$objWord = New-Object -ComObject Word.Application
$objWord.Visible = $true
$filename = "$env:USERPROFILE\Documents\test.docx"
$objDocument = $objWord.Documents.Open($filename)

$LETable = $objDocument.Tables.Item(1)
$LETableCols = $LETable.Columns.Count
$LETableRows = $LETable.Rows.Count

#Write-output "Starting to write... "

#Write-output "Starting to write... "

#$asterix = "* "
#Write-Host $LETable.Cell(6,2).Range.Text|foreach {$_ + "*"}
$content2 = $LETable.Cell(6,2).Range.Text -replace '\r\a'," -creplace '(?m)^\s*\r?\n',"
$hl = "`n"
$content3 = $LETable.Cell(7,1).Range.Text -replace '\r\a'," -creplace '(?m)^\s*\r?\n',"
$content4 = $LETable.Cell(7,2).Range.Text -replace '\r\a'," -creplace '(?m)^\s*\r?\n',"
$doc2 = $objWord.Documents.Add()
$InputFile = ($content2,$hl,$content3,$content4)
foreach($b in $InputFile){$char = '* '; $c= "$char$b" ; $c}
$objWord.Selection.typetext("$c")

Changed this line: $InputFile = ($content2,$hl,$content3,$content4)
To be an array so that the foreach had more than One item to iterate through.
The way you had it concatenated together it wasn't an Array which is what Foreach operates on best.

Thom

August 18, 2016 at 7:42 am

Thom, thank you very much. It is close, but it puts only for one string an asterix – "*".
I need to dive into each string and to ask where is a new line and for each new line to put "*"

Thanks.

August 19, 2016 at 4:40 am

Hi Stefan,
It sounds like you are referring to what look like new line withing a cell of a table in Word. These are not new lines, they are Vertical Tabs. You can detect these with RegEx using \v

For Example:

$objWord = New-Object -ComObject Word.Application
$objWord.Visible = $true
$filename = "C:\Users\user\Desktop\New Microsoft Word Document.docx"
$objDocument = $objWord.Documents.Open($filename)

$LETable = $objDocument.Tables.Item(1)
$LETableCols = $LETable.Columns.Count
$LETableRows = $LETable.Rows.Count

#Write-output "Starting to write... "

#Write-output "Starting to write... "

#$asterix = "* "
#Write-Host $LETable.Cell(6,2).Range.Text|foreach {$_ + "*"}
$content2 = "*$($LETable.Cell(1,6).Range.Text -replace '\v', "*")*"
$content2

Results in

*Asdf*fdas*1234*4312*

for Cell 1,6 in a table that contains the following data

Asdf
fdas
1234
4312
  • This reply was modified 2 years, 1 month ago by  Curtis Smith.

August 19, 2016 at 9:14 am

Dear Curtis,

Unfortunately, I couldn't perform successful result.
Maybe my data in word are not god formatted, although I've checked several times..
Please, could you upload your example, or maybe you want my file?
I've managed to do it through C#, but I want to show my friends that power-shell can do it also 🙂

Thanks!

edit: here is my output:
image

August 19, 2016 at 2:19 pm

Would be best for you to provide a sample of the data you are working with, that way we can see the challenge. If you have this working with C#, provide a screenshot of the output your are expecting and want to duplicate with PowerShell along with the data sample.

August 19, 2016 at 2:40 pm

Hi and thank you Curtis.

Here is my folder with two files.
First docx file is the file that contains a table, and I highlighted what I want to be taken from it.
And .txt file with "*" in each row.
test folder

Thanks again!

August 20, 2016 at 3:23 am

Here's how to do it

$objWord = New-Object -ComObject Word.Application
$objWord.Visible = $true
$filename = "D:\Temp\file - This I get as an input object.doc"
$objDocument = $objWord.Documents.Open($filename)

$LETable = $objDocument.Tables.Item(1)
$LETableCols = $LETable.Columns.Count
$LETableRows = $LETable.Rows.Count

#Write-output "Starting to write... "

#Write-output "Starting to write... "

#$asterix = "* "

# -We will just collect all of our values into a single $output variable to use when we type to the new document
# -First we Replace all of the Carriage Return characters with a Carriage Return, an *, and a space.  This puts
#  an * at the beginning of each line.
# -We then replace the bel character with nothing because we don't want it.
# -The result is a string that has no starting *, but has an * at every Carriage Return after that as well as an
#  * and a space at the very end.
$output = $LETable.Cell(6,2).Range.Text -replace "`r", "`r* " -replace "`a"
$output += $LETable.Cell(7,1).Range.Text -replace "`r", "`r* " -replace "`a"
$output += $LETable.Cell(7,2).Range.Text -replace "`r", "`r* " -replace "`a"

$doc2 = $objWord.Documents.Add()

# -Then when we type our text, we add an * and space to the very beginning, and trim the * and space off the very
#  end.
$objWord.Selection.typetext("* $($output.TrimEnd("* "))")

August 22, 2016 at 9:42 am

I was so close! Thank you Curtis! I got still to learn 🙂