I am not sure if this is the right place to ask but I have to start somewhere. I am writing a PowerShell script which will create a Word 2019 document with some information gleaned from a Nessus scan. If successful I will probably use it in other places as well. I’ve made progress but have found something that doesn’t work. I suspect it may be a bug elsewhere (e.g. Word or .Net etc.). However, I thought I better start at the beginning. The basic code is:
$Selection = $Report.Parent.Selection $Selection.Font.Shading.Texture = [Microsoft.Office.Interop.Word.Shading]::wdTextureDarkHorizontal $Selection.Font.Shading.ForegroundPatternColor = [Microsoft.Office.Interop.Word.WdColor]::wdColorRed $Selection.Font.Shading.BackgroundPatternColor = [Microsoft.Office.Interop.Word.WdColor]::wdColorLightBlue Add-Text -Document $Report -Text "This should be shaded text
That produces shaded text right enough but just in plain blue. It does not do create the texture. If I move the line that sets the
backgroundpatterncolor so that it is before the line that sets the
texture then there is no shading whatsoever. It seems that the
foregroundpatterncolor not only don’t work but they stop the
backgroundpatterncolor line working as well.
Part of my attempt to fix this involves running word, creating a line of text and then recording a macro that creates a textured shading. The macro is:
Sub Macro1() With Selection.Font With .Shading .Texture = wdTextureDarkHorizontal .ForegroundPatternColor = wdColorRed .BackgroundPatternColor = 5287936 End With .Borders(1).LineStyle = wdLineStyleNone .Borders.Shadow = False End With With Options .DefaultBorderLineStyle = wdLineStyleSingle .DefaultBorderLineWidth = wdLineWidth050pt .DefaultBorderColor = wdColorAutomatic End With End Sub
and I have replicated it in PowerShell as:
$Selection = $Report.Parent.Selection $Selection.Font.Shading.Texture = [Microsoft.Office.Interop.Word.Shading]::wdTextureDarkHorizontal $Selection.Font.Shading.ForegroundPatternColor = [Microsoft.Office.Interop.Word.WdColor]::wdColorRed $Selection.Font.Shading.BackgroundPatternColor = 5287936 $Selection.Font.Borders(1).LineStyle = [Microsoft.Office.Interop.Word.WdLineStyle]::wdLineStyleNone $Selection.Font.Borders.Shadow = $FALSE $Selection.Application.Options.DefaultBorderLineStyle = [Microsoft.Office.Interop.Word.WdLineStyle]::wdLineStyleSingle $Selection.Application.Options.DefaultBorderLineWidth = [Microsoft.Office.Interop.Word.WdLineWidth]::wdLineWidth050pt $Selection.Application.Options.DefaultBorderColor = [Microsoft.Office.Interop.Word.WdColor]::wdColorAutomatic
and that also fails. If I run the VB macro though, it works and produces the textured shading that I specified. On the other hand, it fails when PowerShell executes the same instructions. I suspect it is a .net or Word problem but I am not sure since it does work in VB.
Any thoughts will be appreciated.