Skip to content
This repository has been archived by the owner on Apr 12, 2022. It is now read-only.

Add-WordList ignores -Paragraph parameter #74

Open
kakemonster opened this issue Mar 29, 2022 · 4 comments
Open

Add-WordList ignores -Paragraph parameter #74

kakemonster opened this issue Mar 29, 2022 · 4 comments
Labels
bug Something isn't working

Comments

@kakemonster
Copy link

Thanks for this great module!

I'm trying to add a list after a certain paragraph in a document. This works great for Add-WordText, but Add-WordList always puts the list at the end of the document.

Example Document:

This is a lot of intro text, ipsum etc.
Insert list after this.
This is a lot of secondary text.

Example Code:

$test = Get-WordDocument -FilePath "C:\temp\text.docx" 
$array = @("Test1", "Test2", "Test3") 
$paragraph = Get-WordParagraphs -WordDocument $test | Where-Object Text -eq "Insert list after this."

Add-WordList -WordDocument $test -Paragraph $paragraph -ListData $array -ListType Bulleted -ListLevels 0 -Supress $True
Add-WordText -WordDocument $test -Paragraph $paragraph -Text "List:" -FontSize 11 -HeadingType "Heading3" -Supress $True

Save-WordDocument -WordDocument $test -FilePath $outPath -OpenDocument

Saved Document:

This is a lot of intro text, ipsum etc.
Insert list after this.
List:
This is a lot of secondary text.

1.	Test1
2.	Test2
3.	Test3

It does this even if I'm not adding any text. New-WordList have the same behavior. There aren't any examples with Add-WordList and the paragraph parameter, so am I doing something wrong?

@PrzemyslawKlys PrzemyslawKlys added the bug Something isn't working label Mar 30, 2022
@PrzemyslawKlys
Copy link
Member

Ye, I can confirm this. Given that I am working on completely new module I am not sure if I will be fixing this.

@PrzemyslawKlys
Copy link
Member

What I can suggest tho is to use Documentimo approach which is builtin into PSWriteWord

Documentimo -FilePath $PSScriptRoot\Documentimo-BasicList.docx {
    DocList {
        DocListItem -Text 'Test 1' -Level 1
        DocListItem -Text 'Test 1' -Level 2
        DocListItem -Text 'Test 1' -Level 2
    }
    DocText -LineBreak
} -Open
$TableDesigns = [Xceed.Document.NET.TableDesign].GetEnumNames()

$TableData = @{
    Name          = 'Test'
    Value         = 'Showing design'
    Company       = 'Evotec'
    Module        = 'Documentimo'
    'Best Module' = 'PSWriteWord'
}

Documentimo -FilePath "$PSScriptRoot\TableDesigns.docx" {
    foreach ($Design in $TableDesigns) {
        DocNumbering -Text "Table Design - $Design" -Level 1 -Type Numbered -Heading Heading1 {
            DocTable -DataTable $TableData -Design $Design
        }
    }
} -Open

And here's more complicated example:

@kakemonster
Copy link
Author

Thanks for the suggestion, but I don't see how I can use that to input it into a specific place in the document.

I have two workarounds for other people that need to do something similar.

One where you get a list object from New-WordList and use the InsertListAfterSelf on the document itself.
You have to get a document or create a temporary one if you don't want to append it to the "main" one.
This is because the New-WordList function has Add-WordList in it and WordDocument parameter as a requirement. This should maybe not be there.

This solution outputs it to a numbered list, even if you supply it with "-Type Bulleted". I don't know why.

$paragraph = $document.Paragraphs | Where-Object Text -eq "Insert List here."
$temp = Get-WordDocument -FilePath $tempPath

$list = New-WordList -WordDocument $temp -Paragraph $paragraph -Supress $false {
    New-WordListItem -Level 0 -Text "Test 1"
    New-WordListItem -Level 0 -Text "Test 2"
}
$paragraph.InsertListAfterSelf($list)

The other one is where you loop through an array and adds them as text instead. Then sets that paragraph to style "Bulleted", I don't know if this is correct for English documents, because my document style is in my native language.

This adds an actual bulleted list, but I didn't find any solution for creating indentation, since IdentLevel is ReadOnly.

$paragraph = $document.Paragraphs | Where-Object Text -eq "Insert List here."
$listArray = @("Test 1", "Test2")

foreach ($listItem in $listArray) {
    $paragraph = Add-WordText -WordDocument $document -Paragraph $paragraph -Text $listItem #-Supress $True
    $paragraph.StyleName = "Bulleted"
    #$paragraph.IndentationBefore = 1 # Does nothing
    #$paragraph.IndentLevel = 1 # ReadOnly :(
}

@PrzemyslawKlys
Copy link
Member

You can actually try to fix that in the cmdlet itself - it shouldn't be very hard, but like I said - I wrote completely new .NET library https://github.com/EvotecIT/OfficeIMO

I talk about it a bit here: https://twitter.com/PrzemyslawKlys/status/1496576889250582533

It's far superior and works on Windows / Linux and Mac. The PS version if it will be released under https://github.com/EvotecIT/PSWriteOffice but there's a lot of open points yet to make it workable. I will get complete control over .NET and PowerShell library so it will allow me to configure stuff that was not possible before. It has support for sections, headers, footers, lists, tables and even cover pages. But it requires more work on my side.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants