首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > VB >

用VB或VBA查询WORD,该如何处理

2012-01-29 
用VB或VBA查询WORD比如:我有10个word文件想查找每个文件中的固定字符:如:商品名称然后输出导excel谁能给个

用VB或VBA查询WORD
比如:我有10个word文件

          想查找每个文件中的固定字符   :   如:商品名称

            然后输出导excel

谁能给个好点的例子?

是用vba吗?

[解决办法]
用VB或VBA都无所谓,关键是
1、你那10个WORD文件的文件名是否已知?
2、找到指定的字符后,要导出什么内容到Excel?
3、word文件格式统一吗?
[解决办法]
可能是IP或者text没有定义。
[解决办法]
参考:
http://topic.csdn.net/t/20060528/16/4784318.html
[解决办法]
读取WORD表格中的内容:
wdApp.Documents.Open "E:\Lixin\Share\word\baogao\aaa.doc "
For i = 1 To wdApp.ActiveDocument.Tables(1).Rows.Count
wdApp.ActiveDocument.Tables(1).Cell(i, 1).Select
If Left(wdApp.ActiveDocument.Selection.Text, Len(wdApp.ActiveDocument.Selection.Text) - 2) = "铅笔 " Then
wdApp.ActiveDocument.Tables(1).Cell(i, 2).Select
MsgBox Left(wdApp.ActiveDocument.Selection.Text, Len(wdApp.ActiveDocument.Selection.Text) - 2)
End If
Next
[解决办法]
完整代码:
Dim wdApp As Word.Application
Dim exApp As Excel.Application

Private Sub Command1_Click()
If Len(Text1.Text) < 1 Then MsgBox "请选择word文档 ": Exit Sub
If Len(Text2.Text) < 1 Then MsgBox "请选择excel文档 ": Exit Sub
Set wdApp = New Word.Application
Set exApp = New Excel.Application
wdApp.Visible = True
exApp.Visible = True
xx = wdApp.Documents.Open(Text1.Text)
exApp.Workbooks.Open Text2.Text
For k = 1 To exApp.WorksheetFunction.CountA(exApp.ActiveWorkbook.Sheets(1).Range( "a:a "))
s_txt = Range( "a " & k)
For i = 1 To wdApp.ActiveDocument.Tables.Count
For j = 1 To wdApp.ActiveDocument.Tables(i).Rows.Count
wdApp.ActiveDocument.Tables(i).Cell(j, 1).Select
If Left(wdApp.Selection.Text, Len(wdApp.Selection) - 2) = s_txt Then
wdApp.ActiveDocument.Tables(i).Cell(j, 2).Select
exApp.ActiveWorkbook.Sheets(1).Range( "b " & k) = Left(wdApp.Selection.Text, Len(wdApp.Selection.Text) - 2)
End If
Next
Next
Next
wdApp.Quit (False)
exApp.ActiveWorkbook.Save
exApp.Quit
MsgBox "取值完成! "
End Sub

Private Sub Command2_Click()
CommonDialog1.Filter = "Word文档(*.doc)|*.doc "
CommonDialog1.ShowOpen
If Len(CommonDialog1.FileName) > 1 Then Text1.Text = CommonDialog1.FileName
End Sub

Private Sub Command3_Click()
CommonDialog1.Filter = "Excel文档(*.xls)|*.xls "
CommonDialog1.ShowOpen
If Len(CommonDialog1.FileName) > 1 Then Text2.Text = CommonDialog1.FileName
End Sub

Private Sub Form_Load()
Text1.Text = " "
Text2.Text = " "
End Sub

热点排行