将word宏转换为vbscript

将word宏转换为vbscript,vbscript,ms-word,Vbscript,Ms Word,我正在尝试创建一个具有不同首页的docx文件,并在页脚中插入一个图像 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 .DifferentFir

我正在尝试创建一个具有不同首页的docx文件,并在页脚中插入一个图像

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


有什么线索可以解释为什么会发生这种情况吗?

您的代码无法打开指定的构建块集合。下面是一个优化版本的代码,可以动态获取“内置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

引用的构建块文件是否存在,是否包含名为“Blank”的条目?是的,此代码是从Word 2013中的宏翻译而来的。如果需要,我将编辑文章以包含宏。尝试在第16行之前调用
.Application.Templates.LoadBuildingBlocks
。同样的错误。是word 2013的bug吗?我将尝试另一个安装。我刚刚运行了你的脚本的修改版本(删除了常量,用我的个人构建块路径替换了路径,更改了构建块条目名称),它工作得非常好。出现此错误的原因可能是
应用程序.Templates
集合不包含您指定的内容,或者
BuildingBlockEntries
集合不包含名为“Blank”的条目。尝试拆分代码以查看它是哪一个。这非常有效,但是生成的文档的正文中有图像,而不是页脚。谢谢,我已经接受了答案。有没有关于如何调整插入图像大小的线索?