VBA Word 宏中 Excel vlookup 的错误 424 "Object Required"
VBA error 424 "Object Required" for Excel vlookup in Word Macro
我正在尝试通过 MS Word 宏在 MS Excel 工作簿上执行 MS Excel vlookup。
我的 MS Word 宏:
Sub Salary_Step1()
bool_debug = True ' set to False once code is working
Dim int_PayGrade As Integer
Dim xlApp, xlWorkBook As Object
Dim str_Locality As String, str_WorkBook As String, str_Worksheet As String
Set xlApp = CreateObject("Excel.Application")
int_PayGrade = ActiveDocument.FormFields("PayGrade").DropDown.Value - 1
str_Locality = ActiveDocument.FormFields("Locality").Result
If (bool_debug) Then MsgBox ("PayGrade = " & int_PayGrade & vbCrLf & "Locality = " & str_Locality)
Application.ScreenUpdating = False
str_WorkBook = "C:\Users\" & Environ("Username") & "\Documents\Announcement Postion Template\PayScales.xlsx"
str_Worksheet = ActiveDocument.FormFields("Locality").Result
If Dir(str_WorkBook) = "" Then
MsgBox "Cannot find the designated workbook: " & str_WorkBook, vbExclamation
Exit Sub
End If
With xlApp
.Visible = True ' set to False when code is working
Set xlWorkBook = .Workbooks.Open(FileName:=str_WorkBook, ReadOnly:=True, AddToMru:=False)
With xlWorkBook
Set xlSheet = .Worksheets(str_Worksheet)
With xlSheet
.Select ' comment out when code is working
int_salary_step1 = .WorksheetFunction.VLookup(int_PayGrade, Sheet1.Range("A1:K16"), 2, False)
int_salary_step10 = .WorksheetFunction.VLookup(int_PayGrade, Sheet1.Range("A1:K16"), 11, False)
End With
.Close False ' set to True when code is working
End With
.Quit
End With
If (bool_debug) Then MsgBox ("Pay Step 1 = " & int_salary_step1 & vbCrLf & "Pay Step 10 = " & int_salary_step10)
Set xlWorkBook = Nothing: Set xlApp = Nothing
Application.ScreenUpdating = True
End Sub
我知道
- 正在打开工作簿,因为
第一个 With
中的 .Visible = True ' set to False when code is working
- 选择了正确的 sheet 因为
第三个 With
. 中的 .Select ' comment out when code is working
我明白了
Error 404 Object Required
当我尝试执行以下任一操作时:
int_salary_step1 = .WorksheetFunction.VLookup(int_PayGrade, Sheet1.Range("A1:K16"), 2, False)
int_salary_step10 = .WorksheetFunction.VLookup(int_PayGrade, Sheet1.Range("A1:K16"), 11, False)
我 Dim
将 xlApp
、xlWorkBook
和 xlSheet
编辑为 Object
,并且 Set
每一个。当我尝试 .WorksheetFunction.VLookup(...)
.
时,我认为我正在使用 Object
根据 BigBen 和 Timothy Rylatt 的评论,我添加了 Option Explicit
,发现我没有 Dim
编辑所有变量,尤其是 sheet1
,这打错了,应该是xlSheet
。所以我的错。
但是 Dim
编辑了我所有的变量后,我得到了
Run-time error '438'
Object doesn't support this property or method
真的没有什么好理由,我试过了
int_salary_step1 = .Application.VLookup(int_PayGrade, Sheet1.Range("A1:K16"), 2, False)
int_salary_step10 = .Application.VLookup(int_PayGrade, Sheet1.Range("A1:K16"), 11, False)
相对于
int_salary_step1 = .WorksheetFunction.VLookup(int_PayGrade, Sheet1.Range("A1:K16"), 2, False)
int_salary_step10 = .WorksheetFunction.VLookup(int_PayGrade, Sheet1.Range("A1:K16"), 11, False)
而且,中提琴!它运行正常!
非常感谢 BigBen 和 Timothy Rylatt!
我正在尝试通过 MS Word 宏在 MS Excel 工作簿上执行 MS Excel vlookup。
我的 MS Word 宏:
Sub Salary_Step1()
bool_debug = True ' set to False once code is working
Dim int_PayGrade As Integer
Dim xlApp, xlWorkBook As Object
Dim str_Locality As String, str_WorkBook As String, str_Worksheet As String
Set xlApp = CreateObject("Excel.Application")
int_PayGrade = ActiveDocument.FormFields("PayGrade").DropDown.Value - 1
str_Locality = ActiveDocument.FormFields("Locality").Result
If (bool_debug) Then MsgBox ("PayGrade = " & int_PayGrade & vbCrLf & "Locality = " & str_Locality)
Application.ScreenUpdating = False
str_WorkBook = "C:\Users\" & Environ("Username") & "\Documents\Announcement Postion Template\PayScales.xlsx"
str_Worksheet = ActiveDocument.FormFields("Locality").Result
If Dir(str_WorkBook) = "" Then
MsgBox "Cannot find the designated workbook: " & str_WorkBook, vbExclamation
Exit Sub
End If
With xlApp
.Visible = True ' set to False when code is working
Set xlWorkBook = .Workbooks.Open(FileName:=str_WorkBook, ReadOnly:=True, AddToMru:=False)
With xlWorkBook
Set xlSheet = .Worksheets(str_Worksheet)
With xlSheet
.Select ' comment out when code is working
int_salary_step1 = .WorksheetFunction.VLookup(int_PayGrade, Sheet1.Range("A1:K16"), 2, False)
int_salary_step10 = .WorksheetFunction.VLookup(int_PayGrade, Sheet1.Range("A1:K16"), 11, False)
End With
.Close False ' set to True when code is working
End With
.Quit
End With
If (bool_debug) Then MsgBox ("Pay Step 1 = " & int_salary_step1 & vbCrLf & "Pay Step 10 = " & int_salary_step10)
Set xlWorkBook = Nothing: Set xlApp = Nothing
Application.ScreenUpdating = True
End Sub
我知道
- 正在打开工作簿,因为
第一个With
中的 - 选择了正确的 sheet 因为
第三个With
. 中的
.Visible = True ' set to False when code is working
.Select ' comment out when code is working
我明白了
Error 404 Object Required
当我尝试执行以下任一操作时:
int_salary_step1 = .WorksheetFunction.VLookup(int_PayGrade, Sheet1.Range("A1:K16"), 2, False)
int_salary_step10 = .WorksheetFunction.VLookup(int_PayGrade, Sheet1.Range("A1:K16"), 11, False)
我 Dim
将 xlApp
、xlWorkBook
和 xlSheet
编辑为 Object
,并且 Set
每一个。当我尝试 .WorksheetFunction.VLookup(...)
.
Object
根据 BigBen 和 Timothy Rylatt 的评论,我添加了 Option Explicit
,发现我没有 Dim
编辑所有变量,尤其是 sheet1
,这打错了,应该是xlSheet
。所以我的错。
但是 Dim
编辑了我所有的变量后,我得到了
Run-time error '438'
Object doesn't support this property or method
真的没有什么好理由,我试过了
int_salary_step1 = .Application.VLookup(int_PayGrade, Sheet1.Range("A1:K16"), 2, False)
int_salary_step10 = .Application.VLookup(int_PayGrade, Sheet1.Range("A1:K16"), 11, False)
相对于
int_salary_step1 = .WorksheetFunction.VLookup(int_PayGrade, Sheet1.Range("A1:K16"), 2, False)
int_salary_step10 = .WorksheetFunction.VLookup(int_PayGrade, Sheet1.Range("A1:K16"), 11, False)
而且,中提琴!它运行正常!
非常感谢 BigBen 和 Timothy Rylatt!