首页 > 解决方案 > MS Word 宏中的“无法获取范围类的 SpecialCell 属性”错误

问题描述

我有以下宏将 Word 文档中的单词替换为单独的 Excel 文件中列出的单词。

Function ListfindAndReplace1()
Dim xlApp As Object
Dim xlWB As Object
Dim xlWS As Object
Dim i As Integer, j As Integer
Dim lastRow As Integer
Set xlApp = CreateObject("Excel.Application")
Set xlWB = xlApp.Workbooks.Open("E:\Dropbox\Dictionaries\zJTA.xlsx") 
Set xlWS = xlWB.Worksheets("Sheet1")
lastRow = xlWS.UsedRange.SpecialCells(xlCellTypeLastCell).Row
For i = 1 To ThisDocument.Words.Count - 1 Step 1
For j = 1 To lastRow Step 1
ThisDocument.Words(i) = Replace(ThisDocument.Words(i), xlWS.Cells(j, 1).Value, xlWS.Cells(j, 2).Value)
Next j
Next i
Set xlWS = Nothing
xlWB.Close True
Set xlWB = Nothing
xlApp.Quit
Set xlApp = Nothing
End Function

当我运行宏时,以下行中出现错误消息“运行时错误 1004:无法获取范围类的 SpecialCell 属性”

lastRow = xlWS.UsedRange.SpecialCells(xlCellTypeLastCell).Row

我尝试了很多东西,但没有用。请帮忙。

标签: excelreplacems-wordmacroscompare

解决方案


推荐阅读