个人随笔和一些网文摘录
http://www.dlxp.cn

VBS获取外网IP并复制到剪切板

想写一段代码真的不容易,先不说写不来吧,还不会写.于是网络上一顿狂搜,都不太完美,于是有了现在的文章和现在的代码.所以现丑了.请大家指正

'以下代码需编译成EXE运行才OK,不知为何,大神可以提一下意见
set oDOM = WScript.GetObject("http://city.ip138.com/ip2city.asp")
flag=0
for i=1 to 10
   if oDOM.readyState = "complete" then 
      flag=1
      exit for
   end if
   WScript.sleep 500
next
if flag=0 then
    WScript.Echo "timeout ..."
    wscript.quit
end if
s=oDOM.documentElement.innerText
Set Re = New RegExp
Re.Pattern="(\d+)\.(\d+)\.(\d+)\.(\d+)"
for each r in Re.Execute(s)
     res=r
     exit for
Next
WScript.Echo "IPAddress: " & res

'设置剪切板的内容
Dim Form, TextBox
Set Form = CreateObject("Forms.Form.1")
Set TextBox = Form.Controls.Add("Forms.TextBox.1").Object
TextBox.MultiLine = True
TextBox.Text = res
TextBox.SelStart = 0
TextBox.SelLength = TextBox.TextLength
TextBox.Copy
MyVar = MsgBox (res,64,"请Ctrl+V粘贴IP地址")

以下是BAT代码:

@set @n=0; /* & echo off&cscript -nologo -e:jscript "%~f0"|clip&echo;外网IP已写入剪贴板&ping -n 2 0 >nul&exit /b& rem */
var url = "http://city.ip138.com/ip2city.asp";
http = new ActiveXObject("Msxml2.XMLHTTP");
stream = new ActiveXObject("ADODB.Stream");
http.open("GET", url, false);
http.send(null);
stream.Type=1;
stream.Mode=3;
stream.Open();
stream.Write(http.responseBody);
stream.Position=0;
stream.Type=2;
stream.Charset="gb2312";
var m = stream.ReadText.match(/\[(\d+\.\d+\.\d+\.\d+)\]/);
stream.Close();
WSH.Echo(m[1]);

VBS访问剪贴板的几种方法
最常见的是InternetExplorer.Application对象,网上一搜一大把。

Set IE = CreateObject("InternetExplorer.Application")
IE.Navigate("about:blank")
Set clipboard = IE.document.parentWindow.clipboardData
'SetData设置剪切板的内容
clipboard.SetData "text", "忘记了,喜欢一个人的感觉"
'GetData获取剪切板的内容
WScript.Echo clipboard.GetData("text")
IE.Quit

实践证明,网上一搜一大把的代码一般不是好代码。SetData方法其实是和IE浏览器的设置有关的。
IE8的默认设置是Prompt,所以运行上面那个脚本的时候会弹出一个对话框,如果这是成Disable,那么这个脚本就无法设置剪贴板内容了(获取不受影响)。
这种没有保证的代码还是少用一些的好,在Windows 7下可以用clip.exe来设置剪贴板的内容,获取的话还是用IE就行了。

Dim WshShell
set WshShell = CreateObject("wscript.Shell")
str = "忘记了,喜欢一个人的感觉"
WshShell.Run "cmd.exe /c echo " & str & " | clip",0,False

用Word.Application也可以设置和获取剪贴板内容

'设置剪切板的内容
Dim Word
Set Word = CreateObject("Word.Application")
Word.Documents.Add
Word.Selection.Text = "忘记了,喜欢一个人的感觉"
Word.Selection.Copy
Word.Quit False
'获取剪切板的内容
Dim Word
Set Word = CreateObject("Word.Application")
Word.Documents.Add
Word.Selection.PasteAndFormat(wdFormatPlainText)
Word.Selection.WholeStory
str = Word.Selection.Text
Word.Quit False
WScript.Echo str

最神奇的是用Microsoft Forms 2.0 Object Library。

'设置剪切板的内容
Dim Form, TextBox
Set Form = CreateObject("Forms.Form.1")
Set TextBox = Form.Controls.Add("Forms.TextBox.1").Object
TextBox.MultiLine = True
TextBox.Text = "忘记了,喜欢一个人的感觉"
TextBox.SelStart = 0
TextBox.SelLength = TextBox.TextLength
TextBox.Copy
'获取剪切板的内容
Dim Form, TextBox
Set Form = CreateObject("Forms.Form.1")
Set TextBox = Form.Controls.Add("Forms.TextBox.1").Object
TextBox.MultiLine = True
If TextBox.CanPaste Then
    TextBox.Paste
    WScript.Echo TextBox.Text
End If

参考资料:
http://demon.tw/programming/vbs-clipboard.html
http://www.bathome.net/thread-8329-1-1.html
http://www.bathome.net/thread-36391-1-1.html
http://www.bathome.net/thread-546-1-9.html

未经允许不得转载:Leejoa's 生活随笔 » VBS获取外网IP并复制到剪切板
分享到: 更多 (0)

评论 抢沙发

评论前必须登录!