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的更多相关文章
随机推荐
- weblogic10以下,许可证过期解决办法
weblogic10以后的版本已经不再使用license.bea的方式来进行软件授权,之前的历史版本oracle提供了一个免费的许可证更新. 1.首先进入oracle的官网下载地址http://www ...
- Installing Jenkins as a Windows service
Install Jenkins as a Windows service NOTE: if you installed Jenkins using the windows installer, you ...
- 【配置详解】Quartz配置文件详解
我们通常是通过quartz.properties属性配置文件(默认情况下均使用该文件)结合StdSchedulerFactory 来使用Quartz的.StdSchedulerFactory 会加载属 ...
- it做形式主语的句子
1. it was considerate of you to visit my mother every day and (to) bring me your notes to help me wi ...
- .psl脚本介绍
.ps1文件是PowerShell写好的脚本文件 可以在记事本中写一段PowerShell代码,然后将其保存为“xxx.ps1”,后面要使用它的时候,双击即可运行了.这有点像批处理的“.bat”文件, ...
- 【转载】谈谈自己对REST、SOA、SOAP、RPC、ICE、ESB、BPM知识汇总及理解
转载自:https://blog.csdn.net/tantexian/article/details/48196453 SOA: 维基百科解释:SOA:面向服务的软件架构(Service Orien ...
- 1、Python中的正则表达式(0601)
回顾: 1.文件对象: open('file','mode','bufsize') read,readline,readlines,write,writelines,flush,seek,tell 2 ...
- HDU 4825 Xor Sum(01字典树入门题)
http://acm.hdu.edu.cn/showproblem.php?pid=4825 题意: 给出一些数,然后给出多个询问,每个询问要从之前给出的数中选择异或起来后值最大的数. 思路:将给出的 ...
- 进度条的制作-python
import time,sys def view_bar(num, total): rate = float(num) / float(total) rate_num = int(rate * 100 ...
- SSH KEY 设置 目录在open ~ 根目录下的.ssh 里面
当我们从github或者gitlab上clone项目或者参与项目时,需要证明我们的身份.github.gitlab支持使用SSH协议进行免密登录,而SSH协议采用了RSA算法保证了登录的安全性.我们要 ...