For the benefit of other beginners like myself who can struggle greatly with these things, here’s the full code:
Code:
Option Explicit
Public x As Integer
Sub AllFolderDocs_Search()
Application.ScreenUpdating = False
Dim strFolder As String, strFile As String, strDocNm As String, wdDoc As Document
Dim UF3 As Uform_PauseCode
strDocNm = ActiveDocument.FullName: strFolder = GetFolder
If strFolder = "" Then Exit Sub
strFile = Dir(strFolder & "\*.docx", vbNormal)
While strFile <> ""
x = 0
'Change the following <> to >= to pick up from
'where the previous search left off:
If strFolder & "\" & strFile <> strDocNm Then
Set wdDoc = Documents.Open(FileName:=strFolder & "\" & strFile, AddToRecentFiles:=False)
With wdDoc
'Call your other macro or insert its code here, i.e.:
With Selection.Find
.Text = "My search"
‘etc…
End With
Selection.Find.Execute
If Selection.Find.Found Then
Application.ScreenUpdating = True
Set UF3 = New Uform_PauseCode
UF3.Show vbModeless
Do
DoEvents
If x = 1 Then Exit Do 'and continue the search
If x = 2 Then Exit Sub
Loop
Application.ScreenUpdating = False
End If
End With
DoEvents
wdDoc.Close SaveChanges:=True
End If
strFile = Dir()
Wend
Set wdDoc = Nothing
Application.ScreenUpdating = True
End Sub
'-----------------------------
Function GetFolder() As String
Dim oFolder As Object
GetFolder = ""
Set oFolder = CreateObject("Shell.Application").BrowseForFolder(0, "Choose a folder", 0)
If (Not oFolder Is Nothing) Then GetFolder = oFolder.Items.Item.Path
Set oFolder = Nothing
End Function
'-----------------------------
Function MyFunction1(x As Integer) As Integer
x = 1
End Function
Function MyFunction2(x As Integer) As Integer
x = 2
End Function
And here’s the code for the two Userform buttons:
Code:
Private Sub Button1_Continue_Click()
Call MyFunction1 (x)
End Sub
Private Sub Button2_Exit_Click()
Call MyFunction2 (x)
Unload Me
End Sub
Call the Functions and Userform command buttons whatever you like, and you may prefer to use a less common variable name than “x” seeing as it’s a public variable (but x will still work fine). Greg will notice that I’ve retained the Selection object for the search rather than use the Range object. I think beginners like myself will usually find the Selection object much easier to work with because you can see what’s going on if you want to step through the code with the F8 key.
I’ve also retained Paul Edstein’s use of “StrDocNm” because it includes the line:
Code:
If strFolder & "\" & strFile <> strDocNm Then
If you want to continue the search from where you left off, then have that document (which contained the successful search when you exited the code) as the Active document when you run the code again, and change the <> in the above line to >=
Greg, Paul, Andrew, or Greg Mayor could probably provide a few lines of code to determine whether the active document is within the search folder and ask the user if he wants to continue from there or search the entire folder. But as it is, it’s not too big a deal to make the above small change to the code instead.
Happy Christmas