---
title: "对象'HWND'的方法'IWebBrowser1'失败’"
author: "piperzero"
date: "2012-12-22T12:38:00Z"
tags: []
slug: "hwnd-iwebbrowser1"
draft: false
lastmod: "2013-01-07T09:58:24Z"
id: 1764523
url: "https://piperzero.iteye.com/blog/1764523"
---

不能直接使用。解决方法如下：  
一、新建一个模块，复制下面代码：  
Option Explicit  
Private Const GW\_HWNDNEXT = 2  
Private Const GW\_CHILD = 5  
Private Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd 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

Function GetWebBrowserHwnd(hwndBrowserContainer As Long) As Long  
Dim RetVal As Long  
Dim hwndPeer As Long  
Dim ClassString As String * 256  
hwndPeer = GetWindow(hwndBrowserContainer, GW\_CHILD)  
Do While hwndPeer &lt;&gt; 0  
RetVal = GetClassName(hwndPeer, ClassString, 256)  
If InStr(ClassString, "Shell Embedding") &lt;&gt; 0 Then  
GetWebBrowserHwnd = hwndPeer  
Exit Do  
End If  
hwndPeer = GetWindow(hwndPeer, GW\_HWNDNEXT)  
Loop  
End Function  
二、在窗体中获取webbrowser1的hwnd的方法是：  
dim i as long  
i=GetWebBrowserHwnd(Me.hwnd)