|
楼主 |
发表于 2019-4-18 10:13
|
显示全部楼层
经过各种录制宏和各种百度,我已经基本完成了这个需求,再此分享一下,也请各位大佬看看有没有什么问题。
Private Sub Document_Open()
Dim wdPath As String, myText As String
Selection.EscapeKey
Selection.WholeStory '全选
Selection.Fields.ToggleShowCodes 'Shift+F9
Selection.Find.ClearFormatting
With Selection.Find
.Text = "LINK"
.Wrap = wdFindContinue
End With
Selection.Find.Execute
a = Selection.End
Selection.Find.ClearFormatting
With Selection.Find
.Text = ".xlsm"
.Wrap = wdFindContinue
End With
Selection.Find.Execute
b = Selection.Start
Selection.Start = a + 1
Selection.End = b
Selection.Copy
myText = Selection.Text '选中LINK到.xlsm间的内容并赋值给myText
wdPath = ActiveDocument.Path ‘获取当前路径
wdPath = Replace(wdPath, "\", "\\") ’单\变为双\\
wdPath = "Excel.Sheet.12 " & Chr(34) & wdPath & "\\Digital Calculation" '将word的当前路径赋值给wdPath,后缀变成excel的名称
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = myText
.Replacement.Text = wdPath
.Forward = True
.Wrap = wdFindAsk
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchByte = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll '查找myText替换为wdPath
Selection.WholeStory '全选
Selection.Fields.ToggleShowCodes 'Shift+F9
Selection.EscapeKey
End Sub
|
|