谁能帮我看一下如何添加关闭Excel进程

goosen 2008-05-10 07:04:24
我的代码如下:


Private Sub Command1_Click()
Dim i As Integer, j As Integer, G As Integer
Dim fn As Long, strT As String, arr() As String
For i = 0 To File1.ListCount - 1
fn = FreeFile
Open File1.Path & "\" & File1.List(i) For Input As #fn
For j = 1 To 3
Line Input #fn, strT
Next j
arr = Split(strT, ",")
sh.cells(1, 1) = "线路名"
sh.cells(1, 2) = "导线长度"
sh.cells(1, 3) = "Fb"
sh.cells(1, 4) = "Fb(max)"
sh.cells(1, 5) = "相对闭合差"
sh.cells(1, 6) = "站数"
sh.cells(i + 2, 1) = arr(7)
sh.cells(i + 2, 2) = arr(4)
sh.cells(i + 2, 3) = arr(0)
sh.cells(i + 2, 4) = arr(1)
sh.cells(i + 2, 5) = arr(6)
G = 0
Do Until EOF(1)
Line Input #fn, strT
G = G + 1
Loop

Close #fn
sh.cells(i + 2, 6) = G
Next i
ex.Visible = True

End Sub

Private Sub Command2_Click()
Dim i As Integer, j As Integer
Dim fn As Long, strT As String, arr() As String
For i = 0 To File1.ListCount - 1
fn = FreeFile
Open File1.Path & "\" & File1.List(i) For Input As #fn
For j = 1 To 2
Line Input #fn, strT
Next j
arr = Split(strT, ",")
sh.cells(1, 1) = "线路名"
sh.cells(1, 2) = "导线长度"
sh.cells(1, 3) = "∑b"
sh.cells(i + 2, 1) = arr(0)
For j = 1 To 1
Line Input #fn, strT
Next j
Close #fn
arr = Split(strT, ",")
sh.cells(i + 2, 2) = arr(1)
sh.cells(i + 2, 3) = arr(3)
Next i
ex.Visible = True


End Sub


Private Sub Dir1_Change()
File1.Path = Dir1.Path
End Sub

Private Sub Drive1_Change()
Dir1.Path = Drive1.Drive
End Sub

Private Sub Form_Load()
File1.Pattern = "g*.txt;z*.txt;1-printhighf-g*.txt;1-printhighf-z*.txt"
Set ex = CreateObject("Excel.Application")
Set wb = ex.workbooks.Add
Set sh = wb.Sheets(1)
End Sub
...全文
79 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
goosen 2008-05-11
  • 打赏
  • 举报
回复
这不等于没改吗?

我的问题是

每运行一次程序,,,就会有一个EXCEL进程
比如我打开程序10次

进程里就会有10个进程

我关了程序 进程还在
zzyong00 2008-05-10
  • 打赏
  • 举报
回复
Private Sub Command1_Click() 
Dim i As Integer, j As Integer, G As Integer
Dim fn As Long, strT As String, arr() As String
dim ex,wb,sh
Set ex = CreateObject("Excel.Application")
Set wb = ex.workbooks.Add
Set sh = wb.Sheets(1)

For i = 0 To File1.ListCount - 1
fn = FreeFile
Open File1.Path & "\" & File1.List(i) For Input As #fn
For j = 1 To 3
Line Input #fn, strT
Next j
arr = Split(strT, ",")
sh.cells(1, 1) = "线路名"
sh.cells(1, 2) = "导线长度"
sh.cells(1, 3) = "Fb"
sh.cells(1, 4) = "Fb(max)"
sh.cells(1, 5) = "相对闭合差"
sh.cells(1, 6) = "站数"
sh.cells(i + 2, 1) = arr(7)
sh.cells(i + 2, 2) = arr(4)
sh.cells(i + 2, 3) = arr(0)
sh.cells(i + 2, 4) = arr(1)
sh.cells(i + 2, 5) = arr(6)
G = 0
Do Until EOF(1)
Line Input #fn, strT
G = G + 1
Loop

Close #fn
sh.cells(i + 2, 6) = G
Next i
ex.Visible = True
End Sub
goosen 2008-05-10
  • 打赏
  • 举报
回复
我不想马上保存


只是打开就可以了

保存 直接用Excel来操作就行拉
zzyong00 2008-05-10
  • 打赏
  • 举报
回复
是呀,你不想保存吗?
wb.Close False
改为wb.SaveAs "c:\book1.xls"

goosen 2008-05-10
  • 打赏
  • 举报
回复
还是不行 用你的这种方法
一运行就问 是否保存文件了
zzyong00 2008-05-10
  • 打赏
  • 举报
回复
wb.Close False 改为wb.close true
zzyong00 2008-05-10
  • 打赏
  • 举报
回复
Private Sub Command1_Click() 
Dim i As Integer, j As Integer, G As Integer
Dim fn As Long, strT As String, arr() As String
dim ex,wb,sh
Set ex = CreateObject("Excel.Application")
Set wb = ex.workbooks.Add
Set sh = wb.Sheets(1)

For i = 0 To File1.ListCount - 1
fn = FreeFile
Open File1.Path & "\" & File1.List(i) For Input As #fn
For j = 1 To 3
Line Input #fn, strT
Next j
arr = Split(strT, ",")
sh.cells(1, 1) = "线路名"
sh.cells(1, 2) = "导线长度"
sh.cells(1, 3) = "Fb"
sh.cells(1, 4) = "Fb(max)"
sh.cells(1, 5) = "相对闭合差"
sh.cells(1, 6) = "站数"
sh.cells(i + 2, 1) = arr(7)
sh.cells(i + 2, 2) = arr(4)
sh.cells(i + 2, 3) = arr(0)
sh.cells(i + 2, 4) = arr(1)
sh.cells(i + 2, 5) = arr(6)
G = 0
Do Until EOF(1)
Line Input #fn, strT
G = G + 1
Loop

Close #fn
sh.cells(i + 2, 6) = G
Next i
ex.Visible = True



wb.Close False
ex.Quit
Set ex = Nothing
Set wb = Nothing
Set sh = Nothing
End Sub
goosen 2008-05-10
  • 打赏
  • 举报
回复
Private Sub Command1_Click()
Dim i As Integer, j As Integer, G As Integer
Dim fn As Long, strT As String, arr() As String
For i = 0 To File1.ListCount - 1
fn = FreeFile
Open File1.Path & "\" & File1.List(i) For Input As #fn
For j = 1 To 3
Line Input #fn, strT
Next j
arr = Split(strT, ",")
sh.cells(1, 1) = "线路名"
sh.cells(1, 2) = "导线长度"
sh.cells(1, 3) = "Fb"
sh.cells(1, 4) = "Fb(max)"
sh.cells(1, 5) = "相对闭合差"
sh.cells(1, 6) = "站数"
sh.cells(i + 2, 1) = arr(7)
sh.cells(i + 2, 2) = arr(4)
sh.cells(i + 2, 3) = arr(0)
sh.cells(i + 2, 4) = arr(1)
sh.cells(i + 2, 5) = arr(6)
G = 0
Do Until EOF(1)
Line Input #fn, strT
G = G + 1
Loop

Close #fn
sh.cells(i + 2, 6) = G
Next i
ex.Visible = True

Set ex = CreateObject("Excel.Application")
Set wb = ex.workbooks.Add
Set sh = wb.Sheets(1)


wb.Close False
ex.Quit
Set ex = Nothing
Set wb = Nothing
Set sh = Nothing
End Sub



我放在这里后程序出错拉
你能帮我看一下吗
DengXingJie 2008-05-10
  • 打赏
  • 举报
回复
看你的代码是有两个Command来操作Excel
建议你的不要把
Set ex = CreateObject("Excel.Application")
Set wb = ex.workbooks.Add
Set sh = wb.Sheets(1)
这些语句放在Form_Load中,而是分别放在Command1与Command2中
然后再Command1与Command2中的结尾处都加入以下代码
wb.Close False
ex.Quit
Set ex= Nothing
Set wb= Nothing
Set sh = Nothing


以你现有的代码在哪结束Excel进程都会出问题.
DengXingJie 2008-05-10
  • 打赏
  • 举报
回复
根据你的情况,放在Form_Unload事件较合适
goosen 2008-05-10
  • 打赏
  • 举报
回复
是加在
Private Sub Form_Load()
File1.Pattern = "g*.txt;z*.txt;1-printhighf-g*.txt;1-printhighf-z*.txt"
Set ex = CreateObject("Excel.Application")
Set wb = ex.workbooks.Add
Set sh = wb.Sheets(1)
End Sub


这里面吗
DengXingJie 2008-05-10
  • 打赏
  • 举报
回复
wb.Close False
ex.Quit
Set ex= Nothing
Set wb= Nothing
Set sh = Nothing

7,763

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧