20170906xlVBA_GetEMailFromDocument
Public Sub GetDataFromWord()
AppSettings
'On Error GoTo ErrHandler
Dim StartTime, UsedTime As Variant
StartTime = VBA.Timer
'Input code here Dim Wb As Workbook
Dim Sht As Worksheet
Dim oSht As Worksheet
Dim Rng As Range
Dim Arr As Variant Dim Dic As Object
Set Dic = CreateObject("Scripting.Dictionary") 'Dim wdApp As Word.Application
'Dim wdDoc As Word.Document
Dim wdApp As Object
Dim wdDoc As Object 'Const SHEET_NAME As String = "提取信息"
Set Wb = Application.ThisWorkbook
Set Sht = Wb.Worksheets(1) On Error Resume Next
Set wdApp = GetObject(, "Word.Application")
If wdApp Is Nothing Then
Set wdApp = CreateObject("Word.Application")
End If
On Error GoTo 0 'Set wdApp = New Word.Application Filename = Dir(Wb.Path & "\*.doc*")
Do While Filename <> ""
Debug.Print Filename
FilePath = Wb.Path & "\" & Filename
Set wdDoc = wdApp.Documents.Open(FilePath)
Text = wdDoc.Content.Text If RegTest(Text, "(\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*)") Then
Arr = RegGetArray("(\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*)", Text)
For i = LBound(Arr) To UBound(Arr)
Key = CStr(Arr(i))
Debug.Print Key
If Not Dic.Exists(Key) Then
Dic(Key) = Dic.Count + 1
End If
Next i End If Filename = Dir
Loop Application.StatusBar = ">>>>>>>>Regexpress Getting array >>>>>>>>"
wdDoc.Close False '关闭doc
wdApp.Quit '退出app
Set wdApp = Nothing
Set wdDoc = Nothing With Sht
.Cells.ClearContents
.Range("A1:B1").Value = Array("序号", "邮箱")
Set Rng = .Range("A2")
Set Rng = Rng.Resize(Dic.Count, 2)
Rng.Value = Application.WorksheetFunction.Transpose(Array(Dic.Items, Dic.keys))
End With
UsedTime = VBA.Timer - StartTime
Debug.Print "UsedTime:" & Format(UsedTime, "0.000 Seconds")
'MsgBox "UsedTime:" & Format(UsedTime, "0.000 Seconds"), vbOKOnly, "QQ "
ErrorExit:
Set Wb = Nothing
Set Sht = Nothing
Set Rng = Nothing Set Dic = Nothing AppSettings False On Error Resume Next
wdApp.Quit Exit Sub
ErrHandler:
If Err.Number <> 0 Then
MsgBox Err.Description & "!", vbCritical, "QQ "
Debug.Print Err.Description
Err.Clear
Resume ErrorExit
End If
End Sub
Public Sub AppSettings(Optional IsStart As Boolean = True)
If IsStart Then
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Application.Calculation = xlCalculationManual
Application.StatusBar = ">>>>>>>>Macro Is Running>>>>>>>>"
Else
Application.ScreenUpdating = True
Application.DisplayAlerts = True
Application.Calculation = xlCalculationAutomatic
Application.StatusBar = False
End If
End Sub
Public Function RegGetArray(ByVal Pattern As String, ByVal OrgText As String) As String()
Dim Reg As Object, Mh As Object, OneMh As Object
Dim Arr() As String, Index As Long
Dim Elm As String
Set Reg = CreateObject("Vbscript.Regexp")
With Reg
.MultiLine = True
.Global = True
.Ignorecase = False
.Pattern = Pattern
Set Mh = .Execute(OrgText) Index = 0
ReDim Arr(1 To 1)
For Each OneMh In Mh
Index = Index + 1
ReDim Preserve Arr(1 To Index)
'If OneMh.submatches(0) <> "" Then Elm = OneMh.submatches(0)
Arr(Index) = OneMh.submatches(0) Next OneMh
End With
RegGetArray = Arr
Set Reg = Nothing
Set Mh = Nothing
End Function
Public Function RegTest(ByVal OrgText As String, ByVal Pattern As String) As Boolean
'传递参数 :原字符串, 匹配模式
Dim Regex As Object
Set Regex = CreateObject("VBScript.RegExp")
With Regex
.Global = True
.Pattern = Pattern
End With
RegTest = Regex.TEST(OrgText)
Set Regex = Nothing
End Function
20170906xlVBA_GetEMailFromDocument的更多相关文章
随机推荐
- qt无法定位程序输入点 于动态链接库 qt5core.dll
造成步骤:一开始是将现成的dll[Qt5.9.3]放在文件夹里,然后使用Qt5.7.1编译的exe放进去,产生标题错误 原因:dll库不匹配 解决:使用Qt5.7.1自带的cmd命令行,使用winde ...
- Junit的异常测试
方式1: @Test(expected = IndexOutOfBoundsException.class) public void empty() { new ArrayList<Object ...
- Eclipse使用maven命令安装第三方jar包
使用原因: 使用maven时,有些第三方jar包是不能从maven远程仓库中下载得到,因此导致在pom.xml中添加jar包依赖时会怎么添加都会报错(Missing artifact ojdbc:oj ...
- windows下使用gvim不支持python3.6问题解决
在用户目录下C:\Users\Administrator\新建vim配置文件夹vimfiles,然后该文件下建立一个文件vimrc vimrc内容: set pythonthreedll=python ...
- Golang初练手-多线程网站路径爆破
以前用Python写过这个工具,前两天看了golang的基础,就想着用这个语言把这个工具重写一遍 先放张图 用法 Example : Buster.exe -u=https://www.baidu.c ...
- 【resultType】Mybatis种insert或update的resultType问题
Attribute "resultType" must be declared for element type "insert"或"update&q ...
- Java 多线程案例
同步代码块 SynchronizedTest类,用来表示取票功能 package concurency.chapter6; public class SynchronizedTest implemen ...
- P4145 上帝造题的七分钟2 / 花神游历各国
思路 每个数不会被开方超过log次,对每个数暴力开方即可 代码 #include <algorithm> #include <cstring> #include <cst ...
- iframe初始化属性
<iframe id="user" src="xxx.html" frameborder="0" width="" ...
- LightOJ 1268 Unlucky Strings(KMP+矩阵乘法+基础DP)
题意 给出字符串的长度 \(n\) ,以及该字符串是由哪些小写字母组成,现给出一个坏串 \(S\) ,求存在多少种不同的字符串,使得其子串不含坏串. \(1 \leq n \leq 10^9\) \( ...