我有一个代码片段,它打开一个word模板,然后尝试设置命名FormFields的值。
$options = @{
'foo' ='bar';
'bizz' = 'buzz';
}
$document = 'C:\Form_template.doc'
$word = new-object -ComObject Word.application
$doc = $word.Documents.Open($document)
$word.visible = $true
$fields = $doc.FormFields
$fields.item('foo').Result = $options['foo']
$fields.item('bizz').Result = $options['bizz']运行此代码段时,窗体字段的设置不正确。但是,当我跑的时候
$fields.item('foo').Result = 'bar'
$fields.item('bizz').Result = 'buzz'这些值是按需要设置的。
编辑:下面是交互式shell中的一个示例
PS C:\>$fields.item('foo').Result = $options['foo']
PS C:\>$fields.item('bizz').Result = $options['bizz']
PS C:\> $doc.FormFields.Item('foo').Result
PS C:\> $doc.FormFields.Item('bizz').Result
PS C:\>#Now let's try setting the values directly with a string.
PS C:\>$fields.item('foo').Result = 'bar'
PS C:\>$fields.item('bizz').Result = 'buzz'
PS C:\> $doc.FormFields.Item('foo').Result
bar
PS C:\> $doc.FormFields.Item('bizz').Result
buzz为什么我不能使用哈希值来设置FormField值?
发布于 2017-01-11 18:48:11
根据Ben的建议,使用[string]$options['bizz']转换字符串可以正确设置值。
PS C:\>$fields.item('bizz').Result = [string]$options['bizz']
PS C:\> $doc.FormFields.Item('foo').Result
buzz经过进一步的研究,我发现将散列值转换为string返回了一个使用.toString()的不同类型的vs。
PS C:\> $options['bizz'].getType()
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True String System.Object
PS C:\> $options['bizz'].toString().getType()
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True String System.Object
PS C:\> [string]$options['bizz'].getType()
string我对此很感兴趣,但这将是另一个帖子的主题。合适的演员阵容解决了我的问题。
https://stackoverflow.com/questions/41597778
复制相似问题