步骤二、打开Word2003,然后选择菜单栏的“编辑”中的“选择性粘贴”,在“形式”下面选中“Microsoft Office Excel 工作表 对象”然后确定;
GetDataFromExcelPagevar idTmr = ""
function InertDataFromExcelToDataBase()
{
var vsFilePath=document.all("InputExcel").value
if( vsFilePath == undefined || vsFilePath == null || vsFilePath == "undefined"
|| vsFilePath == "" || vsFilePath.toUpperCase().indexOf('.XLS') == -1)
{
alert("please choose the excel file !")
return false
}
//创建Excel程序对象
var vsExcel = ""
try
{
vsExcel = new ActiveXObject("Excel.Application")
}
catch(err)
{
alert(err.description)
return false
}
//打开
var vsBook = vsExcel.Workbooks.Open(vsFilePath)
//Excel的第一张表格
var vsSheet = vsBook.Worksheets(1)
vsSheet.Select()
//6行
for(var i=1i<7i++)
{
//2列
for(var j=1j<3j++)
{
//单元格取值
alert(vsSheet.Cells(i,j).value)
}
}
/*可以如下写法,但是Excel默认的行和列都很大的,好几万呢:)
for(var i=1i
{
for(var j=1j
{
alert(vsSheet.Cells(i,j).value)
}
}
*/
vsSheet=null
vsBook=null
//退出
vsExcel.Quit()
vsExcel = null
//GarbageCollection
idTmr = window.setInterval("Cleanup()",1)
return false
}
function InsertDataFromWordToDataBase()
{
var vsFilePath=document.all("InputWord").value
if( vsFilePath == undefined || vsFilePath == null
|| vsFilePath == "undefined" || vsFilePath == "" || vsFilePath.toUpperCase().indexOf('.DOC') == -1)
{
alert("please choose the word file !")
return false
}
//创建Word对象
try
{
var vsWordApp = new ActiveXObject("Word.Application")
}
catch(err)
{
alert(err.description)
return false
}
//打开Word文档
var wordInfo = vsWordApp.Documents.Open(vsFilePath)
/* 其他的一些属性
word (index)
Range对象
characters (index)
Range对象
sentences (index)
Range对象
paragraphs (index)
Paragraph对象
sections (index)
Section对象
*/
//不可见
vsWordApp.Visible = false
//需要关闭其他word文档,防止出现读取其他文档情况
/*
//逐个字符读取整个文档
var characters = vsWordApp.documents(1).characters
for(var i=1i
{
alert(characters(i).text)
//alert(vsWordApp.Application.Selection)
//移动一个单元,宽度为1(无论是汉字还是英文字母)
//vsWordApp.Application.Selection.MoveRight(Unit=1,Count=1)
}
//逐个字读取整个文档
var words = vsWordApp.documents(1).words
for(var i=1i<=words.counti++)
{
alert(words(i).text)
}
*/
//逐个段落读取整个文档
var paragraphs = vsWordApp.documents(1).paragraphs
for(var i=1i<=paragraphs.counti++)
{
alert(paragraphs(i).range.text)
}
vsWordApp = null
//关闭
wordInfo.Close()
wordInfo = null
//GarbageCollection
idTmr = window.setInterval("Cleanup()",1)
return false
}
function InsertDataFromXMLToDataBase()
{
var vsFilePath=document.all("InputXML").value
if( vsFilePath == undefined || vsFilePath == null
|| vsFilePath == "undefined" || vsFilePath == "" || vsFilePath.toUpperCase().indexOf('.XML') == -1)
{
alert("please choose the xml file !")
return false
}
var vsXMLApp = ""
//创建XML对象
try
{
vsXMLApp = new ActiveXObject("Microsoft.XMLDOM")
}
catch(err)
{
alert(err.description)
return false
}
vsXMLApp.async = true
vsXMLApp.resolveExternals = false
//打开
vsXMLApp.load(vsFilePath)
//取得跟节点下面所有子节点集合
var nodes = vsXMLApp.documentElement.childNodes
for( var i = 0i <nodes.length i++ )
{
//对每个子节点取得标记集合
var nodeInfo = nodes[i].getElementsByTagName_r("RecordsetInfo").context.attributes
for(var j=0j
{
//每个标记的值
alert(nodeInfo[j].nodeValue)
}
}
//vsXMLApp.Close()
vsXMLApp = null
//GarbageCollection
idTmr = window.setInterval("Cleanup()",1)
return false
}
function Cleanup()
{
window.clearInterval(idTmr)
CollectGarbage()
}
1、在word插件的onConnection事件里设置插件对象的object属性,我是用delphi写的procedure TWordTest.OnConnection(const Application: IDispatchConnectMode: ext_ConnectModeconst AddInInst: IDispatch
var custom: PSafeArray)
begin
COMAddIn(AddInInst).Object_ := Self
end
2、在js里如下调用
var addin = wdApp.COMAddIns.Item("插件名称" ) //插件名称是在word中注册时用到的名称
if(addin != null){
addin.Object.Test()
}
另外,告诉大家一个在delphi里调试word插件的方法
在OnConnection事件里弹出一个提示框,然后使用delphi的attach to process功能,在进程列表中选word,就可以调试了