VBA,EXCEL,WORD:从Word中的Excel的水平行的格式

从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访问?

尝试以下操作,它使用InlineShape对象来“保留”要添加的行,以便直接解决该问题。 我不明白为什么你有什么不工作,但如果这不起作用,它可能至less会给你一个更丰富的错误消息:

 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 

尝试这个:

 Dim a As Object Set a = GetObject(, "Word.Application") With a.ActiveDocument.Sections(1).headers(wdHeaderFooterPrimary) .Range.InlineShapes.AddHorizontalLineStandard.Height = 1 End With