【发布时间】:2015-12-23 19:52:39
【问题描述】:
- 我想使用 Excel 将“标签名称”存储在 A 列中,并将它们关联的“替换文本”存储在 B 列中。代码运行时,它需要收集每个标签,一次一个(逐行),在整个 Word 文档中搜索这些词,并将它们替换为相应的替换词。
- 我注意到页眉和页脚中的特殊标签没有被替换。我转向这篇文章 (http://word.mvps.org/faqs/customization/ReplaceAnywhere.htm) 并发现使用一系列范围(或循环浏览文档中所有可用的故事范围)我能够做到这一点。
- 我按照上面链接中的建议改进了我的代码,只要我的代码嵌入到我的“普通”Word 文件中,它就可以工作,从而使用 Word 中的 VBA 代码对另一个 Word 文档进行操作。但是,我们的目标是在读取 Excel 文件时使用 VBA Excel 来操作替换。
- 当我将代码移至 Excel 时,我遇到了一个自动化错误,该错误显示为,
“运行时错误'-2147319779 (8002801d)': 未注册自动化错误库。”。
- 我从查看注册表到使用“Word.Application.12”代替“Word.Application”来寻找答案。
我有一台装有 Microsoft Office 2007 的 Windows 7 64 位机器。我选择了以下库:
-
Excel:
- Visual Basic 应用程序
- Microsoft Excel 12.0 对象库
- OLE 自动化
- Microsoft Access 12.0 对象库
- Microsoft Outlook 12.0 对象库
- Microsoft Word 12.0 对象库
- Microsoft Forms 2.0 对象库
- Microsoft Office 14.0 对象库
-
单词:
- Visual Basic 应用程序
- Microsoft Word 12.0 对象库
- OLE 自动化
- Microsoft Office 12.0 对象库
我对在 Excel 内部操作 VBA 没有任何问题。通常,我会将一组字符串传递给该函数,但现在,我已将字符串嵌入函数内部,就好像我只打算用另一个预定字符串交换一个字符串(对于任意数量的实例) .
Function Story_Test()
Dim File As String
Dim Tag As String
Dim ReplacementString As String
Dim a As Integer
Dim WordObj As Object
Dim WordDoc As Object
Dim StoryRange As Word.Range
Dim Junk As Long
Dim BaseFile As String
'Normally, these lines would be strings which get passed in
File = "Z:\File.docx"
Tag = "{{Prepared_By}}"
ReplacementString = "Joe Somebody"
'Review currently open documents, and Set WordDoc to the correct one
'Don't worry, I already have error handling in place for the more complex code
Set WordObj = GetObject(, "Word.Application")
BaseFile = Basename(File)
For a = 1 To WordObj.Documents.Count
If WordObj.Documents(a).Name = BaseFile Then
Set WordDoc = WordObj.Documents(a)
Exit For
End If
Next a
'This is a fix provided to fix the skipped blank Header/Footer problem
Junk = WordDoc.Sections(1).Headers(1).Range.StoryType
'Okay, this is the line where we can see the error.
'When this code is run from Excel VBA, problem. From Word VBA, no problem.
'Anyone known why this is???
'***********************************************************************
For Each StoryRange In WordObj.Documents(a).StoryRanges
'***********************************************************************
Do
'All you need to know about the following function call is
' that I have a function that works to replace strings.
'It works fine provided it has valid strings and a valid StoryRange.
Call SearchAndReplaceInStory_ForVariants(StoryRange, Tag, _
ReplacementString, PreAdditive, FinalAdditive)
Set StoryRange = StoryRange.NextStoryRange
Loop Until StoryRange Is Nothing
Next StoryRange
Set WordObj = Nothing
Set WordDoc = Nothing
End Function
【问题讨论】:
-
这个问题再好不过了。一种建议是检查 Word-VBA 项目中的引用,并查看 Excel-VBA 项目中缺少哪些引用
-
与您的解决方案相关的唯一小问题是,在 VBA Excel 中我有 Microsoft Office 14.0 对象库,而在 VBA Word 中,我有 Microsoft Office 12.0 对象库,如上所述。但是,这似乎不是解决办法。
-
只是为了确认一下,您的意思是您尝试将 Excel-VBA 中的引用从 Office14 更改为 Office12?
-
在我的 Word VBA 中,我只有“Microsoft Office 12.0 对象库”选项。在 Excel VBA 中,我只有“Microsoft Office 14.0 对象库”选项。你相信这里可能会有所不同吗?呃......可能与自动化错误有关的一个??
-
我会说这种可能性是存在的。因为从报错信息来看,很明显是自动化问题。这些 DLL 中的某个地方正试图访问另一个未安装的 DLL。至少,我建议您在安装了完全相同的库的安装上尝试您的代码。不知何故,office14 或 office12 但不是两者都
标签: vba excel runtime-error