简体   繁体   English

VBA,EXCEL,WORD:Word标题中Excel中水平线的格式

[英]VBA, EXCEL, WORD: Formatting of Horizontal line from Excel in Word Header

From excel VBA I am adding a horizontal line to a header of a Word document. 从excel VBA,我在Word文档的标题中添加一条水平线。

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. 但这引发了错误5941-集合中所请求的成员不存在。

I also tried 我也试过

With a.InlineShapes.AddHorizontalLineStandard 
    .Height = 1
End With

But didnt work either. 但也没有工作。

I tried the code in Word vba, which worked. 我尝试了在Word vba中运行的代码。 Am I missing something here? 我在这里想念什么吗? How can I format the line from Excel? 如何从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. 然后,我执行了.InlineShapes.Count ,该.InlineShapes.Count返回0。然后我在文档主体中添加了一行并再次执行,然后返回1。 So the problem seems to be that the header can not be access from Excel? 所以问题似乎是无法从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. 尝试以下操作,该操作使用InlineShape对象“保留”您要添加的行,以便可以直接对其进行寻址。 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM