主题:[讨论]代码请教,如何转为VBA for EXCEL应用
我有一个网址,日常常用到其中的单号查询,举例:999 77192323
在这个页面上进行配货查询,因为网页操作实在不便.
所以想转成EXCEL应用, VBA for EXCEL
我只懂VBA,粗略知道CreateObject("MSXML2.XMLHTTP")的使用.
以下代码不懂怎样改为网址获得内容,有没有高手指点一二?
[url]http://www.cargoserv.com/webtracking/ca/tracking.asp[/url]
[color=ff00ff]以下为网页源代码[/color]
<SCRIPT language = "javascript" src = "../../js/tracking_func.js"></SCRIPT>
<html>
<head>
<title>Cargo Shipment Information (Chinese)</title>
</head>
<body background="images/bg3.jpg">
<a href="etracking.asp" target="_top">
<p align="center"><img src="images/line1.gif" border="0" width="73" height="33"></a>
<img src="images/line2.gif" border="0" width="64" height="33"> <img src="images/line.gif" width="307" height="33"></p>
<div align="center">
<center>
<table border="0" width="80">
<tr>
<td></td>
<td valign="top"><img src="images/z1.jpg"><br>
<br>
目 前 中 国 国 际 航 空
公 司 使 用 的 货 运 计
算 机 系 统 已 覆 盖 北
京 市 货 、 机 场 货 运
、 上 海 、 广 州 、 大
连 营 业 部 、 天 津 分
公 司 、 以 及 纽 约 、
东 京 、 新 加 坡 、 法
兰 克 福 、 罗 马 等 办
事 处 , 随 着 外 站 货
运 计 算 机 系 统 的 使
用 , 货 运 系 统 信 息
越 来 越 完 善 , 用 户
可 通 过 以 下 方 式 对
运 单 进 行 查 询 , 掌
握 您 的 货 物 去 向 。
<p><font color="#c04000"><b>在 查 询 时 请
输 入 您 的 运 单 号:</b></font></p>
</td>
</tr>
<tr>
<td colspan="2">
<form method="POST"
name="FrontPage_Form1"
align="centre"
onsubmit="return (FrontPage_Form1_Validator(this) &&
disableButton(this))">
<table border="1" bgcolor="#6da2e0" cellspacing="0" cellpadding="4">
<tr>
<th colspan="2"><font size="2">两字代码</font></th>
<th colspan="2"><font size="2">运单号前缀</font></th>
<th colspan="2"><font size="2">运单号</font></th>
<th nowrap><font size="2">确定按钮</font></th>
</tr>
<tr>
<td align="center"><font color="#800040" size="3"><em>Carrier:</em></font></td>
<td align="center"><font color="#0000FF" size="3" face="Times New Roman"><strong>CA</strong></font></td>
<td align="center"><font color="#800040" size="3"><em>Prefix:</em></font></td>
<td align="center"><font color="#0000FF" size="3" face="Times New Roman"><input type="text" name="Pfx" maxlength="3" size="3" value=""></font></td>
<td align="center" nowrap><font color="#800040" size="3"><em>Shipment:</em></font></td>
<td align="center" nowrap>
<input type="hidden" name="Carrier" value="CA">
<p align="center"><input type="text" size="12" name="Shipment" maxlength="8" value="" align="centre" language="JavaScript" onchange="checkValidNumber()"></p>
</td>
<td align="center">
<table border="0" cellspacing="1">
<tr>
<td bgcolor="#C0C0C0"><input type="Submit" value="Track">
</tr>
</table>
</td>
</tr>
</table>
</form>
<hr>
<br>
<img src="../../images/poweredby.png">
</td>
</tr>
</table>
</center>
</div>
</html>
<SCRIPT LANGUAGE="JavaScript">
<!--
function checkValidNumber()
{
var number
number = document.forms[0].Shipment.value
if (number.length < 8)
{
alert('The shipment number entered is invalid')
}
}
//-->
</SCRIPT>
[color=ff00ff]以下为 ../../js/tracking_func.js 文件内容[/color]
function changeButton(s) {
// alert("Within JS file changeButton");
s.value = "Please Wait";
return true;
}
function waitButton(s) {
// alert("Within JS file waitButton");
if (condition==true){
s.value="ClickToGo";
s.disabled=false;
}else{
s.value="PleaseWait";
s.disabled=true;
}
}
function disableButton(theform) {
// alert ("Within JS file disableButton");
if (document.all || document.getElementById) {
for (i = 0; i < theform.length; i++) {
var tempobj = theform.elements[i];
if (tempobj.type.toLowerCase() == "submit" || tempobj.type.toLowerCase() == "reset")
tempobj.disabled = true;
}
return true;
}
}
function FrontPage_Form1_Validator_old(theForm)
{
// alert ("Within JS file FrontPage_Form1_Validator");
if (theForm.Shipment.value == "")
{
alert("Please enter a value for the \"AWB number\" field.");
theForm.Shipment.focus();
return (false);
}
if (theForm.Shipment.value.length < 8)
{
alert("Please enter at least 8 numbers in the \"AWB number\" field.");
theForm.Shipment.focus();
return (false);
}
if (theForm.Shipment.value.length > 8)
{
alert("Please enter at most 8 numbers in the \"AWB number\" field.");
theForm.Shipment.focus();
return (false);
}
var checkOK = "0123456789-.,";
var checkStr = theForm.Shipment.value;
var allValid = true;
var decPoints = 0;
var allNum = "";
for (i = 0; i < checkStr.length; i++)
{
ch = checkStr.charAt(i);
for (j = 0; j < checkOK.length; j++)
if (ch == checkOK.charAt(j))
break;
if (j == checkOK.length)
{
allValid = false;
break;
}
if (ch == ".")
{
allNum += ".";
decPoints++;
}
else if (ch != ",")
allNum += ch;
}
if (!allValid)
{
alert("Please enter only numbers in the \"AWB number\" field.");
theForm.Shipment.focus();
return (false);
}
if (decPoints > 1)
{
alert("Please enter a valid number in the \"Shipment\" field.");
theForm.Shipment.focus();
return (false);
}
return (true);
}
function FrontPage_Form1_Validator(theForm)
{
if (theForm.Pfx.value == "")
{
alert("Please enter a value for the \"Prefix\" field.");
theForm.Pfx.focus();
return (false);
}
if (theForm.Pfx.value.length < 3)
{
alert("Please enter at least 3 numbers in the \"Prefix\" field.");
theForm.Pfx.focus();
return (false);
}
if (theForm.Pfx.value.length > 3)
{
alert("Please enter at most 3 numbers in the \"Prefix\" field.");
theForm.Pfx.focus();
return (false);
}
var checkOK = "0123456789-.,";
var checkStr = theForm.Pfx.value;
var allValid = true;
var decPoints = 0;
var allNum = "";
for (i = 0; i < checkStr.length; i++)
{
ch = checkStr.charAt(i);
for (j = 0; j < checkOK.length; j++)
if (ch == checkOK.charAt(j))
break;
if (j == checkOK.length)
{
allValid = false;
break;
}
if (ch == ".")
{
allNum += ".";
decPoints++;
}
else if (ch != ",")
allNum += ch;
}
if (!allValid)
{
alert("Please enter only digit digits in the \"Prefix\" field.");
theForm.Pfx.focus();
return (false);
}
if (decPoints > 1)
{
alert("Please enter a valid number in the \"Pfx\" field.");
theForm.Pfx.focus();
return (false);
}
if (theForm.Shipment.value == "")
{
alert("Please enter a value for the \"AWB number\" field.");
theForm.Shipment.focus();
return (false);
}
if (theForm.Shipment.value.length < 8)
{
alert("Please enter at least 8 digits in the \"AWB number\" field.");
theForm.Shipment.focus();
return (false);
}
if (theForm.Shipment.value.length > 8)
{
alert("Please enter at most 8 digits in the \"AWB number\" field.");
theForm.Shipment.focus();
return (false);
}
var checkOK = "0123456789-.,";
var checkStr = theForm.Shipment.value;
var allValid = true;
var decPoints = 0;
var allNum = "";
for (i = 0; i < checkStr.length; i++)
{
ch = checkStr.charAt(i);
for (j = 0; j < checkOK.length; j++)
if (ch == checkOK.charAt(j))
break;
if (j == checkOK.length)
{
allValid = false;
break;
}
if (ch == ".")
{
allNum += ".";
decPoints++;
}
else if (ch != ",")
allNum += ch;
}
if (!allValid)
{
alert("Please enter only digits in the \"AWB number\" field.");
theForm.Shipment.focus();
return (false);
}
if (decPoints > 1)
{
alert("Please enter a valid number in the \"Shipment\" field.");
theForm.Shipment.focus();
return (false);
}
return (true);
}
在这个页面上进行配货查询,因为网页操作实在不便.
所以想转成EXCEL应用, VBA for EXCEL
我只懂VBA,粗略知道CreateObject("MSXML2.XMLHTTP")的使用.
以下代码不懂怎样改为网址获得内容,有没有高手指点一二?
[url]http://www.cargoserv.com/webtracking/ca/tracking.asp[/url]
[color=ff00ff]以下为网页源代码[/color]
<SCRIPT language = "javascript" src = "../../js/tracking_func.js"></SCRIPT>
<html>
<head>
<title>Cargo Shipment Information (Chinese)</title>
</head>
<body background="images/bg3.jpg">
<a href="etracking.asp" target="_top">
<p align="center"><img src="images/line1.gif" border="0" width="73" height="33"></a>
<img src="images/line2.gif" border="0" width="64" height="33"> <img src="images/line.gif" width="307" height="33"></p>
<div align="center">
<center>
<table border="0" width="80">
<tr>
<td></td>
<td valign="top"><img src="images/z1.jpg"><br>
<br>
目 前 中 国 国 际 航 空
公 司 使 用 的 货 运 计
算 机 系 统 已 覆 盖 北
京 市 货 、 机 场 货 运
、 上 海 、 广 州 、 大
连 营 业 部 、 天 津 分
公 司 、 以 及 纽 约 、
东 京 、 新 加 坡 、 法
兰 克 福 、 罗 马 等 办
事 处 , 随 着 外 站 货
运 计 算 机 系 统 的 使
用 , 货 运 系 统 信 息
越 来 越 完 善 , 用 户
可 通 过 以 下 方 式 对
运 单 进 行 查 询 , 掌
握 您 的 货 物 去 向 。
<p><font color="#c04000"><b>在 查 询 时 请
输 入 您 的 运 单 号:</b></font></p>
</td>
</tr>
<tr>
<td colspan="2">
<form method="POST"
name="FrontPage_Form1"
align="centre"
onsubmit="return (FrontPage_Form1_Validator(this) &&
disableButton(this))">
<table border="1" bgcolor="#6da2e0" cellspacing="0" cellpadding="4">
<tr>
<th colspan="2"><font size="2">两字代码</font></th>
<th colspan="2"><font size="2">运单号前缀</font></th>
<th colspan="2"><font size="2">运单号</font></th>
<th nowrap><font size="2">确定按钮</font></th>
</tr>
<tr>
<td align="center"><font color="#800040" size="3"><em>Carrier:</em></font></td>
<td align="center"><font color="#0000FF" size="3" face="Times New Roman"><strong>CA</strong></font></td>
<td align="center"><font color="#800040" size="3"><em>Prefix:</em></font></td>
<td align="center"><font color="#0000FF" size="3" face="Times New Roman"><input type="text" name="Pfx" maxlength="3" size="3" value=""></font></td>
<td align="center" nowrap><font color="#800040" size="3"><em>Shipment:</em></font></td>
<td align="center" nowrap>
<input type="hidden" name="Carrier" value="CA">
<p align="center"><input type="text" size="12" name="Shipment" maxlength="8" value="" align="centre" language="JavaScript" onchange="checkValidNumber()"></p>
</td>
<td align="center">
<table border="0" cellspacing="1">
<tr>
<td bgcolor="#C0C0C0"><input type="Submit" value="Track">
</tr>
</table>
</td>
</tr>
</table>
</form>
<hr>
<br>
<img src="../../images/poweredby.png">
</td>
</tr>
</table>
</center>
</div>
</html>
<SCRIPT LANGUAGE="JavaScript">
<!--
function checkValidNumber()
{
var number
number = document.forms[0].Shipment.value
if (number.length < 8)
{
alert('The shipment number entered is invalid')
}
}
//-->
</SCRIPT>
[color=ff00ff]以下为 ../../js/tracking_func.js 文件内容[/color]
function changeButton(s) {
// alert("Within JS file changeButton");
s.value = "Please Wait";
return true;
}
function waitButton(s) {
// alert("Within JS file waitButton");
if (condition==true){
s.value="ClickToGo";
s.disabled=false;
}else{
s.value="PleaseWait";
s.disabled=true;
}
}
function disableButton(theform) {
// alert ("Within JS file disableButton");
if (document.all || document.getElementById) {
for (i = 0; i < theform.length; i++) {
var tempobj = theform.elements[i];
if (tempobj.type.toLowerCase() == "submit" || tempobj.type.toLowerCase() == "reset")
tempobj.disabled = true;
}
return true;
}
}
function FrontPage_Form1_Validator_old(theForm)
{
// alert ("Within JS file FrontPage_Form1_Validator");
if (theForm.Shipment.value == "")
{
alert("Please enter a value for the \"AWB number\" field.");
theForm.Shipment.focus();
return (false);
}
if (theForm.Shipment.value.length < 8)
{
alert("Please enter at least 8 numbers in the \"AWB number\" field.");
theForm.Shipment.focus();
return (false);
}
if (theForm.Shipment.value.length > 8)
{
alert("Please enter at most 8 numbers in the \"AWB number\" field.");
theForm.Shipment.focus();
return (false);
}
var checkOK = "0123456789-.,";
var checkStr = theForm.Shipment.value;
var allValid = true;
var decPoints = 0;
var allNum = "";
for (i = 0; i < checkStr.length; i++)
{
ch = checkStr.charAt(i);
for (j = 0; j < checkOK.length; j++)
if (ch == checkOK.charAt(j))
break;
if (j == checkOK.length)
{
allValid = false;
break;
}
if (ch == ".")
{
allNum += ".";
decPoints++;
}
else if (ch != ",")
allNum += ch;
}
if (!allValid)
{
alert("Please enter only numbers in the \"AWB number\" field.");
theForm.Shipment.focus();
return (false);
}
if (decPoints > 1)
{
alert("Please enter a valid number in the \"Shipment\" field.");
theForm.Shipment.focus();
return (false);
}
return (true);
}
function FrontPage_Form1_Validator(theForm)
{
if (theForm.Pfx.value == "")
{
alert("Please enter a value for the \"Prefix\" field.");
theForm.Pfx.focus();
return (false);
}
if (theForm.Pfx.value.length < 3)
{
alert("Please enter at least 3 numbers in the \"Prefix\" field.");
theForm.Pfx.focus();
return (false);
}
if (theForm.Pfx.value.length > 3)
{
alert("Please enter at most 3 numbers in the \"Prefix\" field.");
theForm.Pfx.focus();
return (false);
}
var checkOK = "0123456789-.,";
var checkStr = theForm.Pfx.value;
var allValid = true;
var decPoints = 0;
var allNum = "";
for (i = 0; i < checkStr.length; i++)
{
ch = checkStr.charAt(i);
for (j = 0; j < checkOK.length; j++)
if (ch == checkOK.charAt(j))
break;
if (j == checkOK.length)
{
allValid = false;
break;
}
if (ch == ".")
{
allNum += ".";
decPoints++;
}
else if (ch != ",")
allNum += ch;
}
if (!allValid)
{
alert("Please enter only digit digits in the \"Prefix\" field.");
theForm.Pfx.focus();
return (false);
}
if (decPoints > 1)
{
alert("Please enter a valid number in the \"Pfx\" field.");
theForm.Pfx.focus();
return (false);
}
if (theForm.Shipment.value == "")
{
alert("Please enter a value for the \"AWB number\" field.");
theForm.Shipment.focus();
return (false);
}
if (theForm.Shipment.value.length < 8)
{
alert("Please enter at least 8 digits in the \"AWB number\" field.");
theForm.Shipment.focus();
return (false);
}
if (theForm.Shipment.value.length > 8)
{
alert("Please enter at most 8 digits in the \"AWB number\" field.");
theForm.Shipment.focus();
return (false);
}
var checkOK = "0123456789-.,";
var checkStr = theForm.Shipment.value;
var allValid = true;
var decPoints = 0;
var allNum = "";
for (i = 0; i < checkStr.length; i++)
{
ch = checkStr.charAt(i);
for (j = 0; j < checkOK.length; j++)
if (ch == checkOK.charAt(j))
break;
if (j == checkOK.length)
{
allValid = false;
break;
}
if (ch == ".")
{
allNum += ".";
decPoints++;
}
else if (ch != ",")
allNum += ch;
}
if (!allValid)
{
alert("Please enter only digits in the \"AWB number\" field.");
theForm.Shipment.focus();
return (false);
}
if (decPoints > 1)
{
alert("Please enter a valid number in the \"Shipment\" field.");
theForm.Shipment.focus();
return (false);
}
return (true);
}