1、打开office excel 2003,依次点击菜单栏:【工具】--->【宏】--->【Visual Basic 编辑器】。
2、打开Visual Basic 编辑窗口如下图。
3、下面,我们使用excel自动打开百度代码如下:Sub Main() Dim ie As Object Set ie = CreateObject("InternetExplorer.Application") ie.Visible = True ie.Navigate "http://www.baidu.com"End Sub点击运行按钮,自动打开百度首页
4、然后在百度搜索框内填写搜索的内容“你好,百度”,代码如下:ie.Document.getElementById("kw").value="hellowworld" 。其中kw为百度搜索框的ID,至于如何获得网页中元素的ID号,请自己百度一下。
5、最后我们得到百度的【百度一下】按钮的ID号,并点击按钮:ie.Document.getElementById("su").click,运行后结果如下图:
6、整段代码如下:Sub Main()//主程序入口 Dim ie As Object//定义的浏览器变量 Set ie = CreateObject("InternetExplorer.Application")//创建浏览器变量 ie.Visible = True//显示浏览器 ie.Navigate "http://www.baidu.com"//打开百度首页 While ie.ReadyState <> 4 Or ie.Busy = True//检测浏览器是否完全打开 DoEvents//假如浏览器还没有完全打开,则等待 Wend ie.Document.getElementById("kw").Value = "hellow world"//在浏览器//中填写搜索内容:“hellow world” ie.Document.getElementById("su").Click//点击搜索按钮End Sub