I am using Powershell to insert Cross Reference. However to does not move to the next column it just insert into the first one. It does move down to the next row.
$RowCount = 2
foreach ($Risk in $Risksreport)
{
$Document.tables[2].Cell($RowCount,1).Range.text = $Risks.Risk
$Document.tables[2].Cell($RowCount,2).Range.InsertCrossReference(0,9,$Risk.Weakness,1)
$Document.tables[2].Cell($RowCount,3).Range.InsertCrossReference(0,-1,$Risk.Weakness,1)
If($RowCount -ge 6)
{
$Document.tables[2].Rows.Add()
}
$RowCount
}
}```
CodePudding user response:
You will need to "collapse" the range before using InsertCrossReference, for example something like this collapses the range to the beginning of the range, then does the insertion. I leave you to correct the powershell syntax as necessary.
$rng = cument.tables[2].Cell($RowCount,2).Range
$rng.Collapse(1)
$rng.InsertCrossReference(0,9,$Risk.Weakness,1)
Some types of Range operation just do not work when the entire cell has been selected (which is what the first line in the above code does). Usually, Word actually flags an error in that case, but in this case it just does something else, but probably not the thing you want.
You may also need to check some of your other parameters.
- I do not see a "9" in the
wdReferenceKind
Enum (although in fact "9" does not seem to cause a problem here. - You do not say what is in $Risk.Weakness but it might need to be a sequence number rather than a piece of text.
- In VBA, strictly speaking, True is -1 and False is 0. 1 should be treated as True but it is worth checking.