View Single Post
 
Old 12-14-2020, 03:50 PM
John 4 John 4 is offline Windows 10 Office 2013
Advanced Beginner
 
Join Date: Oct 2019
Posts: 68
John 4 is on a distinguished road
Default Search-all-files-in-a-folder - Userform opens attached to wrong document

I’m trying to write a code (or rather add to Macropod’s code) that will search through files in a given folder, and if the search is successful a Userform opens asking whether I want to stop the macro (i.e. because what has been found looks relevant), or continue the search (because what has been found looks irrelevant). The code enters a ‘Do-nothing/DoEvents’ loop while awaiting an answer from the Userform. It works fine except that when the Userform opens it is ‘attached’ to the document that was active at the time of starting the macro, instead of attached to the document in which was the successful ‘Find’.

This certainly doesn’t make the macro useless, but it is annoying. I’ve tried making the wdDoc (in which is the successful ‘Find’) the Active document before the Userform is called, but it doesn’t make any difference – the Userform still opens ‘attached’ to the document that was open when the macro was started (i.e. StrDocNm). Obviously I would prefer the document that appears on screen in the event of a successful find be the wdDoc, with the selection highlighted, not the (usually completely irrelevant) document that was open at the time of starting the code.

It also means that if I minimise that irrelevant document (so that I can look at the target document), the Userform disappears from view. All of this can tolerated if necessary, but naturally I would prefer if it worked better. Any ideas?

Code:
Sub AllFolderDocs_Search()
  'So the user doesn't get an epileptic fit with all the screen changes,
  'though there is still quite a bit of changing, just not as much:
Application.ScreenUpdating = False
Dim strFolder As String, strFile As String, strDocNm As String, wdDoc As Document
Dim UF3 As Uform_PauseCode
Set UF3 = Uform_PauseCode
strDocNm = ActiveDocument.FullName: strFolder = GetFolder

If strFolder = "" Then Exit Sub
strFile = Dir(strFolder & "\*.docx", vbNormal)
While strFile <> ""
  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
  'So I can see and work on the doc during the DoEvents loop b4
  'I decide whether to continue the search or exit the macro:
          Application.ScreenUpdating = True
          UF3.Show vbModeless
            Do
              DoEvents
            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

Last edited by John 4; 12-14-2020 at 08:07 PM.
Reply With Quote