Board logo

标题: [问题求助] vbs如何实现excel中的选择性粘贴 [打印本页]

作者: lgzhaha    时间: 2013-4-17 21:11     标题: vbs如何实现excel中的选择性粘贴

在网上搜索到excel的选择性粘贴的语句,但貌似不能用
AWB.ActiveSheet.Cells.PasteSpecial Paste:=xlValues
这句是选择性粘贴的语句吧......
那我这样用可以不?
--------------------------------------------------------------
Set oWord = CreateObject("Word.Application")
oWord.Visible = False     
Set objDoc = oWord.Documents.Open("c:\aa.doc")
Set oExcel = CreateObject("Excel.Application")
oExcel.Visible = True  
Set oWb = oExcel.Workbooks.Open("C:\aa.xls")
Set xlsheet = oExcel.Worksheets("sheet1")
oWord.ActiveDocument.Tables(3).Cell(3,3).Select
oWord.Selection.Copy
xlsheet.Cells(1,1).PasteSpecial Paste:=xlValues
    ( 运行后上面这句提示   “缺少语句”,请问哪里出错了)
oWord.Quit
Set oWord = Nothing
Set objDoc = Nothing
Set fso = Nothing
Set oread = Nothing
Set oExcel = Nothing
Set oWb = Nothing
Set xlsheet = Nothing
--------------------------------------------------------------------------------------
我的目的就是想把aa.doc中的表格里的信息提取到aa.xls中,
如果使用 xlsheet.Cells(1,1).PasteSpecial正常,可以复制粘贴,但把word中的一个单元格都复制进来了,所以就想用excel的选择性粘贴来粘贴上文本。
就指点。。。
作者: CrLf    时间: 2013-5-24 18:49

两个错误:
1、Paste:=xlValues 是 vba 的内部参数赋值方式
2、xlValues 是 excel 的常量,在 vbs 中尚未定义

改下应该就好了:
  1. Const xlPasteAll=-4104
  2. Const xlPasteAllExceptBorders=7
  3. Const xlPasteColumnWidths=8
  4. Const xlPasteComments=-4144
  5. Const xlPasteFormats=-4122
  6. Const xlPasteFormulas=-4123
  7. Const xlPasteFormulasAndNumberFormats=11
  8. Const xlPasteValidation=6
  9. Const xlPasteValues=-4163
  10. Const xlPasteValuesAndNumberFormats=12
  11. '定义 excel 中的各种常量,不定义直接引用数值也可,不过可读性欠佳
  12. Set oWord = CreateObject("Word.Application")
  13. oWord.Visible = False     
  14. Set objDoc = oWord.Documents.Open("c:\aa.doc")
  15. Set oExcel = CreateObject("Excel.Application")
  16. oExcel.Visible = True  
  17. Set oWb = oExcel.Workbooks.Open("C:\aa.xls")
  18. Set xlsheet = oExcel.Worksheets("sheet1")
  19. oWord.ActiveDocument.Tables(3).Cell(3,3).Select
  20. oWord.Selection.Copy
  21. xlsheet.Cells(1,1).PasteSpecial xlPasteValues
  22. oWord.Quit
  23. Set oWord = Nothing
  24. Set objDoc = Nothing
  25. Set fso = Nothing
  26. Set oread = Nothing
  27. Set oExcel = Nothing
  28. Set oWb = Nothing
  29. Set xlsheet = Nothing
复制代码

作者: luoxiao    时间: 2013-7-15 10:54

Set oExcel= CreateObject("Excel.Application")
Set oWb = oExcel.Workbooks.Open("d:\test.xls")
Set oSheet = oWb.Sheets("Sheet1")
oExcel.rows(3).delete
-------------------------------
能告诉下我为什么把test.xls改成*.xls没用啊?
要怎么写才有用?




欢迎光临 批处理之家 (http://bbs.bathome.net/) Powered by Discuz! 7.2