Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 12-16-2020, 03:36 PM
gmaxey gmaxey 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 2016
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,617
gmaxey is just really nicegmaxey is just really nicegmaxey is just really nicegmaxey is just really nicegmaxey is just really nice
Default

John,




Personally, I think the sooner you learn to use ranges vice selection, the better off you will be. Whichever you use, in my experience, just "If Selection.Find.Execute ..." works just as well as "Selection.Find.Execute followed by If Selection.Find.Found ..."


The latter for some reason is a practice of Paul's which I don't understand but also feel he will never change. To each his own. You don't need the public variable "x" at all or the two functions. Just use the form tag property:

Code:
Sub AllFolderDocs_Search()
Dim strFolder As String, strFile As String, strDocNm As String, wdDoc As Document
Dim oFrm As Uform_PauseCode
  Application.ScreenUpdating = False
  strDocNm = ActiveDocument.FullName
  strFolder = GetFolder
  If strFolder = "" Then Exit Sub
  strFile = Dir(strFolder & "\*.doc*", vbNormal)
  While strFile <> ""
    '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 Selection.Find
        .Text = "Dogs"
        If .Execute Then
          Application.ScreenUpdating = True
          Set oFrm = New Uform_PauseCode
          With oFrm
            .Show vbModeless
            Do
              DoEvents
              Select Case .Tag
                Case Is = "Continue"
                  Unload oFrm
                  Exit Do
                Case Is = "Exit"
                  Unload oFrm
                  GoTo lbl_Exit
              End Select
            Loop
          End With
          Application.ScreenUpdating = False
        End If
      End With
      DoEvents
      wdDoc.Close SaveChanges:=True
    End If
    strFile = Dir()
  Wend

 lbl_Exit:
  Set oFrm = Nothing: Set wdDoc = Nothing
  Application.ScreenUpdating = True
   Exit Sub
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
lbl_Exit:
  Exit Function
End Function
and:


Code:
Option Explicit
Private Sub Button1_Continue_Click()
  Tag = "Continue"
lbl_Exit:
  Exit Sub
End Sub

Private Sub Button2_Exit_Click()
  Tag = "Exit"
lbl_Exit:
  Exit Sub
End Sub
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
  If CloseMode = 0 Then
    Cancel = True
    Button2_Exit_Click
  End If
lbl_Exit:
  Exit Sub
End Sub
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
Reply



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 04:46 AM.


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