简体   繁体   中英

Powershell and Microsoft Word bookmarks and cells

So here's one for ya, a bit complicated (I think).

I have a document template, shown to below:word_cells

I have a storage function and call shown below: Function (a bit ugly):

function storage
{
param([string]$CompName)
Get-WmiObject win32_volume -ComputerName $CompName -Filter "DriveType=3 AND Label <> 'System Reserved' AND DriveLetter IS NOT NULL" | Sort-Object DriveLetter | ForEach-Object{"{0}, {1} - {2}gb `n" -f $_.Name,$_.Label,([Math]::Truncate($_.Capacity/1GB))}
}

Call:

#Storage input
Write-Host "...writing Storage"
$objRange = $wordDoc.Bookmarks.Item("storage").Range
$objRange.Text = storage -CompName $computer
$wordDoc.Bookmarks.Add("storage",$objRange) | Out-Null

Now as it stands, it half works. It pumps out the values in the correct format meaning the query works and the bookmark in Word is correct, but all of the drives are placed in a single, stretch out cell. My issue now is that I would like to have each drive appear in it's own cell (largest column) within the template. Right now, I have the Word bookmark 'storage' placed in the large cell immediately to the right of 'Storage'.

Is there a way to place each string of drive into it's own cell systematically? Is this possible? Furthermore (if this is doable), is there a way to auto-add rows based on needs within the code? Say I have a server like this with a C: - G: while another server has simply a C: and D:. Is it possible to start the template with a single row there and add more as needed from the code?

What current entry looks like:

当前_条目

What I would like it to look like:

首选

Thank you!

As agreed in comments, there are some tips based on Word-VBA for active document. Hope this will help to make a step forward.

Before在此处输入图片说明

A) to add information in the cell below cell where your storage bookmark is located :

    Sub text_to_cell_below_bookmark()

        ActiveDocument.Bookmarks("storage").Range.Next( _
        wdCell, _
        ActiveDocument.Bookmarks("storage").Range.Tables(1).Columns.Count _
                ).Text = "your storage information in the cell below"
    End Sub

After在此处输入图片说明

B) to add additional row below the cell with bookmark:

    Sub additional_row_below_bookmark()

    ActiveDocument.Bookmarks("storage").Range.Tables(1).Rows.Add _
        ActiveDocument.Bookmarks("storage").Range.Tables(1).Rows( _
        ActiveDocument.Bookmarks("Storage").Range.Information(wdEndOfRangeRowNumber) + 1)
    End Sub

Edit how to get row and column of cell in table where your bookmark is located:

Dim bmRow, bmCol
'to get row
bmRow = ActiveDocument.Bookmarks("storage").Range.Information(wdEndOfRangeRowNumber)
'to get column
bmCol = ActiveDocument.Bookmarks("storage").Range.Information(wdEndOfRangeColumnNumber)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM