打开文件竟然遇到文件重复
(Vlax-Invoke-Method (Vlax-Get excel1 'Workbooks) 'open xls)我用lisp打开excel文件出错,竟然提示文件已存在,但帮助提示此类错误出现于新建或另存的时候,我打开文件也遇到了,这愈发是RP了,有遇到过我这种问题的吗 没什么大惊小怪的,你的程序用了open方法,而没有用相应的close方法,就会出现这种错误 (Vlax-Invoke-Method (Vlax-Get excel1 'ActiveWorkbook) 'Close)
有的,每次都有用这句关闭文件 xiaoquansb 发表于 2013-6-6 12:42 static/image/common/back.gif
(Vlax-Invoke-Method (Vlax-Get excel1 'ActiveWorkbook) 'Close)
有的,每次都有用这句关闭文件
如果你不愿意发你的代码的话,你最好先遍历workbooks集合,再决定是否调用open方法 再说了,活动文档并不一定是你指定的文档,我一般是调用workbooks集合的close方法,而不是某个workbook对象的close方法。把所有的文档都关了,一了百了 (defun 汇总玻璃提料单 (xls_path)
;(princ xls_path)
(setq i 0 num (length xls_path) 有效料单 (list) 无效料单 "" 玻璃清单 (list (list "玻璃清单" )))
(foreach xls xls_path
(princ "\n正在处理 " ) (princ (setq i (1+ i))) (princ "/" ) (princ num)
;打开提料单
(Vlax-Invoke-Method (Vlax-Get excel1 'Workbooks) 'open xls)
(setq cells1 (Vlax-Get excel1 'Cells))
;获取料单编号
(setq 料单编号 (Vlax-Get (vlax-variant-value (vlax-get-property cells1 "item" 4 6)) 'Value2))
;汇总该料单数据
(IF (vl-string-search "-BL-" 料单编号)
(progn
(setq 有效料单 (APPEND 有效料单 (list 料单编号)))
(setq n 5)
(while (setq 外协编号 (Vlax-Get (vlax-variant-value (vlax-get-property cells1 "item" (setq n (1+ n)) 5)) 'Value2))
(if (setq temp (assoc 外协编号 玻璃清单))
;已有编号则添加数量
(progn
(setq 数量 (Vlax-Get (vlax-variant-value (vlax-get-property cells1 "item" n 11)) 'Value2))
(setq new_temp temp)
(repeat (- (+ 9 (length 有效料单)) (length temp))
(setq new_temp (APPEND new_temp (list "-" )))
);end repeat
(if (= (type 数量) (type "" )) (setq 数量 (ATOI 数量)))
(setq new_temp (APPEND new_temp (list 数量)))
(setq 玻璃清单 (SUBST new_temp temp 玻璃清单))
);end progn
;未有编号则新增一行
(progn
(setq 物料名称 (Vlax-Get (vlax-variant-value (vlax-get-property cells1 "item" n 2)) 'Value2))
(setq 物料编码 (Vlax-Get (vlax-variant-value (vlax-get-property cells1 "item" n 3)) 'Value2))
(setq 工程属性 (Vlax-Get (vlax-variant-value (vlax-get-property cells1 "item" n 4)) 'Value2))
(setq 加工图号 (Vlax-Get (vlax-variant-value (vlax-get-property cells1 "item" n 6)) 'Value2))
(setq 长度 (Vlax-Get (vlax-variant-value (vlax-get-property cells1 "item" n 7)) 'Value2))
(setq 宽度 (Vlax-Get (vlax-variant-value (vlax-get-property cells1 "item" n 8)) 'Value2))
(setq 厚 (Vlax-Get (vlax-variant-value (vlax-get-property cells1 "item" n 9)) 'Value2))
(if (= (type 长度) (type "" )) (setq 长度 (ATOI 长度)))
(if (= (type 宽度) (type "" )) (setq 宽度 (ATOI 宽度)))
(setq 单件面积(RTOS (/ (* 长度 宽度) 10000.0) 2 3))
(setq 自由项 (Vlax-Get (vlax-variant-value (vlax-get-property cells1 "item" n 13)) 'Value2))
(if (or (= 自由项 nil) (= 自由项 "" )) (setq 自由项 "-" ))
(setq 数量 (Vlax-Get (vlax-variant-value (vlax-get-property cells1 "item" n 11)) 'Value2))
(if (= (type 数量) (type "" )) (setq 数量 (ATOI 数量)))
(setq temp (list 外协编号 物料名称 物料编码 工程属性 加工图号 长度 宽度 厚 单件面积 自由项))
(repeat (1- (length 有效料单))
(setq temp (APPEND temp (list "-" )))
);end repeat
(setq new_temp (APPEND temp (list 数量)))
(setq 玻璃清单 (APPEND 玻璃清单 (list new_temp)))
);end progn
);end if
);end while
);end progn
(setq 无效料单 (STRCAT 无效料单 "\n " 料单编号))
);end if
(Vlax-Invoke-Method (Vlax-Get excel1 'ActiveWorkbook) 'Close) );end foreach
;添加总数项
(setq n 1)
(foreach temp 玻璃清单
(if (/= (car temp) "玻璃清单" )
(progn
(setq new_temp temp n (1+ n))
(repeat (- (+ 10 (length 有效料单)) (length temp))
(setq new_temp (APPEND new_temp (list "-" )))
);end repeat
(setq 总数 (STRCAT "=SUM(K" (ITOA n) ":" (->column (+ (length 有效料单) 10))(ITOA n) ")"))
(setq new_temp (APPEND new_temp (list 总数 )))
(setq 玻璃清单 (SUBST new_temp temp 玻璃清单))
);end progn
);end if
);end foreach
;寻找重复料单
(setq 重复料单 "" temp_list 有效料单)
(foreach temp temp_list
(setq temp_list (cdr temp_list))
(if (MEMBER temp temp_list)
(setq 重复料单 (STRCAT 重复料单 "\n " temp))
);end if
);end foreach
);end defun 本帖最后由 xiaoquansb 于 2013-6-6 18:11 编辑
xiaoquansb 发表于 2013-6-6 18:02 http://bbs.mjtd.com/static/image/common/back.gif
(defun 汇总玻璃提料单 (xls_path)
;(princ xls_path)
(setq i 0 num (length xls_path) 有效料单 (lis ...
这段就是出问题的子函数,我把close放在foreach里面,每打开一个excel文档就关一次,结果就随机地到某一个文档就提示文件重复,报错,我把这里的close取消了,放在的程序的最后一步执行close把所有打开的excel一次过关闭,问题反而消失了(这应该就是楼上朋友说的那般道理吧) repeat改为while方法即可.不关文件用repeat没问题
页:
[1]