How to add rows to a premade table

Hi Guys,

I’m trying to do my Dyslexic friend a favour by automating his invoices.

So far I’ve built him a database to keep all of his data digitally so he doesn’t need to write things down, a few windows forms written in powershell to do things like customer’s details jobs and parts etc. Up until this point my powershell was really basic so I’m really proud I’ve got this far without having to ask for help, however this ones got me stumped 🙁

I’ve come to the part where I need to create an invoice. So I made a template in word (https://imgur.com/a/U2EfD3v)

I’ve written a function that does the find and replace on the tags with !! around them which was fairly straight forward.

Now comes the hard part. We’ve worked out that an invoice can have many jobs and many parts. My powershell form has 2 arrays of objects that collect the information for jobs, parts labour and part cost) based on the invoice number the form generates in the database. What I’m trying to do is work out how to open my template and grow the formatted sections with this data.

What I think I need to figure out:

Do a count of how many jobs are in the array

Open word document and go down 15 rows (the one under the title “job description”)

Add table row range of the count above

Loop through this range and add my data in the table cells

Do something about the formatting so the borders look like one box

Do the same for the parts table part

Can any one point me in the right direction on how to achieve the above; it’s been driving me a little mad

Many thanks for any help

:edit sandbox code ive got so far

Function Find-Replace {

Param($find,$Replace)

$objSelection = $objWord.Selection

$FindText = $find

$MatchCase = $False

$MatchWholeWord = $true

$MatchWildcards = $False

$MatchSoundsLike = $False

$MatchAllWordForms = $False

$Forward = $True

$Wrap = $wdFindContinue

$Format = $False

$wdReplaceNone = 0

$ReplaceWith = $replace

$wdFindContinue = 1

$a = $objSelection.Find.Execute($FindText,$MatchCase,$MatchWholeWord,$MatchWildcards,$MatchSoundsLike,$MatchAllWordForms,$Forward,$Wrap,$Format,$ReplaceWith)

}

#Variables to pass in

$InvoiceNumber =”123456″

$InvoiceDate = Get-Date -format “dd-MMM-yyyy”

$CustomerName =”ME!”

$customerAddress1 = “Myhouse”

$customerAddress2 = “my street”

$customerAddress3 = “my town”

$customerAddress4 = “My county”

$customerAddress5 = “My postcode”

$objWord = New-Object -comobject Word.Application

$objWord.Visible = $false

$objDoc = $objWord.Documents.Open(“C:JDSinvoice.doc”)

Find-Replace -find “!invoiceNumber!” -Replace $InvoiceNumber

Find-Replace -find “!date!” -Replace $InvoiceDate

Find-Replace -find “!CustomerName!” -Replace $CustomerName

Find-Replace -find “!customerAddress1!” -Replace $customerAddress1

Find-Replace -find “!customerAddress2!” -Replace $customerAddress2

Find-Replace -find “!customerAddress3!” -Replace $customerAddress3

Find-Replace -find “!customerAddress4!” -Replace $customerAddress4

Find-Replace -find “!customerAddress5!” -Replace $customerAddress5

$objDoc.SaveAs(“C:JDSnew.doc”)

$objWord.Quit()

submitted by /u/annonymouse2009
[link] [comments]

Leave a Reply