VBA,VB 自动登录网站

来源:互联网 发布:淘宝蚂蚁花呗 编辑:程序博客网 时间:2024/04/28 08:47

很多时候大家都需要登录网站, 查询一下信息。可不可以在office中登录呢?可以,用VBA即可。

需要添加到引用

  Microsoft Internet Controls

  Microsoft HTML Object Library

 

Sub loginRenRen()

    Dim ie As InternetExplorer
    Dim doc As HTMLDocument
   
    Set ie = New InternetExplorer
   
   
    ie.Navigate "http://www.renren.com/"
    ie.Visible = True

    'http://www.renren.com/Home.do 个人主页
    Do
      DoEvents
    Loop Until ie.ReadyState = 4 '在网页没有完全下载之前,VB 转让控制权
   
    If ie.LocationURL = "http://www.renren.com/Home.do" Then Exit Sub '如果已经登录,退出程序


   
    Set doc = ie.Document
   
    doc.getElementById("email").Value = "用户名"
    doc.getElementById("password").Value = "密码"
    doc.getElementById("login").Click
   
    doc.Close
    Set doc = Nothing
    'ie.Quit
    Set ie = Nothing
   
   
End Sub