![]() |
#1
|
|||
|
|||
![]()
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. |
|
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
![]() |
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 |
![]() |
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 |