乔山办公网我们一直在努力
您的位置:乔山办公网 > excel表格制作 > <em>Excel</em> 求教如何用<em>VBA</em>打开网页

<em>Excel</em> 求教如何用<em>VBA</em>打开网页

作者:乔山办公网日期:

返回目录:excel表格制作


'准备工作:1.用IE打开百度
          2.调用函数GetIE

'代码搜索标题包含百度的IE窗口e799bee5baa6e997aee7ad94e4b893e5b19e364,然后控制打开hao123,最后保存为c:\myhtml.txt
Option Explicit
  '
  '   工程要引用  "Microsoft   HTML   Object   Library"
  '
    
Private Type UUID
      Data1   As Long
      Data2   As Integer
      Data3   As Integer
      Data4(0 To 7)       As Byte
End Type
Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hWnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Private Declare Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hWnd As Long) As Long
Private Declare Function GetClassName Lib "user32" _
      Alias "GetClassNameA" ( _
      ByVal hWnd As Long, _
      ByVal lpClassName As String, _
      ByVal nMaxCount As Long) As Long
Private Declare Function EnumWindows Lib "user32" (ByVal lpEnumFunc As Long, ByVal lParam As Long) As Boolean
Private Declare Function EnumChildWindows Lib "user32" (ByVal hWndParent As Long, ByVal lpEnumFunc As Long, lParam As Long) As Long
  
Private Declare Function RegisterWindowMessage Lib "user32" _
      Alias "RegisterWindowMessageA" ( _
      ByVal lpString As String) As Long
  
Private Declare Function SendMessageTimeout Lib "user32" _
      Alias "SendMessageTimeoutA" ( _
      ByVal hWnd As Long, _
      ByVal msg As Long, _
      ByVal wParam As Long, _
      lParam As Any, _
      ByVal fuFlags As Long, _
      ByVal uTimeout As Long, _
      lpdwResult As Long) As Long
              
Private Const SMTO_ABORTIFHUNG = &H2
  
Private Declare Function ObjectFromLresult Lib "oleacc" ( _
      ByVal lResult As Long, _
      riid As UUID, _
      ByVal wParam As Long, _
      ppvObject As Any) As Long
Dim IEhwnd As Long
Dim IEserver As Long
'
'   IEDOMFromhWnd
'
'   Returns   the   IHTMLDocument   interface   from   a   WebBrowser   window
'
'   hWnd   -   Window   handle   of   the   control
'
Function IEDOMFromhWnd() As IHTMLDocument
Dim IID_IHTMLDocument     As UUID
Dim hWnd   As Long
Dim lRes   As Long
Dim lMsg   As Long
Dim hr     As Long
    '   Find   a   child   IE   server   window
    EnumWindows AddressOf EnumWindowProc, ByVal 0
    If IEhwnd Then EnumChildWindows IEhwnd, AddressOf EnumChildProc, ByVal 0
    If IEserver Then hWnd = IEserver Else Exit Function
    
    '   Register   the   message
    lMsg = RegisterWindowMessage("WM_HTML_GETOBJECT")
    '   Get   the   object   pointer
    Call SendMessageTimeout(hWnd, lMsg, 0, 0, _
                    SMTO_ABORTIFHUNG, 1000, lRes)
    If lRes Then
          '   Initialize   the   interface   ID
          With IID_IHTMLDocument
                .Data1 = &H626FC520
                .Data2 = &HA41E
                .Data3 = &H11CF
                .Data4(0) = &HA7
                .Data4(1) = &H31
                .Data4(2) = &H0
                .Data4(3) = &HA0
                .Data4(4) = &HC9
                .Data4(5) = &H8
                .Data4(6) = &H26
                .Data4(7) = &H37
          End With
          '   Get   the   object   from   lRes
          hr = ObjectFromLresult(lRes, IID_IHTMLDocument, 0, IEDOMFromhWnd)
    End If
End Function
  
Private Function IsIEServerWindow(ByVal hWnd As Long) As Boolean
Dim lRes     As Long
Dim sClassName     As String
    sClassName = GetClsName(hWnd)
    IsIEServerWindow = StrComp(sClassName, "Internet Explorer_Server", vbTextCompare) = 0
End Function
'返回窗口类名
Public Function GetClsName(ByVal hWnd As Long) As String
Dim lRes     As Long
Dim sClassName     As String
    sClassName = String$(200, 0)
    lRes = GetClassName(hWnd, sClassName, Len(sClassName))
    GetClsName = Left$(sClassName, lRes)
End Function
'返回窗口标题
Public Function GetWinTitle(ByVal lhWnd As Long) As String
    Dim MyStr As String
    MyStr = String(200, Chr$(0))
    GetWindowText lhWnd, MyStr, 200
    GetWinTitle = Left(MyStr, InStr(MyStr, Chr$(0)) - 1)
End Function
Function EnumWindowProc(ByVal hWnd As Long, ByVal lParam As Long) As Long
Dim sIEtitle As String
    sIEtitle = GetWinTitle(hWnd)
    If InStr(1, sIEtitle, "百度") Then  '搜索标题包含baidu的窗口
        IEhwnd = hWnd
    Else
        EnumWindowProc = 1
    End If
End Function
Function EnumChildProc(ByVal hWnd As Long, lParam As Long) As Long
    If IsIEServerWindow(hWnd) Then
        IEserver = hWnd
    Else
        EnumChildProc = 1
    End If
End Function
Function GetIE() As Long
    Dim Doc As IHTMLDocument2
    Dim s As String
    Set Doc = IEDOMFromhWnd
    If Not Doc Is Nothing Then
        Doc.url = "http://" '打开网页
        Do Until Doc.readyState = "complete"
            DoEvents
        Loop
        s = Doc.body.innerHTML
        Open "c:\myhtml.txt" For Output As #1
        Print #1, s
        Close
    End If
End Function


http://club.excelhome.net/thread-507935-1-1.html
帮你百度了
Range("b4").Hyperlinks(1).Follow

Excel 请问如何用VBA打开网页,给段代码,并解释
代码如下:
Public Sub GoToOkexcel()
'打开网页
Dim myIE As InternetExplorer
Set myIE = New InternetExplorer
With myIE
.Visible = True
.Navigate "http://www.cnrmmmmmmmmmmwwwwiiiiiib.net/bbs/"
End With
Set myIE = Nothing
End Sub

运行时可能会出现“数据未定义”错误。

解决办法

在VBA编辑器窗口依次点击

工具==引用

勾选Microsoft Internet Controls

确定,再运行,OK!

相关阅读

  • 用<em>Excel</em> <em>vba</em>调用Outloo

  • 乔山办公网excel表格制作
  • 我是用office2010的,打开outlook后点文件-选项-信任中心-信任中心设置-编程访问-选“从不向我发送……”。选这个有一定的危险性,容易被病毒使用你的邮箱。Excel VBA实现Outlook和Foxmail群
关键词不能为空
极力推荐

聚合标签

ppt怎么做_excel表格制作_office365_word文档_365办公网