Microsoft Office Forums

Go Back   Microsoft Office Forums > >

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #7  
Old 12-16-2020, 01:21 PM
John 4 John 4 is offline Search-all-files-in-a-folder - Userform opens attached to wrong document Windows 10 Search-all-files-in-a-folder - Userform opens attached to wrong document Office 2013
Advanced Beginner
Search-all-files-in-a-folder - Userform opens attached to wrong document
 
Join Date: Oct 2019
Posts: 68
John 4 is on a distinguished road
Default

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
Reply With Quote
 

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Search-all-files-in-a-folder - Userform opens attached to wrong document 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
Search-all-files-in-a-folder - Userform opens attached to wrong document 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

Other Forums: Access Forums

All times are GMT -7. The time now is 03:11 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