Gianni HIll
Gianni HIll

Reputation: 43

Set old variable from a derived variable in VBA

Would it be possible to set the find and replace text to whatever the new variable "path" is so that next time the macro runs it now looks for the new path instead of the original path from the first find and replace?

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

I update a specific path of LINK codes in order to keep the objects active when the folder gets moved around. However, if I move the folder more than once, the macro above would break because it is searching for path A, the first. It replaces it with path B. But, if I want to move it again, searching for path B to get path C, it is still searching for path A.

Upvotes: 0

Views: 44

Answers (1)

AJD
AJD

Reputation: 2438

Set a Custom Property in the document to store the path. From memory: Document.CustomProperties is the collection you want to check and manipulate.

Upvotes: 1

Related Questions