2016-03-23 95 views
1

從excel VBA中,我將一個水平行添加到Word文檔的標題中。VBA,EXCEL,WORD:格式化Excel中的水平行文字標題

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訪問?

回答

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 
1

試試這個:

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