[英]VBA, EXCEL, WORD: Formatting of Horizontal line from Excel in Word Header
从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
,该.InlineShapes.Count
返回0。然后我在文档主体中添加了一行并再次执行,然后返回1。 所以问题似乎是无法从Excel访问标头?
尝试这个:
Dim a As Object
Set a = GetObject(, "Word.Application")
With a.ActiveDocument.Sections(1).headers(wdHeaderFooterPrimary)
.Range.InlineShapes.AddHorizontalLineStandard.Height = 1
End With
尝试以下操作,该操作使用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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.