如何使用Word宏从word文档中复制excel和past中的未定义范围。 我试过下面的但是没能成功。能帮帮我吗。
注意:如果我提到它的工作范围很好,而我想在word宏中使用代码NumberOfRows = .Range("A65536").End(xlUp).Row
- 我得到运行时错误1004。
Sub InputExcel()
Set appExcel = CreateObject("Excel.Application")
Dim INP_File As Variant
Dim lngRows As Long
Dim LenMgs As Long
Dim NumberOfRows As String
INP_File = appExcel.GetOpenFilename("Excel files (*.xlsx;*.xls),*.xlsx;*.xls", 2)
appExcel.Workbooks.Open INP_File
If INP_File > 0 Then
Selection.Find.ClearFormatting
With Selection.Find
.Text = "Sample"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
Selection.HomeKey Unit:=wdLine
LenMgs = appExcel.Worksheets("Sheet1").Range("A1").CurrentRegion.Rows.Count
'NumberOfRows = appExcel.Worksheets("Sheet1").Range("A65536").End(xlUp).Row
Set Rng = appExcel.Worksheets("Sheet1").Range(appExcel.Worksheets("Sheet1").Cells(4, 1), appExcel.Worksheets("Sheet1").Cells(LenMgs, 5))
'Rng.Copy
'appExcel.Worksheets("Sheet1").Range("A1:B5").Copy - This is working !! if I specify the range.
Selection.Paste
End If
appExcel.ActiveWorkbook.Close
appExcel.Quit
Set appExcel = Nothing
End Sub
答案 0 :(得分:1)
您收到错误是因为xlUp
是Excel常量,而MS Word无法识别它,因为您使用Late Binding连接Excel。
您必须将此声明为代码的顶部
Const xlup = -4162
另外,您可能需要阅读THIS以查找Excel中的最后一行?