Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我的Excel文档中的编号: param( [参数(必需=$true,位置=0)][字符串]$FilePath, [参数(必需=$true,位置=1)][字符串]$ObjectProperties ) 函数获取属性值{ [CmdletBinding()]_Excel_Vba_Powershell - Fatal编程技术网

我的Excel文档中的编号: param( [参数(必需=$true,位置=0)][字符串]$FilePath, [参数(必需=$true,位置=1)][字符串]$ObjectProperties ) 函数获取属性值{ [CmdletBinding()]

我的Excel文档中的编号: param( [参数(必需=$true,位置=0)][字符串]$FilePath, [参数(必需=$true,位置=1)][字符串]$ObjectProperties ) 函数获取属性值{ [CmdletBinding()],excel,vba,powershell,Excel,Vba,Powershell,我的Excel文档中的编号: param( [参数(必需=$true,位置=0)][字符串]$FilePath, [参数(必需=$true,位置=1)][字符串]$ObjectProperties ) 函数获取属性值{ [CmdletBinding()]参数( [参数(必需=$true)]$ComObject, [参数(必需=$true)][字符串]$Property ) $Binding=“System.Reflection.BindingFlags”-作为[类型] 试一试{ $Object

我的Excel文档中的编号:


param(
[参数(必需=$true,位置=0)][字符串]$FilePath,
[参数(必需=$true,位置=1)][字符串]$ObjectProperties
)
函数获取属性值{
[CmdletBinding()]参数(
[参数(必需=$true)]$ComObject,
[参数(必需=$true)][字符串]$Property
)
$Binding=“System.Reflection.BindingFlags”-作为[类型]
试一试{
$ObjectType=$CoObject.GetType()
$Item=$ObjectType.InvokeMember(“Item”,$Binding::GetProperty,$null,$ComObject,$Property)
返回$ObjectType.InvokeMember(“值”,$Binding::GetProperty,$null,$Item,$null)
}
抓住{
返回$null
}
}
#主要
$Application=新对象-ComObject Excel.Application
$Application.Visible=$false
$Document=$Application.Workbooks.Open($FilePath)
$Properties=$Document.builtinocumentproperties
$Hash=@{}
$Hash.Add(“名称”,$FilePath)
ForEach($ObjectProperties中的属性)
{   
$Value=获取属性值-ComObject$Properties-Property$Property
$Hash.Add($Property,$Value)
}
#COM对象清理
如果($null-ne$文档){
$Document.Close($false)
删除变量名文档
}
如果($null-ne$Properties){
删除变量名属性
}
如果($null-ne$Application){
$Application.Quit()
[System.Runtime.InteropServices.Marshal]::ReleaseComObject($Application)| Out Null
删除变量名应用程序
}
[gc]::collect()
[gc]::WaitForPendingFinalizers()
#显示收集的信息
新对象PSObject-属性$Hash

谢谢,但是您的列表,以及我在Excel文件中运行此操作得到的列表,不包含
修订号
。哇,所以只需使用Excel打开文件并使用该对象模型来提取属性即可。这听起来像是一个非常缓慢的操作,但这可能是我唯一的希望。Yeppers,任何时候你在Office文档的细节,这意味着弹出文件。请记住,Windows资源管理器在后台执行此类操作,以便可以在资源管理器中实时查看这些文档并报告/显示这些属性。如果你有很多文件,当然,使用并行处理或作业。然而,Excel、Word等只能打开一次,检查所有文件,然后将其关闭。想想你必须做些什么来增加数字。您打开Word/Excel,并通过OM使用VBA添加修订版sooo,这将是您重新阅读的关键点。
ProductVersion   FileVersion      FileName
--------------   -----------      --------
                                  example.xls
### Get file properties
## 
Get-ItemProperty -Path 'D:\Temp' -filter '*.xl*' | 
Format-list -Property * -Force
### Enumerate file properties in PowerShell

# get the first file
(
$Path = ($FileName = (Get-ChildItem -Path 'D:\Temp' -Filter '*.xl*').FullName ) | 
Select-Object -First 1
)

$shell = New-Object -COMObject Shell.Application
$folder = Split-Path $path
$file = Split-Path $path -Leaf

$shellfolder = $shell.Namespace($folder)
($shellfile = $shellfolder.ParseName($file))


<#
You'll need to know what the ID of the extended attribute is. 
This will show you all of the ID's:
#>

0..287 | 
Foreach-Object { '{0} = {1}' -f $_, $shellfolder.GetDetailsOf($null, $_) }

# Once you find the one you want you can access it like this:
$shellfolder.GetDetailsOf($shellfile, 216)
# Getting specific properties fomr MS Word
$Path = "D:\Temp"
$ObjectProperties = "Author","Keywords","Revision number"

$Application = New-Object -ComObject Word.Application
$Application.Visible = $false
$Binding = "System.Reflection.BindingFlags" -as [type]

$Select = "Name","Created"
$Select += $ObjectProperties

ForEach ($File in (Get-ChildItem $Path -Include '*.docx' -Recurse))
{   $Document = $Application.Documents.Open($File.Fullname)

    $Properties = $Document.BuiltInDocumentProperties

    $Hash = @{}
    $Hash.Add("Name",$File.FullName)
    $Hash.Add("Created",$File.CreationTime)

    ForEach ($Property in $ObjectProperties)
    {   $DocProperties = [System.__ComObject].InvokeMember("item",$Binding::GetProperty,$null,$Properties,$Property)
        Try {$Value = [System.__ComObject].InvokeMember("value",$binding::GetProperty,$null,$DocProperties,$null)}
        Catch {$Value = $null}

        $Hash.Add($Property,$Value)
    }

    $Document.Close()

    [System.Runtime.InteropServices.Marshal]::ReleaseComObject($Properties) | 
    Out-Null

    [System.Runtime.InteropServices.Marshal]::ReleaseComObject($Document) | 
    Out-Null

    New-Object PSObject -Property $Hash | 
    Select $Select
}
$Application.Quit()

# Results
<#
Name            : D:\Temp\Test.docx
Created         : 06-Feb-20 14:23:55
Author          : ...
Keywords        : 
Revision number : 5
#>


# Getting specific properties fomr MS Excel
$Path = "D:\Temp"
$ObjectProperties = "Author","Keywords","Revision number"

$Application = New-Object -ComObject excel.Application
$Application.Visible = $false
$Binding = "System.Reflection.BindingFlags" -as [type]

$Select = "Name","Created"
$Select += $ObjectProperties

ForEach ($File in (Get-ChildItem $Path -Include '*.xlsx' -Recurse))
{   $Document = $Application.Workbooks.Open($File.Fullname)
    $Properties = $Document.BuiltInDocumentProperties

    $Hash = @{}
    $Hash.Add("Name",$File.FullName)
    $Hash.Add("Created",$File.CreationTime)

    ForEach ($Property in $ObjectProperties)
    {   $DocProperties = [System.__ComObject].InvokeMember("item",$Binding::GetProperty,$null,$Properties,$Property)

        Try {$Value = [System.__ComObject].InvokeMember("value",$binding::GetProperty,$null,$DocProperties,$null)}
        Catch {$Value = $null}

        $Hash.Add($Property,$Value)
    }

    $Document.Close()

    [System.Runtime.InteropServices.Marshal]::ReleaseComObject($Properties) | 
    Out-Null

    [System.Runtime.InteropServices.Marshal]::ReleaseComObject($Document) | 
    Out-Null

    New-Object PSObject -Property $Hash | 
    Select $Select
}
$Application.Quit()

# Results
<#
Name            : D:\Temp\Test.xlsx
Created         : 25-Nov-19 20:47:15
Author          : ...
Keywords        : 
Revision number : 2
#>