![]() |
|
#7
|
|||
|
|||
|
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
Code:
Private Sub Button1_Continue_Click() Call MyFunction1 (x) End Sub Private Sub Button2_Exit_Click() Call MyFunction2 (x) Unload Me End Sub I’ve also retained Paul Edstein’s use of “StrDocNm” because it includes the line: Code:
If strFolder & "\" & strFile <> strDocNm Then 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
|
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
Extract Document Property and insert it into the same document, for many files in a folder
|
MisrIdley | Word VBA | 1 | 12-07-2017 12:41 PM |
| VBA Word - Search Within Files Containing A String - Copy Files to New Folder | jc491 | Word VBA | 0 | 01-09-2016 12:00 PM |
| Outlook 2010: Saving attachments opens up wrong windows folder to save in | jeroen | Outlook | 0 | 09-29-2015 01:51 AM |
Music is attached to wrong animation
|
musbegin | PowerPoint | 2 | 11-17-2014 02:40 AM |
| VBA Code in a UserForm module to delete a Command Button which opens the userform | Simoninparis | Word VBA | 2 | 09-21-2014 03:50 AM |