如何在自动运行宏后关闭xls表

时间:2014-01-06 11:49:39

标签: macros xls

我有以下宏代码,这将自动运行宏并登录到提到的网站。但xls表仍然打开..

-----------------------------------------------------------------
Sub Auto_open()

login

End Sub


Sub login()
Dim IntExpl As Object
Set IntExpl = CreateObject("InternetExplorer.Application")
Dim dd As Object
Dim dd1 As Object
Dim dd2 As Object
Dim dd3 As Object

With IntExpl
      .navigate "........."
      .Visible = True
     ' If (.Document.getElementById("LoginUsername").exist) Then
       Do Until IntExpl.ReadyState = 4
       Loop
       Set dd = .Document.getElementById("LoginUsername")
       dd.Value = "AAAAA"
       dd.Click
       Set dd1 = .Document.getElementById("LoginPassword")
       dd1.Value = "AAAAAA"
       dd1.Click
       Set dd2 = .Document.getElementById("loginBtn")
       dd2.Click


End With
End Sub
-------------------------------------------------------------------------------

我想在运行整个宏之后关闭xls文件。

1 个答案:

答案 0 :(得分:0)

您可以定义工作簿,然后在宏完成后关闭它。

Sub Auto_open()

Dim wb as Workbook
set wb = ActiveWorkbook
login
wb.close
End Sub


Sub login()
Dim IntExpl As Object
Set IntExpl = CreateObject("InternetExplorer.Application")
Dim dd As Object
Dim dd1 As Object
Dim dd2 As Object
Dim dd3 As Object

With IntExpl
      .navigate "........."
      .Visible = True
     ' If (.Document.getElementById("LoginUsername").exist) Then
       Do Until IntExpl.ReadyState = 4
       Loop
       Set dd = .Document.getElementById("LoginUsername")
       dd.Value = "AAAAA"
       dd.Click
       Set dd1 = .Document.getElementById("LoginPassword")
       dd1.Value = "AAAAAA"
       dd1.Click
       Set dd2 = .Document.getElementById("loginBtn")
       dd2.Click


End With
End Sub