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的更多相关文章
随机推荐
- 数据库 --- 4 多表查询 ,Navicat工具 , pymysql模块
一.多表查询 1.笛卡儿积 查询 2.连接 语法: ①inner 显示可构成连接的数据 mysql> select employee.id,employee.name,department ...
- 并发 --- 2 进程的方法,进程锁 守护进程 数据共享 进程队列, joinablequeue模型
一.进程的其他方法 1. .name 进程名 (可指定) 2. .pid 进程号 3. os.getpid 在什么位置就是什么的进程号 4. .is ...
- CXF整合spring,在tomcat中发布webService
服务端 1.首先下载CXF的jar包 http://pan.baidu.com/s/1dFBwSRf 密码: qyax.里面自带了需要用到的spring的jar包. 或者使用maven,如下配置.不论 ...
- 如果让我重来,我会选择C和(或者)Python。
如果让我重来,我会选择C和(或者)Python.Python语法和库更丰富,上手更容易,使用更方便.C简单直接,学习成本不高,贴近底层,能帮助了解底层细节.先强调:1. 语言只是工具,假以时日,你都会 ...
- 论文笔记之:Optical Flow Estimation using a Spatial Pyramid Network
Optical Flow Estimation using a Spatial Pyramid Network spynet 本文将经典的 spatial-pyramid formulation ...
- JVM相关笔记
类的加载过程 加载阶段 主要完成以下3件事情:1.通过“类全名”来获取定义此类的二进制字节流2.将字节流所代表的静态存储结构转换为方法区的运行时数据结构3.在java堆中生成一个代表这个类的java. ...
- 5、web站点架构模式简介及Nginx
LB Cluster: 提升系统容量的方式: scale up:向上扩展 scale out:向外扩展 LVS工作在内核中,本身的数量不受套接字数量限制,利用LVS做调度器,优化得当的话,并发数量可以 ...
- Webpack+React项目入门——入门及配置Webpack
一.入门Webpack 参考文章:<入门Webpack,看这篇就够了> 耐心看完这篇非常有帮助 二.React+Webpack环境配置 参考文章:<webpack+react项目初体 ...
- 阿里云CentOS Linux服务器上搭建邮件服务器遇到的问题
参考文章: 阿里云CentOS Linux服务器上用postfix搭建邮件服务器 Linux系统下邮件服务器的搭建(Postfix+Dovecot) 本来想自己搭建邮件服务器,但是看到一篇资料表示阿里 ...
- AFM(3)---Maude使用说明
load file-name 1可用绝对路径 2.可进入maude文件所在目录下load 3.默认工作空间是什么?