在一个子对象中后期绑定公共对象变量,并在执行移至下一个子对象时丢失内容

含咖啡因的迈克

我有一个表格,它调用了Module1的许多子程序。在Module1中,我有一个公开声明的对象变量。使用该变量的想法是创建一个后期绑定,scripting.dictionary以避免必须对我当前的vba项目添加太多引用。该词典已成功创建,并已填充到Sub1中。但是,一旦Sub1完成并且Sub2被调用,我注意到字典变量已经还原回其原始类型的Object。

登录表格:

Public progresslbl As Object, subprogresslbl As Object, progressbar As Object, webBr As Object

Private Sub GetExports_Click()
...
...
...
progresslbl.Caption = "Requesting Exports"
RequestExports

'Wait for all emails to be received (reset currentsupplier and count emails, wait for currentsupplier = suppliercount)
WaitforEmails 'Still needs to be created

'Download Exports & Save them to destination user specifies
DownloadFiles


'Restore Outlook: remove temp folder and rule
progresslbl.Caption = "Restoring Outlook Settings"
RestoreOutlook

模块1:

Public IE As Object, downloadTo As String, Outlook As Object, Items As Object, err As Integer, itemdic As Object
'itemdic shows as type Object in Watch window

Sub RequestExports()

    Set itemdic = CreateObject("Scripting.Dictionary"): itemdic.comparemode = vbTextCompare
    'itemdic now shows at type scripting.dictionary in Watch window
    For x = 1 To suppliercount
        With IE.Document
            esplogin.subprogresslbl.Caption = "Searching for Supplier " & x & " of " & suppliercount
            currentsupplier = ActiveSheet.Range("A" & x).Value

            delay 3 'Wait 3 seconds to allow screen to load fully

            .getElementById("supplierSearchTextBox").Focus 'Select Search Box
            .getElementById("supplierSearchTextBox").Value = currentsupplier 'Fill in Search Box

            'Invoke keypress event so the contents are detected
            Set evt = .CreateEvent("keyboardevent"): evt.initEvent "change", True, False
            .getElementById("supplierSearchTextBox").dispatchEvent evt

            Dim searchButton As Object: Set searchButton = .getElementsByTagName("a")(5)
            searchButton.Click

            delay 3

            Dim supplierLink As Object: Set supplierLink = .getElementsByTagName("a")(6)
            'Cycle through list of suppliers in excel until we find another active one
            Do While supplierLink Is Nothing
                err = err + 1
                esplogin.subprogresslbl.Caption = "Supplier Not Found"
                delay 1
                ActiveSheet.Range("A" & x).Interior.Color = vbYellow
                If x = suppliercount Then Exit For
                esplogin.progressbar.Width = 150 / suppliercount * x
                x = x + 1
                esplogin.subprogresslbl.Caption = "Searching for Supplier " & x & " of " & suppliercount
                currentsupplier = ActiveSheet.Range("A" & x).Value
                'Select & Fill in Search Box
                .getElementById("supplierSearchTextBox").Focus
                .getElementById("supplierSearchTextBox").Value = currentsupplier

                'Invoke keypress event so the contents are detected
                Set evt = .CreateEvent("keyboardevent"): evt.initEvent "change", True, False
                .getElementById("supplierSearchTextBox").dispatchEvent evt

                Set searchButton = .getElementsByTagName("a")(5)
                searchButton.Click

                delay 2

                Set supplierLink = .getElementsByTagName("a")(6)
            Loop
            'Login to supplier
            supplierLink.Click

            While IE.Busy
                DoEvents
            Wend

            esplogin.subprogresslbl.Caption = "Exporting Supplier " & x & " of " & suppliercount
            delay 4

            Dim exportButton As Object: Set exportButton = .getElementsByTagName("button")(3)
            exportButton.Click

            delay 1
            .getElementsByTagName("select")(0).Value = "all"
            .getElementsByTagName("select")(1).Value = "5"
            delay 1
            .getElementById("btnExport").Click 'Click Export button
            delay 2

            'Click Ok button to close "Export sent to email" window
            Dim exportResultOK As Object: Set exportResultOK = .getElementById("exportProductModalResul").getElementsByTagName("button")(1)
            exportResultOK.Click

            esplogin.subprogresslbl.Caption = "Awaiting Export Confirm. Email for Supplier " & x & " of " & suppliercount
            delay 1

            Set eitDashboardButton = .getElementsByTagName("a")(11)
            eitDashboardButton.Click
        End With

        'Check to see if latestExport confirmation has arrived yet
        Set latestExport = Items.Find("[Subject] = ""Product Updates Product Export confirmation""")
        'If we haven't already found the latestExport wait and keep checking until we do
        Do While latestExport Is Nothing
            Set latestExport = Items.Find("[Subject] = ""Product Updates Product Export confirmation""")
        Loop

        esplogin.subprogresslbl.Caption = "Received Confirm. Email for Supplier " & x & " of " & suppliercount

        With latestExport
            BatchID = Mid(.Body, InStr(1, .Body, "Batch ID of ", vbTextCompare) + 12, InStrRev(.Body, ".", Len(.Body) - 1, vbTextCompare) - (InStr(1, .Body, "Batch ID of ", vbTextCompare) + 12))
            itemdic.Add currentsupplier, BatchID
            latestExport.Subject = "Product Updates Product Export confirmation - " & currentsupplier
            latestExport.Save 'Save the updated subject
        End With

        esplogin.progressbar.Width = 150 / suppliercount * x
    Next x

    esplogin.progresslbl.Caption = "Export Requests Complete"

    IE.Quit
    Set IE = Nothing
    Exit Sub
Restore:
    RestoreOutlook
    MsgBox ("Issue with Export code")
End Sub


Sub WaitforEmails(Optional currentcount As Integer = 0)

////As soon as the code reaches this point the item dic variable is now a type Object again and has no values

    Dim item As Object, BatchID As String, k As Object

    For Each item In Items
        With item
            If .Subject = "Product Updates: Product Export" Then
                'Instr check for batch id (ie dic key) then whatever dic value it matches replace batch id in dic with download link
                For Each k In itemdic.keys
                    If InStr(1, .HTMLBody, k, vbTextCompare) > 0 Then
                        'Store the download link in place of the batch id
                        itemdic(k) = Mid(.HTMLBody, InStr(1, .HTMLBody, "a href=") + 8, (InStrRev(.HTMLBody, ">here") - 2) - (InStr(1, .HTMLBody, "a href=") + 8))
                        Exit For
                    End If
                Next
                currentcount = currentcount + 1
                If currentcount = (suppliercount - errs) Then Exit For 'we have all of the emails
            End If
        End With
    Next
    If Not currentcount = (suppliercount - errs) Then Application.OnTime Now + TimeValue("00:01:00"), "WaitforEmails(currentcount)"
    While Not currentcount = (suppliercount - errs)
        DoEvents
    Wend
    Exit Sub
Restore:
    RestoreOutlook
    MsgBox ("Issue with WaitforEmail code")
End Sub

'When moving to sub 2 itemdic now reverts back to showing as type Object in Watch window

Sub 2()
    'Work with items in dictionary
    'Application or Object-defined Error I believe?
    'Some error
End Sub

我的问题:

有没有一种方法可以使后期绑定的字典变量跨子(在Module1中)保持其类型(及其内容/值),而不必添加引用?

含咖啡因的迈克

哇,所以我感觉真是个白痴。这个问题一直以来一直困扰着我,至少部分是这样。我认为这个问题有两个方面:

  1. 该项目需要“清理”(导出模块和表格并将其导入到新项目中)
    • 这照顾itemdic到到达WaitforEmails子时没有它的值但是,我确实注意到,当我到达第一个子句的末尾时,它itemdic具有正确的类型和值。我意识到的是,当它返回到用户窗体的代码时,它在监视窗口中(暂时)显示为没有值,并且再次成为对象类型,这很奇怪,但是我想这是因为每个窗口都在当前模块正在主动执行有意义的代码的范围。一旦WaitforEmails从用户窗体调用了子程序,并且代码正在通过该子程序itemdic的行,则正确显示了带有值和类型为Dictionary的子程序
  2. key我用来遍历字典中各项变量被声明为错误,应将其声明Variant为非Object(doh!)。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

当我创建一个子对象时,如何获取父对象

来自分类Dev

猫鼬在子对象中创建一个子对象

来自分类Dev

访问VBA中另一个子对象内的对象字段

来自分类Dev

Java需要一个子对象来更改父对象中的数据

来自分类Dev

显示前5个子元素,并在单击下一个按钮时将其隐藏,并使用jquery显示下5个

来自分类Dev

postgres使用CTE插入,并在下一个子查询中使用插入的id

来自分类Dev

创建一个子集对象,该子对象仅包含现有对象的某些属性

来自分类Dev

从我的父对象 C# WPF 中的对象列表在 datagrid 中创建一个子行/子行

来自分类Dev

从另一个子执行子

来自分类Dev

Android Firebase,只需获取一个子对象的数据

来自分类Dev

如何在GSON中将多个字段解析为一个子对象?

来自分类Dev

Qt findChildren()函数仅返回第一个子对象

来自分类Dev

获取javascript中innerHTML为动态值的下一个子元素

来自分类Dev

解析XML尝试获取下一个子节点

来自分类Dev

仅解析xml文件中的下一个子级别类别

来自分类Dev

在单个周期中引用下一个子列表

来自分类Dev

单击事件上的下一个子菜单时如何关闭上一个打开的子菜单

来自分类Dev

OpenMP中一个模块内的私有和公共变量以及一个子例程

来自分类Dev

选择一个子查询

来自分类Dev

Unity C#-for循环:从一个对象移至下一个(运动)-卡在第一个对象上

来自分类Dev

让Devise在注册时创建一个子域

来自分类Dev

从子对象克隆旋转并将其分配给THREE.JS中另一父对象的另一个子对象

来自分类Dev

MvvmCross:MvxAdapter GetBindableView方法在第一个子对象上无限期循环

来自分类Dev

单击另一个子菜单时如何关闭一个子菜单

来自分类Dev

如何在SQl服务器中显示父下一个子行

来自分类Dev

使用实体框架,我只想包含第一个子对象,而不要包括child(sub的sub)的子对象

来自分类Dev

如何基于另一个zip的内容创建一个子zip?

来自分类Dev

ARKit 保存对象位置并在任何下一个会话中查看它

来自分类Dev

具有池/队列的Python多个子进程在一个完成时立即恢复输出并在队列中启动下一个作业

Related 相关文章

  1. 1

    当我创建一个子对象时,如何获取父对象

  2. 2

    猫鼬在子对象中创建一个子对象

  3. 3

    访问VBA中另一个子对象内的对象字段

  4. 4

    Java需要一个子对象来更改父对象中的数据

  5. 5

    显示前5个子元素,并在单击下一个按钮时将其隐藏,并使用jquery显示下5个

  6. 6

    postgres使用CTE插入,并在下一个子查询中使用插入的id

  7. 7

    创建一个子集对象,该子对象仅包含现有对象的某些属性

  8. 8

    从我的父对象 C# WPF 中的对象列表在 datagrid 中创建一个子行/子行

  9. 9

    从另一个子执行子

  10. 10

    Android Firebase,只需获取一个子对象的数据

  11. 11

    如何在GSON中将多个字段解析为一个子对象?

  12. 12

    Qt findChildren()函数仅返回第一个子对象

  13. 13

    获取javascript中innerHTML为动态值的下一个子元素

  14. 14

    解析XML尝试获取下一个子节点

  15. 15

    仅解析xml文件中的下一个子级别类别

  16. 16

    在单个周期中引用下一个子列表

  17. 17

    单击事件上的下一个子菜单时如何关闭上一个打开的子菜单

  18. 18

    OpenMP中一个模块内的私有和公共变量以及一个子例程

  19. 19

    选择一个子查询

  20. 20

    Unity C#-for循环:从一个对象移至下一个(运动)-卡在第一个对象上

  21. 21

    让Devise在注册时创建一个子域

  22. 22

    从子对象克隆旋转并将其分配给THREE.JS中另一父对象的另一个子对象

  23. 23

    MvvmCross:MvxAdapter GetBindableView方法在第一个子对象上无限期循环

  24. 24

    单击另一个子菜单时如何关闭一个子菜单

  25. 25

    如何在SQl服务器中显示父下一个子行

  26. 26

    使用实体框架,我只想包含第一个子对象,而不要包括child(sub的sub)的子对象

  27. 27

    如何基于另一个zip的内容创建一个子zip?

  28. 28

    ARKit 保存对象位置并在任何下一个会话中查看它

  29. 29

    具有池/队列的Python多个子进程在一个完成时立即恢复输出并在队列中启动下一个作业

热门标签

归档