tri_94 Posted February 25, 2020 Posted February 25, 2020 Hi All I'm working a on a project. I creating group policy reports. Currently i have exported all my gpo's to html. Manually loaded these in word copy and pasted to a single document and changed the heading style, created a Table of contents. Problem is its a very time consuming job. I've looked at Convert-WordDocument.ps1 (https://gallery.technet.microsoft.com/scriptcenter/Convert-Word-documents-5ccc1067) which will convert my html's to docx. I believe its possible to change the heading style and join all the word documents together with powershell. Can anyone please help Many thanks
Arthur Posted February 25, 2020 Posted February 25, 2020 PSWinDocumentation and/or PSWriteWord look like they will do what you need.
tri_94 Posted February 26, 2020 Author Posted February 26, 2020 Hi again I've started to piece together what I require. I've found this code to create a new word doc and insert a heading like I require and this works fine. However I need this to edit a word doc and not create a new one. $Word = New-Object -ComObject Word.Application $Word.Visible = $True $names = Get-ChildItem –Path "D:\test\GPO1\Import\Gpos" -Filter *.docx $names | ForEach-Object {Write-Host "$_" $Name = "$_" $File = "D:\test\$Name.docx" $objDoc = $objWord.Documents.Open($item.FullName,$true) $Document = $Word.Documents.Add() $Selection = $Word.Selection $Selection.Style = 'Title' $Selection.TypeText("$Name") $Selection.TypeParagraph() $Selection.Style = 'Heading 1' $Selection.TypeText("Report compiled at $(Get-Date).") $Selection.TypeParagraph() $Report = $File $Document.SaveAs([ref]$Report,[ref]$SaveFormat::wdFormatDocument) $word.Quit() } also found this code which will open a word doc and replace a line of text. But struggling to piece the parts I need together. Any help would be great. Thanks $objWord = New-Object -comobject Word.Application $objWord.Visible = $false $list = Get-ChildItem Path "D:\test\GPO1\Import\Gpos" -Filter *.docx foreach($item in $list){ $objDoc = $objWord.Documents.Open($item.FullName,$true) $objSelection = $objWord.Selection $wdFindContinue = 1 $FindText = "Sara" $MatchCase = $False $MatchWholeWord = $true $MatchWildcards = $False $MatchSoundsLike = $False $MatchAllWordForms = $False $Forward = $True $Wrap = $wdFindContinue $Format = $False $wdReplaceNone = 0 $ReplaceWith = "AJMOO" $wdFindContinue = 1 $ReplaceAll = 2 $a = $objSelection.Find.Execute($FindText,$MatchCase,$MatchWholeWord, ` $MatchWildcards,$MatchSoundsLike,$MatchAllWordForms,$Forward,` $Wrap,$Format,$ReplaceWith,$ReplaceAll) $objDoc.Save() $objDoc.Close() } $objWord.Quit()
tri_94 Posted February 26, 2020 Author Posted February 26, 2020 PSWinDocumentation and/or PSWriteWord look like they will do what you need. Thanks for that had a quick look and it looks abit over the top for what I require, couldn't find much help with it either.
tri_94 Posted February 26, 2020 Author Posted February 26, 2020 I've managed to get another part of the code to work as I require So this will open the docx and change the heading style. It does give me some errors but not sure what the issue is. Thanks Nick $Word = New-Object -comobject Word.Application $Word.Visible = $false $list = Get-ChildItem -Path "D:\test\GPO1\Import\Gpos" -Filter *.docx foreach($item in $list){ $Name = "$_" $File = "D:\test\$Name.docx" $Document= $Word.Documents.Open($item.FullName,$true) $Selection = $Word.Selection $Selection.Style = 'Title' $Selection.Style = 'Heading 1' $Report = $File $Document.Save() $Document.Close() $Word.Quit() }
tri_94 Posted February 26, 2020 Author Posted February 26, 2020 I have a method to get the end result but I'm sure someone can help me make it work properly. Thanks Currently this is what I use/do #Used to Export GPO to HTML Get-GPO -All | % {$_.GenerateReport('html') | Out-File "\FilePath\$($_.DisplayName).html"} # Using Script "Convert-WordDocument.ps1" to convert all html files in folder .\Convert-WordDocument.ps1 -SourcePath "\FilePath" -IncludeFilter *.html -TargetFormat Default # Open each docx file and change the Heading style $Word = New-Object -comobject Word.Application $Word.Visible = $false $list = Get-ChildItem -Path "\FilePath" -Filter *.docx foreach($item in $list){ $Name = "$_" $File = "\FilePath\$Name.docx" $Document= $Word.Documents.Open($item.FullName,$true) $Selection = $Word.Selection $Selection.Style = 'Title' $Selection.Style = 'Heading 1' $Report = $File $Document.Save() #$Document.Close() #$Word.Quit() } #Might have to end task on Word #Create a macro in word using Sub MergeMultiDocsIntoOne() Dim dlgFile As FileDialog Dim nTotalFiles As Integer Dim nEachSelectedFile As Integer Set dlgFile = Application.FileDialog(msoFileDialogFilePicker) With dlgFile .AllowMultiSelect = True If .Show <> -1 Then Exit Sub Else nTotalFiles = .SelectedItems.Count End If End With For nEachSelectedFile = 1 To nTotalFiles Selection.InsertFile dlgFile.SelectedItems.Item(nEachSelectedFile) If nEachSelectedFile < nTotalFiles Then Selection.InsertBreak Type:=wdPageBreak Else If nEachSelectedFile = nTotalFiles Then Exit Sub End If End If Next nEachSelectedFile End Sub #Insert Table of Contents & Page Numbers #Save as PDF
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now