From excel VBA I am adding a horizontal line to a header of a Word document.
Dim a As Word.Range
Set a = oWord.ActiveDocument.Sections(1).headers(wdHeaderFooterPrimary).Range
a.Collapse Direction:=wdCollapseEnd
a.InlineShapes.AddHorizontalLineStandard
Next I want to format that line:
a.InlineShapes(1).Height = 1
But this throws and error 5941 - The requested member of the collection does not exist.
I also tried
With a.InlineShapes.AddHorizontalLineStandard
.Height = 1
End With
But didnt work either.
I tried the code in Word vba, which worked. Am I missing something here? How can I format the line from Excel?
Edit
I stopped the code after adding the line. Then I executed .InlineShapes.Count
, which returned 0. Then I added a line in the document body and executed again, which return 1 then. So the problem seems to be that the header can not be access from Excel?
Try this:
Dim a As Object
Set a = GetObject(, "Word.Application")
With a.ActiveDocument.Sections(1).headers(wdHeaderFooterPrimary)
.Range.InlineShapes.AddHorizontalLineStandard.Height = 1
End With
Try the following, which uses an InlineShape object to "hold" the line you're adding so that you can address it directly. I can't figure out why what you have isn't working, but if this doesn't work it might at least give you a more informative error message:
Dim rng As word.Range
Dim ils As word.InlineShape
Set rng = oWord.ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Range
rng.Collpase Direction:=wdCollapseEnd
Set ils = rng.InlineShapes.AddHorizontalLineStandard(rng)
ils.height = 1
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.