我正在研究MS Project中的VBA脚本,其中涉及创建工作簿。但是我在定义一个范围时做错了什么,而且它的工作方式似乎有所不同,因为我不在Excel中。
这是我当前的代码,归结为有问题的部分:
Dim myExcel As Object
Dim myWb As Object
Dim myRange as Object 'Not sure if I should declare this one as an object or a range
Dim myRangeString as String
Set myExcel = CreateObject("Excel.Application")
Set myWb = myExcel.Workbooks.Add
myRangeString = "$D$20, $C$23" 'For simplicity. Obviously this is not hard coded.
Set myRange = Range(myRangeString)
此代码在Excel中正常工作。但是在项目中,它停止并返回“运行时错误'1004':方法'对象'_global'失败。”。也许是因为在这种情况下“全局”是项目,而不是出色。因此,我尝试了myexcel.range(myrangestring)的不同变体,但是后来我得到了“应用程序定义或对象定义的错误”。反而。
谁能帮忙?
I'm working on a VBA script in MS Project, which involves the creation of a workbook. But I'm doing something wrong when defining a range, and it seems like it works differently because I'm not in Excel.
This is my current code, boiled down to the problematic part:
Dim myExcel As Object
Dim myWb As Object
Dim myRange as Object 'Not sure if I should declare this one as an object or a range
Dim myRangeString as String
Set myExcel = CreateObject("Excel.Application")
Set myWb = myExcel.Workbooks.Add
myRangeString = "$D$20, $C$23" 'For simplicity. Obviously this is not hard coded.
Set myRange = Range(myRangeString)
This code works fine in Excel. But in Project it stops and throws back "Run-time error '1004': Method 'Range' of object '_Global' failed.". Maybe that's because "Global" in this context is Project and not Excel. So I have tried different variations of MyExcel.Range(myRangeString), but then I get "Application-defined or object-defined error." instead.
Can anyone help?
发布评论
评论(3)
我似乎已经或多或少地解决了它的答案, @orange_guy和@rachel。解决方案是使用分号(不是结肠,而不是逗号)。反正非常感谢!
I seem to have solved it more or less at the same as you posted your answers, @orange_guy and @Rachel. The solution was to use semicolon (not colon, and not comma). Thanks a lot anyways!
您的代码使用较晚的绑定,这意味着对象在运行时是未知的,并且没有 intelliSense 。使用后期绑定的优点是,该代码适用于具有较旧版本的Office的用户。否则,它比用户早期绑定更可取。 (请参阅。)
通过设置对Excel对象库的引用(在VB编辑器,工具:参考,向下滚动并选中Microsoft Excel对象库)的框来使用早期绑定。
这是一个工作的MS Project示例使用后期绑定:使用阵列在块中脱颖而出的代码
是一个好主意 - 也许这就是“ $ d $ d $ 20,$ c $ 23”的意图。但是,正确的范围语法是使用结肠,但是D20:C23尴尬,使用C20:D23。
这是一个早期结合示例,该示例在块中写入数据,对于大量数据而言,它明显更快。
You code is using late binding which means that the objects are unknown until run-time and there is no IntelliSense. The advantage to using late binding is that the code will run fine for users with older versions of Office. Otherwise it's preferable to user early binding. (See Early and Late Binding.)
Use early binding by setting a reference to the Excel Object Library (in the VB Editor, Tools: References, scroll down and check the box for the Microsoft Excel Object Library)
Here's a working MS Project example of your code using late binding:
Writing to Excel in chunks using arrays is a great idea--perhaps that's what was intended by "$D$20, $C$23". However, the correct range syntax would be to use a colon, but D20:C23 is awkward, use C20:D23 instead.
Here is an early binding example that writes data in chunks, which is noticeably faster for large amounts of data.
就像@bigben和@darren一样,我也没有MS项目,但是我在Excel和Outlook中也有类似的交叉应用vba脚本,并在对我来说非常有效。这是片段:
因此,基本上我在任何变量之前都提及该应用程序。另外,可能有效的一件事是 set 您提到的变量(“ myrangestring”)作为对象。我不知道为什么,但是碰巧对我有用。因此,代替:
您会放一些东西:
Just like @BigBen and @Darren, I don't have MS Project either, but I have a similar crossed-application VBA Script among Excel and Outlook and putting the "app" before worked quite well for me. Here's the snippet:
So basically I mention the App before any variable. Also, one thing that might work is to set the variable you mentioned ("myRangeString") as an object. I don't know exactly why, but it happenned to work for me. So instead of:
You would put something alike: