using System;
using System.Data;
using System.Text;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Diagnostics;
using SHDocVw;
using mshtml;namespace SZH_Local_System.Prj_Obj.Trk_pocreation
{
public class TMSquery : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
private System.ComponentModel.Container components = null;public TMSquery(){ InitializeComponent();}
protected override void Dispose( bool disposing ) {
........
........}#region Windows Form Designer generated code
private void InitializeComponent(){
........
........}
#endregionprivate void button1_Click(object sender, System.EventArgs e){
mshtml.HTMLDocument orderquerydoc=Launch_orderquery("http://xxxxxx/xxxxx/order/OrderQueryFrame.htm");
if(orderquerydoc!=null) Operate_OrderQuery(orderquerydoc);
}private void Operate_OrderQuery(mshtml.HTMLDocument xdoc)
{
//这里出错了,xtMONo为http://xxxxxx/xxxxx/order/OrderQueryFrame.htm页面的一个textbox
//如何解决
xdoc.getElementById("txtMONo").innerText= "aa";
}
private mshtml.HTMLDocument Launch_orderquery(string urlparam)
{
SHDocVw.ShellWindows sws = new SHDocVw.ShellWindows();
foreach (SHDocVw.InternetExplorer iw in sws) {
if (iw.LocationName == "Transportation Optimization Management System")
{
mshtml.HTMLDocument doc = (mshtml.HTMLDocument)iw.Document;
FramesCollection framesList = doc.frames; //得到 Frames 集合
object idx = (object)2; //取第三个Frame : mainframe
try{
mshtml.HTMLWindow2 fwin = (mshtml.HTMLWindow2)framesList.item(ref (object)idx); mshtml.HTMLDocument fdoc = (mshtml.HTMLDocument)fwin.document;
if(urlparam!=fdoc.url) fdoc.url=urlparam;
return fdoc;
} catch (System.Exception se) {
MessageBox.Show(se.Message);
}
}
}
sws=null;
return null;
}
}
}
using System.Data;
using System.Text;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Diagnostics;
using SHDocVw;
using mshtml;namespace SZH_Local_System.Prj_Obj.Trk_pocreation
{
public class TMSquery : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
private System.ComponentModel.Container components = null;public TMSquery(){ InitializeComponent();}
protected override void Dispose( bool disposing ) {
........
........}#region Windows Form Designer generated code
private void InitializeComponent(){
........
........}
#endregionprivate void button1_Click(object sender, System.EventArgs e){
mshtml.HTMLDocument orderquerydoc=Launch_orderquery("http://xxxxxx/xxxxx/order/OrderQueryFrame.htm");
if(orderquerydoc!=null) Operate_OrderQuery(orderquerydoc);
}private void Operate_OrderQuery(mshtml.HTMLDocument xdoc)
{
//这里出错了,xtMONo为http://xxxxxx/xxxxx/order/OrderQueryFrame.htm页面的一个textbox
//如何解决
xdoc.getElementById("txtMONo").innerText= "aa";
}
private mshtml.HTMLDocument Launch_orderquery(string urlparam)
{
SHDocVw.ShellWindows sws = new SHDocVw.ShellWindows();
foreach (SHDocVw.InternetExplorer iw in sws) {
if (iw.LocationName == "Transportation Optimization Management System")
{
mshtml.HTMLDocument doc = (mshtml.HTMLDocument)iw.Document;
FramesCollection framesList = doc.frames; //得到 Frames 集合
object idx = (object)2; //取第三个Frame : mainframe
try{
mshtml.HTMLWindow2 fwin = (mshtml.HTMLWindow2)framesList.item(ref (object)idx); mshtml.HTMLDocument fdoc = (mshtml.HTMLDocument)fwin.document;
if(urlparam!=fdoc.url) fdoc.url=urlparam;
return fdoc;
} catch (System.Exception se) {
MessageBox.Show(se.Message);
}
}
}
sws=null;
return null;
}
}
}
xdoc.getElementById("txtMONo").innerText= "aa"; //这行被黄色背景 highlight 打印出来的错误信息: Object reference not set to an instance of an object.
我检查过 xdoc.getElementById("txtMONo").innerText= "aa"; 大小写拼写完全正确. 我现在更多的是怀疑 xdoc 并不是http://xxxxxx/xxxxx/order/OrderQueryFrame.htm 的 document对象 ,
所以导致 访问ID 为txtMONo 的textbox时 出错
检查发现: xdoc.GetElementById("txtMONo")是 为null