0

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.

2
  • Yes, dump it somewhere in the worksheet and then just retrieve it. Commented Feb 13, 2018 at 3:23
  • Word has a number of possibilities. What's not clear is when and from where the new path comes. Could you provide more information about the circumstances for the new path? Commented Feb 13, 2018 at 5:58

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.