简体   繁体   English

Powershell 和 Microsoft Word 书签和单元格

[英]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.它以正确的格式输出值,这意味着查询有效并且 Word 中的书签是正确的,但所有驱动器都放置在一个单独的、展开的单元格中。 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'.现在,我将 Word 书签“存储”放置在“存储”右侧的大单元格中。

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:.假设我有一个这样的服务器,带有 C: - G: 而另一台服务器只有一个 C: 和 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.正如评论中所同意的,有一些基于 Word-VBA 的活动文档提示。 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 : A)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: B)在带有书签的单元格下方添加附加行:

    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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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