类模块到对象,标准模块

时间:2019-07-16 14:47:52

标签: excel vba

我希望有人能真正指导我进行该程序。我正在从事与以下代码相似的工作。

问题是,我不知道这意味着什么, (代码必须在对象中而不是模块中。)

如何在标准模块中调用类模块?或代码如何工作?我的意思是,我确实了解该功能,但是如何执行以下操作?谢谢你们。对我的无知表示歉意。

        Option Explicit

Public WithEvents IE1 As InternetExplorer
Public IE2 As InternetExplorer


Private Sub Automation()

Dim objElement As Object
Dim objButton As Object
Dim objLink As Object
Dim objLink2 As Object
Dim mytextfield1 As Object
Dim mytextfield2 As Object

Set IE1 = CreateObject("internetexplorer.application")
Set IE2 = Nothing

With IE1
.navigate "***website url***"
.Visible = True  'allows for viewing the web page
While .Busy Or .readyState <> 4: DoEvents: Wend
    Set mytextfield1 = .document.all.Item("txtUserName")
    mytextfield1.Value = "***username***"
    Set mytextfield2 = .document.all.Item("txtPassword")
    mytextfield2.Value = "***password***"
    While .Busy Or .readyState <> 4: DoEvents: Wend
    IE1.document.getElementById("Submit").Click
End With

    ' loop until the page finishes loading
Do While IE1.Busy: Loop

 'Opens another link
 With IE1
 While .Busy Or .readyState <> 4: DoEvents: Wend
 IE1.navigate "***url***"
 End With

 'Opens the menu
 With IE1
 While .Busy Or .readyState <> 4: DoEvents: Wend
 IE1.navigate "***URL in frame***", ["left"]
 End With

 'Opens the Profile search menu
 With IE1
 While .Busy Or .readyState <> 4: DoEvents: Wend
 IE1.navigate "***url in another frame***", , ["mainParent"]
 End With

 'Copies the ID# from the Excel worksheet and pastes it to search in site to search
 With IE1
 While .Busy Or .readyState <> 4: DoEvents: Wend
 Application.Wait (Now + TimeValue("0:00:03"))
 Set objElement = .document.frames("mainParent").document.frames("main1").document.forms("AgentIdentificationNumberSearch").document.getElementById("IDN")
 objElement.Value = Sheets("Appointments").Range("a2").Value
 Set objButton = .document.frames("mainParent").document.frames("main1").document.forms("AgentIdentificationNumberSearch").document.getElementById("Search")
 objButton.Click
 End With

 'Clicks "View Profile Summary" and opens new window
 With IE1
 While .Busy Or .readyState <> 4: DoEvents: Wend
 Application.Wait (Now + TimeValue("0:00:03"))
 Set objLink = IE1.document.frames("mainParent").document.forms("AgentProfileList").document.getElementById("grdProfile_r_0").document.getElementsByTagName("a")(1)
 objLink.Click
 End With

 'Ensure new window has been created (if the window does not generate, this will go on forever)
 Do While IE2 Is Nothing: Loop
 Do While IE2.Busy: Loop

'Click first link in the new window
 With IE2
 While .Busy Or .readyState <> 4: DoEvents: Wend
 Application.Wait (Now + TimeValue("0:00:03"))
 Set objLink2 = IE2.document.forms("form1").document.getElementsByTagName("a")(2)
 objLink2.Click
 End With

 Set IE2 = Nothing

End Sub

________________________

Private Sub IE1_NewWindow2(ppDisp As Object, Cancel As Boolean)
    Set IE2 = New InternetExplorer
    Set ppDisp = IE2.Application
    Debug.Print "NewWindow2"
 End Sub

1 个答案:

答案 0 :(得分:0)

  

您如何在标准模块中调用类模块?

您一直都在做,甚至没有意识到。 Range是一个类,Workbook也是一个类,WorksheetApplication也是如此。唯一的区别是您不是创建这些对象的人。

因此将代码移至新的 class模块,并执行过程Public,以使该类的默认接口公开该成员。这可能是重命名该过程的机会,使其看起来像一个动词而不是一个名词。保留名词以表示模块名称和属性,并使用动词表示方法/过程:它们;类/对象

保留这些字段Private,它们是与Public无关的实施细节:

Public WithEvents IE1 As InternetExplorer
Public IE2 As InternetExplorer

您将要从标准模块创建该模块的New 实例-假设您为该类保留了默认的Class1名称,并重命名了RunAutomation的过程中,您的标准模块可能如下所示:

Public Sub DoSomething()
    With New Class1
        .RunAutomation
    End With
End Sub