现在制作报表,遇到个问题,请高手们讲解一下,程序如下:
Dim objExcelAPP
Set objExcelAPP = CreateObject("Excel.Application") ...(这该用GETOBJECT,还是用CREATEOBJECE函数呢)
objExcelAPP.Visible = 1
If TypeName(objExcelAPP)="Application" Then
For Each xlbook In objExcelAPP.Workbooks
If xlbook.FullName=xlsname Then
isOpen=True
Exit For
End If
Next
Else
End If
If isOpen=True Then
objExcelAPP.Cells(13,4).Value = HMIRuntime.Tags("NewTag").read
Dim NewTag_1
Set NewTag_1 = HMIRuntime.Tags("NewTag_1")
NewTag_1.value= Time
NewTag_1.Write
objExcelAPP.Cells(13,5).Value =HMIRuntime.Tags("NewTag_1").read
Else
End If
为什么在全局报表中不能传输数据呢,问题处在哪,请讲解一下,程序就是想获得现在运行的EXCEL报表进程,然后将数据写入,如果不这样,能直接写入吗?估计是不能吧!如果有什么解决的程序,希望高手们不要吝啬,谢谢了!
问题补充:
为什么下面程序就行呢
Dim objExcelAPP
Set objExcelAPP = CreateObject("Excel.Application") ''??è?μ±?°??DDμ?Exceló|ó?3ìDò???ó
objExcelAPP.Visible = 1
objExcelApp.Workbooks.Open "D:\shengchanjilu/R2012/R2012-baobiao.xls"
If 1>0 Then
objExcelAPP.Cells(13,4).Value = HMIRuntime.Tags("NewTag").read
Dim NewTag_1
Set NewTag_1 = HMIRuntime.Tags("NewTag_1")
NewTag_1.value= Time
NewTag_1.Write
objExcelAPP.Cells(13,5).Value =HMIRuntime.Tags("NewTag_1").read
Else
End If
希望能讲解一下
最佳答案
你上面的代码不知道是不是完整的,文档名都没指定,在你的代码基础上修改了下,你参考一下
On Error Resume Next ''此句必须有,防止无EXCEL进程时GetObject出错退出
Dim objExcelAPP,xlbook,xlsname,isOpen
xlsname = "D:\shengchanjilu/R2012/R2012-baobiao.xls" ''Excel文档路径
Set objExcelAPP = GetObject(,"Excel.Application") ''获取当前运行的Excel应用程序对象
objExcelAPP.Visible = 1 ''Excel应用程序显示属性
If TypeName(objExcelAPP) = "Application" Then ''Excel已打开,遍历工作薄查找对应文档是否已打开
For Each xlbook In objExcelAPP.Workbooks
If xlbook.FullName = xlsname Then
isOpen = True
Exit For
End If
Next
Else ''当前没有打开Excel程序则创建Excel应用程序并打开对应的文档
Set objExcelAPP = CreateObject("Excel.Application")
End If
If Not isOpen Then ''如果对应文档没打开则打开
Set xlbook = xlapp.Workbooks.Open(xlsname)
End if
objExcelAPP.Cells(13,4).Value = HMIRuntime.Tags("NewTag").read
Dim NewTag_1
Set NewTag_1 = HMIRuntime.Tags("NewTag_1")
NewTag_1.value = Time
NewTag_1.Write
objExcelAPP.Cells(13,5).Value = HMIRuntime.Tags("NewTag_1").read
提问者对于答案的评价:
呵呵,谢谢,明白了
原创文章,作者:more0621,如若转载,请注明出处:https://www.zhaoplc.com/plc263619.html