在VBA中从派生变量设置旧变量

时间:2018-02-13 03:06:24

标签: vba ms-word word-vba

是否可以将查找和替换文本设置为新变量“path”,以便下次运行宏时,它现在会查找新路径而不是第一次查找和替换的原始路径?< / p>

Sub Code()
ActiveDocument.ActiveWindow.View.ShowFieldCodes = True

Dim oldpath As String
Dim path As String
oldpath = ActiveDocument.path
path = Replace(orgpath, "\", "\\")

Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
    .Text = "C:\\Users\\Gianni\\Desktop"
    .Replacement.Text = path
    .Forward = True
    .Wrap = wdFindContinue
    .Format = False
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll

ActiveDocument.ActiveWindow.View.ShowFieldCodes = False
End Sub

我更新了LINK代码的特定路径,以便在文件夹移动时保持对象处于活动状态。但是,如果我多次移动文件夹,上面的宏将会中断,因为它正在搜索路径A,第一个。它用路径B替换它。但是,如果我想再次移动它,搜索路径B来获取路径C,它仍在搜索路径A.

1 个答案:

答案 0 :(得分:1)

在文档中设置自定义属性以存储路径。从内存:Document.CustomProperties是您要检查和操作的集合。