从 HTML 中嵌入的 VBScript 访问 VB6 集合项

发布于 2024-09-16 11:12:47 字数 742 浏览 5 评论 0原文

我正在通过实践来学习。我得到了一个 OCX 文件,根据谁给我的说法,该文件是使用 VB6 创建的,我的任务是为其创建一个用户界面,以测试一个写得不好的文档文件中描述的所有功能。最重要的是,我不太精通 VBScript,但在学习过程中我成功地躲过了一些子弹。

我有一个返回 Collection 的方法,当我尝试从 VBScript 访问它时,我只能查询 Count 但当我尝试执行 job 时。 Item(i)job(i) 我收到一条错误,指出它没有该属性或方法。

有人可以为我指明正确的方向,以便能够遍历该集合的内容吗?

我必须使用 JavaScript 来完成此任务,但由于有些事情并不那么容易,我决定也许 VBScript 可以帮助我弥补 JavaScript 无法解决的空白。我可以通过 JavaScript 访问 ActiveXObject 的所有属性,但返回其他 VB 对象的方法对我来说有点晦涩难懂。我尝试过 aJob.Item(iCount)aJob.Items(iCount)aJob(iCount)

我的代码是:

For iCount = 1 To aJobs.Count
     MsgBox("Num " & iCount)
     MsgBox(aJobs.Item(iCount))
Next

谢谢。

i'm learning by practice. I was given an OCX file which according to who gave it to me was created using VB6 and I have the task of creating a user interface for it to test all the functionality that is described in a poorly written documentation file. On top of that I am not well-versed in VBScript but I've managed to dodge a few bullets while learning.

I have a method which returns a Collection and when I try to access it from VBScript I am only able to query the Count but when I try to do job.Item(i) or job(i) I get an error stating it doesn't have that property or method.

Can someone point me in the right direction to be able to traverse the contents of this collection?

I had to do it from JavaScript but since some things weren't that easy I decided that perhaps VBScript would help me bridge the gaps where JavaScript didn't cut it. I can access all properties from the ActiveXObject from JavaScript, but the methods which return other VB objects are a little more obscure to me. I've tried aJob.Item(iCount), aJob.Items(iCount) and aJob(iCount).

My code is:

For iCount = 1 To aJobs.Count
     MsgBox("Num " & iCount)
     MsgBox(aJobs.Item(iCount))
Next

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浅唱々樱花落 2024-09-23 11:12:47

人们经常在 VB6 中创建专门的和/或强类型的集合类。但他们并不总是正确地做到这一点,有时他们会创建没有 Item() 方法的“部分”集合实现(或者无法将其标记为类的默认成员)。他们甚至可能有类似的方法或属性,但将其命名为完全不同的名称。

返回原始 Collection 对象的情况很少见,但这是可以完成的,如果是的话,您就不应该遇到 VBScript 中指出的问题。

我刚刚创建了一个名为“HallLib”的 DLL 项目,其中包含三个类:Hallway、DoorKnobs 和 DoorKnob。 DoorKnobs 类是 DoorKnob 对象的集合。 Hallway 类有一个 DoorKnobs 对象,它使用一组具有随机设置属性的随机 DoorKnob 对象进行初始化。 Hallway.DoorKnobs() 返回 DoorKnobs 集合对象作为其结果。

它在这个脚本中工作正常:

Option Explicit

Dim Hallway, DoorKnobs, DoorKnob

Set Hallway = CreateObject("HallLib.Hallway")
Set DoorKnobs = Hallway.DoorKnobs()

MsgBox "DoorKnobs.Count = " & CStr(DoorKnobs.Count)

For Each DoorKnob In DoorKnobs
    MsgBox "DoorKnob.Material = " & CStr(DoorKnob.Material) & vbNewLine _
         & "DoorKnob.Color = " & CStr(DoorKnob.Color)
Next

更新:

这个脚本产生相同的结果:

Option Explicit

Dim Hallway, DoorKnobs, KnobIndex

Set Hallway = CreateObject("HallLib.Hallway")
Set DoorKnobs = Hallway.DoorKnobs()

MsgBox "DoorKnobs.Count = " & CStr(DoorKnobs.Count)

For KnobIndex = 1 To DoorKnobs.Count
    With DoorKnobs.Item(KnobIndex)
        MsgBox "DoorKnob.Material = " & CStr(.Material) & vbNewLine _
             & "DoorKnob.Color = " & CStr(.Color)
    End With
Next

一样:

Option Explicit

Dim Hallway, DoorKnobs, KnobIndex

Set Hallway = CreateObject("HallLib.Hallway")
Set DoorKnobs = Hallway.DoorKnobs()

MsgBox "DoorKnobs.Count = " & CStr(DoorKnobs.Count)

For KnobIndex = 1 To DoorKnobs.Count
    With DoorKnobs(KnobIndex)
        MsgBox "DoorKnob.Material = " & CStr(.Material) & vbNewLine _
             & "DoorKnob.Color = " & CStr(.Color)
    End With
Next

所以我怀疑你需要使用一些类型库浏览器,如 OLEView 来查看你的 OCX 以查看哪些类和它实际暴露的成员。

People often create specialized and/or strongly typed collection classes in VB6. They don't always do it correctly though, and they sometimes create "partial" collection implementations that have no Item() method (or fail to mark it as the default member of the class). They might even have a similar method or property but name it something entirely different.

It is rarer to return a raw Collection object, but it can be done and if it is you shouldn't have the problems you have indicated from VBScript.

I just created a DLL project named "HallLib" with three classes: Hallway, DoorKnobs, and DoorKnob. The DoorKnobs class is a collection of DoorKnob objects. The Hallway class has a DoorKnobs object that it initializes with a random set of DoorKnob objects with randomly set properties. Hallway.DoorKnobs() returns the DoorKnobs collection object as its result.

It works fine in this script:

Option Explicit

Dim Hallway, DoorKnobs, DoorKnob

Set Hallway = CreateObject("HallLib.Hallway")
Set DoorKnobs = Hallway.DoorKnobs()

MsgBox "DoorKnobs.Count = " & CStr(DoorKnobs.Count)

For Each DoorKnob In DoorKnobs
    MsgBox "DoorKnob.Material = " & CStr(DoorKnob.Material) & vbNewLine _
         & "DoorKnob.Color = " & CStr(DoorKnob.Color)
Next

Update:

This script produces identical results:

Option Explicit

Dim Hallway, DoorKnobs, KnobIndex

Set Hallway = CreateObject("HallLib.Hallway")
Set DoorKnobs = Hallway.DoorKnobs()

MsgBox "DoorKnobs.Count = " & CStr(DoorKnobs.Count)

For KnobIndex = 1 To DoorKnobs.Count
    With DoorKnobs.Item(KnobIndex)
        MsgBox "DoorKnob.Material = " & CStr(.Material) & vbNewLine _
             & "DoorKnob.Color = " & CStr(.Color)
    End With
Next

As does:

Option Explicit

Dim Hallway, DoorKnobs, KnobIndex

Set Hallway = CreateObject("HallLib.Hallway")
Set DoorKnobs = Hallway.DoorKnobs()

MsgBox "DoorKnobs.Count = " & CStr(DoorKnobs.Count)

For KnobIndex = 1 To DoorKnobs.Count
    With DoorKnobs(KnobIndex)
        MsgBox "DoorKnob.Material = " & CStr(.Material) & vbNewLine _
             & "DoorKnob.Color = " & CStr(.Color)
    End With
Next

So I suspect you'll need to use some type library browser like OLEView to look at your OCX to see what classes and members it actually exposes.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文