[英]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.