Excel精英培训网

 找回密码
 注册
数据透视表40+个常用小技巧,让你一次学会!
查看: 4224|回复: 3

[已解决]我想在excel中用vba语句统计word文档中包含特定字符的词组并复制到excel中。

[复制链接]
发表于 2013-5-19 22:27 | 显示全部楼层 |阅读模式
如附件。
1,在excel中写vba:我想在excel中用vba语句统计word文档中以“社会”开头,后面跟着2个任意字的词,即每个词总共4个字,如“社会主义”、“社会和谐”、"社会而奋"(有的不一定是一个有完整含义的词,这个就是这种)等等,并把该文档中所有符合这个要求的这些词依次自动录入到excel首列当中。
2,在word中写vba语句:如果我不在excel中写vba语句,而在word中写vba语句,但还统计到excel中去,又该如何操作合适?
我写了一段代码(在附件表1的代码区),不过根本不能完全解决问题。似懂非懂的感觉,请老师们指点一下,多谢你们的辛苦。
3,如果在excel中在不打开word的情况下达到目的代码和打开word的情况下达到目的代码是否有区别啊,如果有在哪里
最佳答案
2013-5-23 22:36
数目统计不对,
  1. Sub 统计词语()
  2.     Dim objWord As Object
  3.     Dim strWordfile$
  4.     Dim strFind$, arr
  5.     Dim strWhat$
  6.     Dim blFind As Boolean
  7.     On Error GoTo ErrorHandler
  8.     Application.ScreenUpdating = False
  9.     Do
  10.         strWhat = Application.InputBox(prompt:="请输入要查找的字符串", Title:="提示", Default:="社会", Type:=2)
  11.     Loop Until strWhat <> "False"


  12.     strWordfile = "89.docx"
  13.     Set objWord = GetObject(ThisWorkbook.Path & Application.PathSeparator & _
  14.                             strWordfile)

  15.     With objWord.ActiveWindow.Selection.Find
  16.         .ClearFormatting
  17.         .Text = "社会[!^1-^125]{2}"
  18.         .Replacement.Text = ""
  19.         .Forward = True
  20.         .Wrap = wdFindContinue
  21.         .MatchWildcards = True
  22.         Do
  23.             blFind = .Execute
  24.             strFind = strFind & objWord.ActiveWindow.Selection.Text & ","
  25.         Loop Until Not blFind
  26.     End With

  27.     If Len(strFind) > 1 Then
  28.         strFind = Left(strFind, Len(strFind) - 1)
  29.         objWord.Close False
  30.         Set objWord = Nothing
  31.         arr = Split(strFind, ",")
  32.         Columns(1).ClearContents
  33.         Range("a1").Resize(UBound(arr)).Value = WorksheetFunction.Transpose(arr)
  34.         MsgBox "统计完成", vbInformation + vbOKOnly
  35.     End If
  36.     Application.ScreenUpdating = True
  37.     Exit Sub
  38. ErrorHandler:
  39.     MsgBox Err.Number & vbCrLf & _
  40.            Err.Description
  41.     Application.ScreenUpdating = True
  42. End Sub
复制代码

统计表.rar

4.44 KB, 下载次数: 20

文档.rar

11.3 KB, 下载次数: 20

发表于 2013-5-23 22:13 | 显示全部楼层
  1. Sub 统计词语()
  2.     Dim objWord As Object
  3.     Dim strWordfile$
  4.     Dim strFind$, arr
  5.     Dim strWhat$
  6.     Dim blFind As Boolean
  7.     On Error GoTo ErrorHandler
  8.     Application.ScreenUpdating = False
  9.     Do
  10.     strWhat = Application.InputBox(prompt:="请输入要查找的字符串", Title:="提示", Default:="社会", Type:=2)
  11.     Loop Until strWhat <> "False"
  12.    

  13.     strWordfile = "89.docx"
  14.     Set objWord = GetObject(ThisWorkbook.Path & Application.PathSeparator & _
  15.                             strWordfile)

  16.     With objWord.ActiveWindow.Selection.Find
  17.         .ClearFormatting
  18.         .Text = "社会[!^1-^125]{2}"
  19.         .Replacement.Text = ""
  20.         .Forward = True
  21.         .Wrap = wdFindContinue
  22.         .MatchWildcards = True
  23.         Do
  24.             blFind = .Execute
  25.             strFind = strFind & objWord.ActiveWindow.Selection.Text & ","
  26.         Loop Until Not blFind
  27.     End With
  28.    
  29.     If Len(strFind) > 1 Then
  30.         strFind = Left(strFind, Len(strFind) - 1)
  31.         objWord.Close False
  32.         Set objWord = Nothing
  33.         arr = Split(strFind, ",")
  34.         Columns(1).ClearContents
  35.         Range("a1").Resize(UBound(arr) + 1).Value = WorksheetFunction.Transpose(arr)
  36.         MsgBox "统计完成", vbInformation + vbOKOnly
  37.     End If
  38.     Application.ScreenUpdating = True
  39.     Exit Sub
  40. ErrorHandler:
  41.     MsgBox Err.Number & vbCrLf & _
  42.            Err.Description
  43.     Application.ScreenUpdating = True
  44. End Sub
复制代码
回复

使用道具 举报

发表于 2013-5-23 22:13 | 显示全部楼层
后面那个字符我强制要求是中文了。
回复

使用道具 举报

发表于 2013-5-23 22:36 | 显示全部楼层    本楼为最佳答案   
数目统计不对,
  1. Sub 统计词语()
  2.     Dim objWord As Object
  3.     Dim strWordfile$
  4.     Dim strFind$, arr
  5.     Dim strWhat$
  6.     Dim blFind As Boolean
  7.     On Error GoTo ErrorHandler
  8.     Application.ScreenUpdating = False
  9.     Do
  10.         strWhat = Application.InputBox(prompt:="请输入要查找的字符串", Title:="提示", Default:="社会", Type:=2)
  11.     Loop Until strWhat <> "False"


  12.     strWordfile = "89.docx"
  13.     Set objWord = GetObject(ThisWorkbook.Path & Application.PathSeparator & _
  14.                             strWordfile)

  15.     With objWord.ActiveWindow.Selection.Find
  16.         .ClearFormatting
  17.         .Text = "社会[!^1-^125]{2}"
  18.         .Replacement.Text = ""
  19.         .Forward = True
  20.         .Wrap = wdFindContinue
  21.         .MatchWildcards = True
  22.         Do
  23.             blFind = .Execute
  24.             strFind = strFind & objWord.ActiveWindow.Selection.Text & ","
  25.         Loop Until Not blFind
  26.     End With

  27.     If Len(strFind) > 1 Then
  28.         strFind = Left(strFind, Len(strFind) - 1)
  29.         objWord.Close False
  30.         Set objWord = Nothing
  31.         arr = Split(strFind, ",")
  32.         Columns(1).ClearContents
  33.         Range("a1").Resize(UBound(arr)).Value = WorksheetFunction.Transpose(arr)
  34.         MsgBox "统计完成", vbInformation + vbOKOnly
  35.     End If
  36.     Application.ScreenUpdating = True
  37.     Exit Sub
  38. ErrorHandler:
  39.     MsgBox Err.Number & vbCrLf & _
  40.            Err.Description
  41.     Application.ScreenUpdating = True
  42. End Sub
复制代码
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|Archiver|Excel精英培训 ( 豫ICP备11015029号 )

GMT+8, 2024-4-26 05:47 , Processed in 0.368873 second(s), 13 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表