【发布时间】:2021-04-04 12:50:54
【问题描述】:
我正在寻找一种方法:
- 在 Excel 工作表中基于单元格 XX 打开 Word 文档(现在,我在单元格 XX 中列出了文档的完整路径。有没有办法可以根据 Word 文档文件名中的标识符打开文档?)
- 使用查找和替换编辑 Word 文档中的文本(Excel 和 Word doc 之间的链接,我正在更新这些链接的路径。旧路径是静态的,新路径会根据用户而变化,并将在单元格 XXX 中找到)李>
- 查找替换后触发更新 word 中的所有链接
- 断开这些链接
- 重命名word文档并将其保存在客户端文件夹中
Sub openfile()
'opening word file based on cell value in excel, this part works
Dim File As String
File = Worksheets("HOME").Range("A54").Value
Set wordapp = CreateObject("word.Application")
wordapp.documents.Open File
wordapp.Visible = True
'finding and replacing text in word file that was opened with text in specific cell from the excel file, not working
objSelection = wordapp.Selection
objSelection.Selection.Find.ClearFormatting
objSelection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "old pathway" 'this will be static text to always find
.Replacement.Text = Worksheets("HOME").Range("A53").Value 'value in the cell changes depending on user
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute replace:=wdReplaceAll
'would like to update all links in the word doc
'would like to break specific links, only the excel links, in the word doc
'would like to rename file and save into a different folder at this point, lost on how to code this
End Sub
我的主要问题是如何在我刚刚打开并“激活”的 Word 文档中触发查找替换序列。
当文档打开时,我收到一条错误消息
运行时错误 450
参数数量错误或属性分配无效
【问题讨论】:
-
我怀疑您是否能够使用
Selection对象完成您的任务,因为Selection 与用户看到的内容相关联。因此 Word 的 Selection 与 Excel 的 Selection 不同,两者都取决于屏幕上的内容。相反,创建一个名为Doc的 Word 文档和一个名为Ws的 Excel 工作表,并将 Word 和 Excel 范围定义为您可以操作的DocRng和WsRng。考虑使用FileOpen对话框来指定要打开的word 文档。 -
您列出了您的要求并转储了一些代码,这不是本网站的工作方式。我们不是免费的代码编写服务。您需要清楚地解释您遇到的实际问题,并提出与您发布的代码相关的一个具体的问题。如果您花一些时间在tour 和阅读help center 页面以了解该网站的工作原理,然后再开始发布,您会发现您在这里的体验会好很多。
-
@KenWhite谢谢,我明白了。我的主要问题是如何在我刚刚打开并“激活”的 word 文档中触发查找替换序列。当文档打开时,我收到一条错误消息“运行时错误 450”错误数量的参数或无效的属性分配“。我的其他笔记是占位符,我更专注于“不工作”部分。为令人困惑的部分道歉格式化。