2015-11-23 69 views
0

我試圖創建一個不同的第一頁的docx文件,並在頁腳中插入圖像轉換宏VBSCRIPT

Dim oWord 
Dim oDoc 

Set oWord = CreateObject("Word.Application") 
Set oDoc = CreateObject("Word.Document") 

oWord.Visible = False 
Set oDoc = oWord.Documents.Add 

With oWord.ActiveDocument.PageSetup 
    .DifferentFirstPageHeaderFooter = True 
End With 

With oWord.ActiveDocument 
    .ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageFooter 
    .Application.Templates(_ 
    "C:\Users\User\AppData\Roaming\Microsoft\Document Building Blocks\1033\15\Built-In Building Blocks.dotx" _ 
     ).BuildingBlockEntries(" Blank").Insert Where=Selection.Range,  RichText _ 
    =True 
.Selection.Fields.Add Selection.Range, wdFieldEmpty, _ 
    "INCLUDEPICTURE ""http://url.me"" \d ", PreserveFormatting=True 
oWord.ActiveDocument.SaveAs("test.docx"); 

此錯誤一個字來

行:16 字符: 5 錯誤:請求的集合成員不存在。 代碼:800A1735 來源:微軟Word

任何線索爲什麼會發生這種情況?

+0

引用的building block文件是否存在並且是否包含名爲「Blank」的條目? – DanL

+0

是的,此代碼是從Word 2013中的宏中翻譯過來的。 如果需要,請編輯帖子以包含宏。 –

+0

嘗試在第16行之前立即調用'.Application.Templates.LoadBuildingBlocks'。 – DanL

回答

0

您的代碼無法打開指定的構建塊集合。以下是您的代碼的優化版本,可以動態獲取「Built-In Building Building Blocks.dotx」模板的路徑。

Dim oWord 
Dim oDoc 

Set oWord = CreateObject("Word.Application") 
oWord.Visible = False 

Set oDoc = oWord.Documents.Add 

With oWord.ActiveDocument.PageSetup 
    .DifferentFirstPageHeaderFooter = True 
End With 

'Get building block path. 
Dim bbPath, entryName, savePath 
bbPath = GetBuildingBlockPath(oWord, "Built-In Building Blocks.dotx") 
entryName = " Blank" 
savePath = "test.docx" 

With oWord.ActiveDocument 
    .ActiveWindow.ActivePane.View.SeekView = 10 
    oWord.Templates(bbPath).BuildingBlockEntries(entryName).Insert oWord.Selection.Range, True 
    oWord.Selection.Fields.Add oWord.Selection.Range, -1, "INCLUDEPICTURE ""http://url.me"" \d ", PreserveFormatting=True 
    .SaveAs(savePath) 
End With 

Function GetBuildingBlockPath(app, bbName) 
    app.Templates.LoadBuildingBlocks 

    For Each t In app.Templates 
     If LCase(t.Name) = LCase(bbName) Then 
      GetBuildingBlockPath = t.FullName 
      Exit Function 
     End If 
    Next 
End Function 
+0

這很好用, 但是生成的文檔在圖像中的身體而不是頁腳。 –

+0

忘記了一個常數。看到更新的答案;-) – DanL

+0

謝謝,我已經接受了答案。 任何線索如何調整插入圖像的大小? –