[英]Populating an excel file from word vba
I'm writing a macro that will populate an excel file with user inputs from active x controls in word. 我正在编写一个宏,该宏将使用来自活动x控件的用户输入来填充excel文件。 I've got almost everything working except that I keep getting an error message when I try and select cell A1 in the sheet that I want to use in the workbook.
除了在尝试在工作簿中要使用的工作表中选择单元格A1时不断收到错误消息外,我几乎可以完成所有工作。 Here is the code:
这是代码:
Workbooks.Open ("mypath\myfile.xlsm")
Workbooks("myfile.xlsm").Activate
Worksheets("sheet1").Select
Range("A1").Select
Do Until (IsEmpty(ActiveCell.Value))
ActiveCell.Offset(1, 0).Select
Loop
ActiveCell.Value = n
ActiveCell.Offset(0, 1).Value = a
ActiveCell.Offset(0, 2).Value = b
ActiveCell.Offset(0, 3).Value = c
Columns("D:D").EntireColumn.AutoFit
Columns("A:A").EntireColumn.AutoFit
Columns("B:B").EntireColumn.AutoFit
Columns("C:C").EntireColumn.AutoFit
Workbooks("myfile.xlsm").Save
Workbooks("myfile.xlsm").Close
The variables in this block of code are the values of the active x controls and are located much further up in the sub. 此代码块中的变量是活动x控件的值,并且位于子控件的更远位置。 This block of code is a small part of an if statement within the sub.
此代码块是sub中if语句的一小部分。 Anyhow, when I take Range("A2").Select out of the code, it works just fine except for the fact that the information that I want to input does not go to the right spot (since it didn't select range A1 to begin with).
无论如何,当我从代码中选择Range(“ A2”)。Select时,除了我要输入的信息没有到达正确的位置(因为它没有选择范围A1)之外,它都可以正常工作首先)。
The error I get is type mismatch 4218. 我得到的错误是类型不匹配4218。
Referencing the Excel object model gives you access to some global objects defined in that object model. 引用Excel对象模型可让您访问该对象模型中定义的某些全局对象。
VBA resolves identifiers in this order: VBA按以下顺序解析标识符:
So when you invoke Range
meaning to be a call to the Excel object model, you actually invoke the same-name Range
global member that's defined in the Word object model. 因此,当您调用
Range
含义是对Excel对象模型的调用时,实际上是调用了在Word对象模型中定义的相同名称的Range
全局成员。
Note I say member and mean it: these are unqualified member calls to Global.Range
. 请注意,我说的是成员 ,是这样:它们是对
Global.Range
不合格成员调用 。 This is important, because a member implies an object , and since everything in the Excel object model (Word's too) has an Application
property, then if you're not explicit about exactly what you're referring to, you might be implicitly creating an Excel.Application
object, that you can't quite clean up properly. 这很重要,因为成员隐含一个对象 ,并且由于Excel对象模型中的所有内容(Word也是如此)都具有
Application
属性,因此,如果您不清楚所指的是什么,则可能会隐式创建一个Excel.Application
对象,您可能无法正确清理。 This usually translates into a "ghost" EXCEL.EXE process lingering in Task Manager well after your macro finishes running. 宏完成运行后,这通常会转换为在任务管理器中持续存在的“幽灵” EXCEL.EXE进程。
The trick is to make that reference explicit, and explicitly constrain its lifetime - a With
block is perfect for this: 诀窍是使该引用显式,并显式限制其寿命-一个
With
块非常适合此操作:
With New Excel.Application
With .Workbooks.Open(path)
With .Worksheets("Sheet1")
lRow = .Cells(.Rows.Count, 1).End(xlUp).Row + 1
.Cells(lRow, 1) = n
.Cells(lRow, 2) = a
.Cells(lRow, 3) = b
.Cells(lRow, 4) = c
.Columns("A:D").EntireColumn.AutoFit
End With
.Save
.Close
End With
.Close
End With
I'm guessing as I don't usually run Excel from Word, but I think the problem might be related to everything being unqualified from Word. 我正在猜测,因为我通常不从Word运行Excel,但是我认为问题可能与从Word获得的所有不合格有关。
If Workbooks.Open
is working, then we can just hang everything related to that workbook on that.. 如果
Workbooks.Open
正在运行,那么我们可以将与此工作簿相关的所有内容挂在上面。
Try the following code instead: 请尝试以下代码:
Dim myWkBk As Workbook, lRow As Long
Set myWkBk = Excel.Application.Workbooks.Open("mypath\myfile.xlsm")
With myWkBk.Sheets("sheet1")
lRow = .Cells(.Rows.Count, 1).End(xlUp).Row + 1
.Cells(lRow, 1) = n
.Cells(lRow, 2) = a
.Cells(lRow, 3) = b
.Cells(lRow, 4) = c
.Columns("A:D").EntireColumn.AutoFit
End With
myWkBk.Save
myWkBk.Close
I've got it figured out. 我知道了。 @Cindy Meister I just needed to add an ActiveSheet.
@Cindy Meister我只需要添加一个ActiveSheet。 qualifier on the troubled line:
麻烦线上的预选赛:
Workbooks.Open ("H:\Second Rotation\OBI project\answersUsers.xlsm")
Workbooks("answersUsers.xlsm").Activate
Sheets("Answers Users").Select
ActiveSheet.Range("A1").Select
Do Until (IsEmpty(ActiveCell.Value))
ActiveCell.Offset(1, 0).Select
Loop
ActiveCell.Value = n
ActiveCell.Offset(0, 1).Value = cwid
ActiveCell.Offset(0, 2).Value = mann
ActiveCell.Offset(0, 3).Value = dept
Columns("A:D").EntireColumn.AutoFit
Workbooks("answersUsers.xlsm").Save
Workbooks("answersUsers.xlsm").Close
Dim myWkBk As Workbook, lRow As Long
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.