Home > Software engineering >  Powershell Word Document Formatting
Powershell Word Document Formatting

Time:11-11

I am using this Code to create a Word Doucment, Name it and Save it to a user area on a fileserver for a large cohort of people.

I have had a look online and found a few ways to incorporate formatting, does anyone have a simple method of trying to add;

  1. Font Size
  2. Font
  3. Line Spacing

I believe the Font/Font Size is fairly straight forward, but the linespacing might be a bit more complex!

If anyone has any ideas, please let me know! Thank you in advance, have a good day

$Word.Visible = $false;
$Doc = $Word.Documents.Add();
$Section = $Doc.Sections.Item(1);
$Header = $Section.Headers.Item(1);
$Header.Range.Text = "$FirstName $SecondName $ID Activity 1";
$Doc.SaveAs("$fileserver\${date}${time}-${FourID}\Desktop\activity 1_${ID}_${FirstName}_${SecondName}.docx");
$Word.Quit()```

CodePudding user response:

Try:

$Word = New-Object -ComObject Word.Application
$Word.Visible = $false;
$Doc = $Word.Documents.Add();
$Section = $Doc.Sections.Item(1);
$Header = $Section.Headers.Item(1);
$Header.Range.Text = "$FirstName $SecondName $ID Activity 1";

$objRange = $Doc.Range()
$objRange.Font.Name = “Arial”
$objRange.Font.Size = 10
$objRange.Font.Spacing = 1.0
$objRange.ParagraphFormat.LineSpacing = 1

$Doc.SaveAs("$fileserver\${date}${time}-${FourID}\Desktop\activity 1_${ID}_${FirstName}_${SecondName}.docx");
$Word.Quit()

This worked for me.

  • Related