从excel VBA我将一条水平线添加到Word文档的标题中。
Dim a As Word.Range
Set a = oWord.ActiveDocument.Sections(1).headers(wdHeaderFooterPrimary).Range
a.Collapse Direction:=wdCollapseEnd
a.InlineShapes.AddHorizontalLineStandard
接下来我要格式化该行:
a.InlineShapes(1).Height = 1
但是这个抛出并且错误 5941 - 该集合的请求成员不存在。
我也试过
With a.InlineShapes.AddHorizontalLineStandard
.Height = 1
End With
但也没有工作。
我尝试使用Word vba中的代码。我在这里错过了什么吗?如何格式化Excel中的行?
修改
添加行后我停止了代码。然后我执行了.InlineShapes.Count
,返回0.然后我在文档正文中添加了一行并再次执行,然后返回1。所以问题似乎是标题无法从Excel访问?
答案 0 :(得分:1)
试试这个:
Dim a As Object
Set a = GetObject(, "Word.Application")
With a.ActiveDocument.Sections(1).headers(wdHeaderFooterPrimary)
.Range.InlineShapes.AddHorizontalLineStandard.Height = 1
End With
答案 1 :(得分:1)
尝试以下操作,它使用InlineShape对象“保留”您要添加的行,以便您可以直接处理它。我无法弄清楚为什么你的工作不起作用,但如果这不起作用,它至少可以给你一个更丰富的错误信息:
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