|
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件 ★ 免费下载 ★ ★ 使用帮助★
请教各位大咖一个问题?
一篇WORD文档,里面包括了一些发规条文,这些发规条文放在一个文件夹中,
文件中的法规文档采用了年度#文号#标题的方式命名,可不可以在WORD文档
中批量对文档里有的文号或者标题的而法规文件夹具有相同文号或者标题的文档建立超链接?
为什么以下代码在word中就行,但通过EXCEL调用word就生成不了?
Sub test2()
Dim aSec As Section
Dim mystr As String
Dim n As Integer
Dim myMatches As Object
Dim Match As Object
Dim a() As String
Dim i As Long
mystr = InputBox("请输入要搜索的字符,不能为空!")
If mystr = Empty Then Exit Sub
For Each aSec In ActiveDocument.Sections
With CreateObject("vbscript.regexp")
.Global = True
.Pattern = "[\da-zA-Z\u4e00-\u9f5a]*" & mystr & "[\da-zA-Z\u4e00-\u9f5a]*"
Set myMatches = .Execute(aSec.Range.Text)
End With
n = n + myMatches.Count
ReDim Preserve a(1 To n)
For Each Match In myMatches
i = i + 1
Selection.Find.ClearFormatting
With Selection.Find
.Text = mystr
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchByte = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
ChangeFileOpenDirectory "C:\Users\HP\Desktop\求助\求助\"
ActiveDocument.Hyperlinks.Add Anchor:=Selection.Range, Address:= _
"法规文件/2015%5e财税〔2015〕119%5e财政部、国家税务总局、科技部关于完善研究开发费用税前加计扣除政策的通知.docx", _
SubAddress:="", ScreenTip:="", TextToDisplay:=""
Next Match
Next
End Sub
但通过EXCEL调用就不行了
Sub 生成链接2()
Dim MyRange As Excel.Range
Dim WdRange As Word.Range
Dim i As Long
Dim r As Long
Dim aSec As Word.Section
Dim mystr As String
Dim n As Integer
Dim myMatches As Object
Dim Match As Object
Dim a() As String
Dim w As Long
Dim mypath As String
On Error Resume Next
Dim Wd
x = MsgBox("确定生成链接吗?", [vbOKCancel])
If x = 2 Then Exit Sub
On Error Resume Next
mypath = ThisWorkbook.Path
Set Wd = CreateObject("word.application")
'With Word对象
'.Documents.Open (ThisWorkbook.Path & "\" & "文档.docx")
'.Visible = True
'End With
'打开Word文档
r = Range("a1").End(xlDown).Row
arr1 = Range("A1:A" & r)
arr2 = Range("B1:B" & r)
arr3 = Range("c1:c" & r)
For i = 1 To UBound(arr1)
x1 = arr1(i, 1) & ""
mystr = arr2(i, 1) & ""
Z1 = arr3(i, 1) & ""
n1 = "\法规文件\" & x1 & "%5e" & mystr & "%5e" & Z1
'If y1 = Empty Then Exit Sub
For Each aSec In ActiveDocument.Sections
'With Wd
'.Documents.Open (mypath & "\" & "4.docx")
' .Visible = True
'.Windows.Application.Visible = True
'.Windows.Application.Selection.Copy
With CreateObject("vbscript.regexp")
.Global = True
.Pattern = "[\da-zA-Z\u4e00-\u9f5a]*" & mystr & "[\da-zA-Z\u4e00-\u9f5a]*"
Set myMatches = .Execute(aSec.Range.Text)
End With
n = n + myMatches.Count
ReDim Preserve a(1 To n)
For Each Match In myMatches
w = w + 1
Selection.Find.ClearFormatting
With Selection.Find
.Text = mystr
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchByte = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
ChangeFileOpenDirectory "C:\Users\HP\Desktop\求助\求助\"
ActiveDocument.Hyperlinks.Add Anchor:=Selection.Range, Address:= _
"法规文件/2015%5e财税〔2015〕119%5e财政部、国家税务总局、科技部关于完善研究开发费用税前加计扣除政策的通知.docx", _
SubAddress:="", ScreenTip:="", TextToDisplay:=""
Next Match
Next
Next
Wd.Documents.Save
Wd.Quit
Set Wd = Nothing
MsgBox "链接生成完毕!"
End Sub
|
|