Я хочу заменить текст в FieldCodes (Word Document).Как я могу использовать переменную для этого? - PullRequest
0 голосов
/ 28 июня 2019

Я хочу заменить текст в FieldCodes (Word Document).Как я могу использовать переменные для этого?

Это для Word Doc со ссылками на другие документы Word (IncludeText Link).Когда я меняю ссылку по одной без переменной, это работает.Когда я использую переменные для него, это не так.

$Desktop = [Environment]::GetFolderPath("Desktop")

$Word = New-Object -ComObject Word.Application
$Document = $Word.Documents.Open("$Desktop\Test.docx")

$Test = 147   # (Test.GetType() = Int32)

$Document.Fields(147)   #Works
$Document.Fields($Test)   #Works
$Document.Fields($Test).LinkFormat.SourceFullName = ""  #Works

$TextLinks = $Document.Fields | Where-Object Type -eq "68" | Select -expand Index
#TextLinks contains value 147 and 149

$Test = $TextLinks[0]  # is also 147 (Test.GetType() = Int32)

$Document.Fields($Test)   #Doesn't work (runs indefinitely)
$Document.Fields($Test).LinkFormat.SourceFullName = ""  #Doesn't work (runs indefinitely)

147..149 | Foreach { $Document.Fields($_).LinkFormat.SourceFullName } #Doesn't work (runs indefinitely)

Обновление:

Теперь он работает с $Test = [INT]$Textlinks[0].Спасибо Синди!Но когда я пытаюсь выполнить цикл, он зависает со вторым значением

$Desktop = [Environment]::GetFolderPath("Desktop")

$Word = New-Object -ComObject Word.Application
$Document = $Word.Documents.Open("$Desktop\Test.docx")

$TextLinks = $Document.Fields | Where-Object Type -eq "68" | Select -expand Index
$ItemNumber = 0
$End = $TextLinks.Count

Do {
    $Item = [INT]$Textlinks[$ItemNumber]
    if ($Document.Fields($Item).LinkFormat.SourceFullName -match "Test") {
        $Link = $Document.Fields($Item).LinkFormat.SourceFullName -replace "Test", "TestTest"
        $Document.Fields($Item).LinkFormat.SourceFullName = $Link
        $Document.Fields($Item).LinkFormat.AutoUpdate = "True"
        $ItemNumber += 1
    }
} Until ($ItemNumber -eq $End)

$Document.Save()
$Word.Quit()
$OUT=[System.Runtime.InteropServices.Marshal]::ReleaseComObject($Word)

Обновление 2:

Код ниже работает нормально, но я не понимаю, почему код выше не

$Desktop = [Environment]::GetFolderPath("Desktop")

$Word = New-Object -ComObject Word.Application
$Document = $Word.Documents.Open("$Desktop\Test.docx")

$TextLinks = $Document.Fields | Where-Object Type -eq "68" | Select -expand Index
$ItemNumber = ($TextLinks.Count)-1
$End = -1

Do {
    $Item = [INT]$Textlinks[$ItemNumber]
    if ($Document.Fields($Item).LinkFormat.SourceFullName -match "Test") {
        $Link = $Document.Fields($Item).LinkFormat.SourceFullName -replace "Test", "TestTest"
        $Document.Fields($Item).LinkFormat.SourceFullName = $Link
        $Document.Fields($Item).LinkFormat.AutoUpdate = "True"
        $ItemNumber -= 1
    }
} Until ($ItemNumber -eq $End)

$Document.Save()
$Word.Quit()
$OUT=[System.Runtime.InteropServices.Marshal]::ReleaseComObject($Word)
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...