Microsoft Office Forums

Go Back   Microsoft Office Forums > >

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #1  
Old 04-04-2018, 05:51 PM
rmk911 rmk911 is offline VBA code to run macro across all documents in folder Windows 7 64bit VBA code to run macro across all documents in folder Office 2007
Novice
VBA code to run macro across all documents in folder
 
Join Date: Apr 2018
Posts: 7
rmk911 is on a distinguished road
Default VBA code to run macro across all documents in folder

Hi there

This is a 3 part question. Also, FYI, I'm completely new to VBA, and am teaching myself as I go on, so all the code below is things I've found and pieced together from the internet.

Note: Part 3 is what I'm really after, and Parts 1+2 splits this up, assuming that Part 3 can't be achieved.

Part 1
I am trying to identify strings of text, whether they be in tables, footnotes, or in the paragraphs in a document, and convert them to a hyperlink. The code I have to do this is as follows:

Sub ConvertHyperlink()

'Set up search
With Selection.Find
.ClearFormatting
.Text = "([A-Za-z0-9]{1,5}).([0-9]{1,5}).([0-9]{1,5}).([0-9_]{1,})"
.Forward = True
.Wrap = wdFindAsk
.MatchWildcards = True
End With

' Find next instance of Pattern "([A-Za-z0-9]{1,5}).([0-9]{1,5}).([0-9]{1,5}).([0-9_]{1,})"
Selection.Find.Execute

' Replace it with a hyperlink
ActiveDocument.Hyperlinks.Add Anchor:=Selection.Range, _
Address:="Documents/" & Selection.Text, _
TextToDisplay:=Selection.Text

End Sub

This works, except it stops after the first one, and I need to hold down F5 to make it keep going.

Goal 1: I want to make this code execute until it reaches the end of the document.

Part 2
Keeping the above in mind, I want to execute this code across all word documents in a folder.

Goal 2: I want the code in Part 1 to run until it reaches the end of the first document, and then goes on to repeat the same process for every document in a specified folder, such as "E:\Folder".

Part 3
My ultimate goal is to find and replace all strings of text, and convert them to a corresponding hyperlink, depending on the string.

For example, string1 should point to www.google.com, so I was planning on first running the vba code outlined in Parts 1 and 2 to convert the string to a hyperlink, and then I have another set of code to then convert this hyperlink to its corresponding website, as per a table which lists what the string is, and what its corresponding website should be.

The code I have for this is:

Sub ReplaceFromTableList()
' from Doug Robbins, Word MVP, Microsoft forums, Feb 2015, based on another macro written by Graham Mayor, Aug 2010
Dim oChanges As Document, oDoc As Document
Dim oTable As Table
Dim oRng As Range
Dim rFindText As Range, rReplacement As Range
Dim i As Long
Dim sFname As String
'Change the path in the line below to reflect the name and path of the table document
sFname = "E:\Folder\TABLE.docx"
Set oDoc = ActiveDocument
Set oChanges = Documents.Open(FileName:=sFname, Visible:=False)
Set oTable = oChanges.Tables(1)


For i = 1 To oTable.Rows.Count
Set oRng = oDoc.Range
Set rFindText = oTable.Cell(i, 1).Range
rFindText.End = rFindText.End - 1
Set rReplacement = oTable.Cell(i, 2).Range
rReplacement.End = rReplacement.End - 1
Selection.HomeKey wdStory
With oRng.Find
.ClearFormatting
.Replacement.ClearFormatting
.MatchWildcards = True
.Text = rFindText.Text
.Replacement.Text = rReplacement.Text
.Forward = True
.Wrap = wdFindContinue
.Execute Replace:=wdReplaceAll
End With
Next i
oChanges.Close wdDoNotSaveChanges

End Sub

Here, E:\Folder\TABLE.docx is the document containing the table.

Goal 3: To convert strings to hyperlinks, depending on a table located in a word document showing the string to be converted, and what the hyperlink should be. This code should run until the end of the document, for all documents in the folder.

Any help, even if it just for the first part, is much appreciated.

Thanks
rmk911
Reply With Quote
 



Similar Threads
Thread Thread Starter Forum Replies Last Post
VBA Code to copy file to folder and keep the original. staicumihai Word VBA 3 10-31-2016 03:07 AM
VBA code to run macro across all documents in folder VBA macro code to remove BMP, text and seal from documents mach_9 Word VBA 5 09-28-2016 03:19 PM
VBA code to run macro across all documents in folder How to open Documents folder directly from CTRL+O of Open folder on QAT scvjudy Word 2 08-11-2014 10:58 PM
Office 2010 Can't Open Or Save Documents in My Documents Folder trippb Office 1 07-12-2013 07:29 AM
Macro code should find inv no in folder and send attachhed mail. visha_1984 Outlook 0 01-30-2013 05:08 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 02:02 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft