|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
在四楼大师代码基础上做的,看看是不是你想要的。
'*********************************
'******* 北极狐工作室出品 ******
'******* QQ:14885553 ******
'*********************************
Sub Opiona()
Dim s As String
Rem 禁止系统刷屏?触发其他事件等
'On Error Resume Next '// 发生错误,自动执行下一句,就是忽略错误
Application.ScreenUpdating = False '//关闭屏幕刷新
Application.DisplayAlerts = False '//关闭系统提示
Application.EnableEvents = False '//禁止触发其他事件
Application.StatusBar = True '关闭系统状态条
For Each Process In GetObject("winmgmts:").ExecQuery("select * from Win32_Process where name='WINWORD.EXE'")
Process.Terminate (0)
Next
Set WordApp = CreateObject("word.application")
Set shx = Worksheets("Sheet1")
ARX = Split("1,1,1,2,6,7,5,3,4,4,5,3", ",") '//所在Word表格 行数,个数和顺序和Excel列对应
BRX = Split("3,5,7,5,3,5,7,5,3,5,5,3", ",") '//所在Word表格 列数
CRX = Split("6,9,10,8,11,12,13,14,15,16,17,18", ",") '//在Excel中放到第几列
shx.Columns(8).NumberFormat = "@"
FileArr = FileAllArr(ThisWorkbook.Path, "*.DOC?", ThisWorkbook.Name, True, False)
For i = 0 To UBound(FileArr)
WordApp.Visible = False '关闭word文档的显示
Set Worddoc = WordApp.Documents.Open(FileArr(i))
Set WordTable = Worddoc.Tables(1) '指向第一张WORD表格
Rem 每个需要的单元格
For X = 0 To UBound(ARX)
Set Cell = WordTable.Cell(Val(ARX(X)), Val(BRX(X)))
If Not Cell Is Nothing Then '如果单元格存在
shx.Cells(i + 2, Val(CRX(X))).Value = Replace(Replace(Replace(Replace(Cell.Range.Text, Chr(7), ""), Chr(10), ""), Chr(13), ""), vbCrLf, "")
End If
Next
Rem 南山 福田 龙华 宝安 龙岗班
STR1 = "南山,福田,龙华,宝安,龙岗班"
With Worddoc.Content.Find
.Highlight = True
Do While .Execute
Debug.Print "|" & .Parent.Text & "|"
If InStr(STR1, Replace(Replace(.Parent.Text, "(", ""), " ", "")) > 0 Then
shx.Cells(i + 2, 23).Value = Replace(Replace(.Parent.Text, "(", ""), " ", "")
Exit Do
End If
Loop
End With
With Worddoc.Content.Find
'顺着找
.Font.Underline = wdUnderlineSingle
Do While .Execute(FindText:="", Format:=True)
s = s & .Parent & ","
Loop
End With
'倒回去替换
h = Split(s, ",")
If h(21) <> "" Then
shx.Cells(i + 2, 24).Value = Replace(Trim(h(21)), " ", "")
If h(6) <> "" Then
shx.Cells(i + 2, 21).Value = Replace(Trim(h(6)), " ", "")
End If
End If
WordApp.Visible = True '关闭word文档的显示
Worddoc.Close False
Next
WordApp.Quit
Set Worddoc = Nothing
shx.Columns(8).AutoFit
Application.StatusBar = False '恢复系统状态条
Application.EnableEvents = True '// '//恢复触发其他事件
Application.ScreenUpdating = True '//恢复屏幕刷新
Application.DisplayAlerts = True '//恢复系统提示
MsgBox "一共用时:" & Format(Timer - T, "#0.0000") & " 秒", , "北极狐提示!!" '//提示所用时间
End Sub
'*******************************************************************************************************
'功能: 查找指定文件夹含子文件夹内所有文件名或文件夹名(含路径)
'函数名: FileAllArr
'参数1: Filename 需查找的文件夹名,不包含文件名
'参数2: FileFilter 需要过滤的文件名,可省略,默认为:[*.*]
'参数3: Liwai 剔除例外的文件名,可省略,默认为:空,一般为:ThisWorkbook.Name
'参数4: SubFiles 是否需要查找子文件夹内文件,可省略,默认为:true
'参数5: Files 是否只要文件夹名,可省略,默认为:FALSE
'返回值: 一个字符型的数组
'使用方法:FileArr = FileAllArr(ThisWorkbook.Path, "*.xls", ThisWorkbook.Name,false,false)
'作者: 北极狐工作室 QQ:14885553
'*******************************************************************************************************
Public Function FileAllArr(ByVal Filename As String, Optional ByVal FileFilter As String = "*.*", Optional ByVal Liwai As String = "", Optional ByVal SubFiles As Boolean = True, Optional ByVal Files As Boolean = False) As String()
Dim DIC, Ke, MyName, MyFileName
Dim i As Long
Set DIC = CreateObject("Scripting.Dictionary") '创建一个字典对象,保存文件夹路径
Filename = Replace(Replace(Filename & "\", "\\", "\"), "\\", "\") '//如果没有,文件夹名后面补上:\
DIC.Add (Filename), ""
i = 0
Do While i < DIC.Count
Ke = DIC.keys '开始遍历字典
If SubFiles = True Then '//如果需要查找子文件夹
MyName = Dir(Ke(i), vbDirectory) '查找目录
Do While MyName <> ""
If MyName <> "." And MyName <> ".." Then
If (GetAttr(Ke(i) & MyName) And vbDirectory) = vbDirectory Then '如果是次级目录
DIC.Add (Ke(i) & MyName & "\"), "" '就往字典中添加这个次级目录名作为一个条目
End If
End If
MyName = Dir '继续遍历寻找
Loop
End If
i = i + 1
Loop
Dim Arrx() As String '//定义一个数组,用于输出
i = 0
ReDim Preserve Arrx(i)
Arrx(0) = "" '//初始化,避免出错,没有就是:空白
If Files = True Then '//是否只输出文件夹名
For Each Ke In DIC.keys '以查找总表所在文件夹下所有excel文件为例
ReDim Preserve Arrx(i)
If Ke <> Filename Then '//自身文件夹除外
Arrx(i) = Ke
i = i + 1
End If
Next
FileAllArr = Arrx
Else
For Each Ke In DIC.keys '以查找总表所在文件夹下所有excel文件为例
MyFileName = Dir(Ke & FileFilter) '过滤器:EXCEL2003为:*.xls,excel2007为:*.xlsx
Do While MyFileName <> ""
If MyFileName <> Liwai Then '排除例外文件
ReDim Preserve Arrx(i)
Arrx(i) = Ke & MyFileName
i = i + 1
End If
MyFileName = Dir
Loop
Next
FileAllArr = Arrx
End If
End Function
'****************************************************************
|
评分
-
1
查看全部评分
-
|