Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 01-25-2017, 10:10 AM
Jude24Joy Jude24Joy is offline Combining 3 Modules into 1 Windows 8 Combining 3 Modules into 1 Office 2013
Novice
Combining 3 Modules into 1
 
Join Date: Dec 2016
Posts: 15
Jude24Joy is on a distinguished road
Default Combining 3 Modules into 1

Hello!

I have 3 modules that I run on .doc* files in succession. I would love to be able to run them all at once. For the first two, I select files in a folder, and for the third, I select the folder itself. Either method would be fine.

The first two modules are actually the same, but the find/replace text is different. The third module removes author information and hyperlinks.

I'm going to copy them here.



Can these be combined in some way?

Thanks in advance!


First module:

Code:
Sub CommandButton1_Click() 
Dim MyDialog As FileDialog, GetStr(1 To 300) As String '300 files is the maximum applying this code
On Error Resume Next 
Set MyDialog = Application.FileDialog(msoFileDialogFilePicker) 
With MyDialog 
  .Filters.Clear 
  .Filters.Add "All WORD File ", "*.doc*", 1 
  .AllowMultiSelect = True 
  i = 1 
  If .Show = -1 Then 
    For Each stiSelectedItem In .SelectedItems 
      GetStr(i) = stiSelectedItem 
      i = i + 1 
    Next 
    i = i - 1 
  End If 
  Application.ScreenUpdating = False 
  For j = 1 To i Step 1 
    Set Doc = Documents.Open(FileName:=GetStr(j), Visible:=True) 
    Windows(GetStr(j)).Activate 
    Selection.Find.ClearFormatting 
    Selection.Find.Replacement.ClearFormatting 
    With Selection.Find 
      .Text = "[text to replace here 1]" 
      .Replacement.Text = "[replacement text here 1]" 
      .Forward = True 
      .Wrap = wdFindAsk 
      .Format = False 
      .MatchCase = False 
      .MatchWholeWord = False 
      .MatchByte = True 
      .MatchWildcards = False 
      .MatchSoundsLike = False 
      .MatchAllWordForms = False 
    End With 
    Selection.Find.Execute Replace:=wdReplaceAll 
    Application.Run macroname:="NEWMACROS" 
    ActiveDocument.Save 
    ActiveWindow.Close 
  Next 
  Application.ScreenUpdating = True 
End With 
MsgBox "operation end, please view", vbInformation 
End Sub
Module 2:
Code:
Sub CommandButton2_Click() 
Dim MyDialog As FileDialog, GetStr(1 To 300) As String '300 files is the maximum applying this code
On Error Resume Next 
Set MyDialog = Application.FileDialog(msoFileDialogFilePicker) 
With MyDialog 
  .Filters.Clear 
  .Filters.Add "All WORD File ", "*.doc*", 1 
  .AllowMultiSelect = True 
  i = 1 
  If .Show = -1 Then 
    For Each stiSelectedItem In .SelectedItems 
      GetStr(i) = stiSelectedItem 
      i = i + 1 
    Next 
    i = i - 1 
  End If 
  Application.ScreenUpdating = False 
  For j = 1 To i Step 1 
    Set Doc = Documents.Open(FileName:=GetStr(j), Visible:=True) 
    Windows(GetStr(j)).Activate 
    Selection.Find.ClearFormatting 
    Selection.Find.Replacement.ClearFormatting 
    With Selection.Find 
      .Text = "[text to replace here 2]" 
      .Replacement.Text = "[replacement text here 2]" 
      .Forward = True 
      .Wrap = wdFindAsk 
      .Format = False 
      .MatchCase = False 
      .MatchWholeWord = False 
      .MatchByte = True 
      .MatchWildcards = True 
      .MatchSoundsLike = False 
      .MatchAllWordForms = False 
    End With 
    Selection.Find.Execute Replace:=wdReplaceAll 
    Application.Run macroname:="NEWMACROS" 
    ActiveDocument.Save 
    ActiveWindow.Close 
  Next 
  Application.ScreenUpdating = True 
End With 
MsgBox "operation end, please view", vbInformation 
End Sub
Module 3:
Code:
Sub UpdateDocuments() 
Application.ScreenUpdating = False 
Dim strFolder As String, strFile As String, strDocNm As String, wdDoc As Document 
strDocNm = ActiveDocument.FullName 
strFolder = GetFolder 
If strFolder = "" Then Exit Sub 
strFile = Dir(strFolder & "\*.doc") 
While strFile <> "" 
  If strFolder & "\" & strFile <> strDocNm Then 
    Set wdDoc = Documents.Open(FileName:=strFolder & "\" & strFile, AddToRecentFiles:=False, Visible:=False) 
    With wdDoc 
      .Fields.Unlink 
      .RemoveDocumentInformation (wdRDIAll) 
      .Close SaveChanges:=True 
    End With 
  End If 
  strFile = Dir() 
Wend 
Set wdDoc = Nothing 
Application.ScreenUpdating = True
End Sub

Last edited by macropod; 01-25-2017 at 02:20 PM. Reason: Added code tags & formatting
Reply With Quote
  #2  
Old 01-25-2017, 02:30 PM
macropod's Avatar
macropod macropod is offline Combining 3 Modules into 1 Windows 7 64bit Combining 3 Modules into 1 Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Merging those three macros is a simple undertaking:
Code:
Sub UpdateDocuments()
Application.ScreenUpdating = False
Dim strFolder As String, strFile As String, strDocNm As String, wdDoc As Document
strDocNm = ActiveDocument.FullName
strFolder = GetFolder
If strFolder = "" Then Exit Sub
strFile = Dir(strFolder & "\*.doc")
While strFile <> ""
  If strFolder & "\" & strFile <> strDocNm Then
    Set wdDoc = Documents.Open(FileName:=strFolder & "\" & strFile, AddToRecentFiles:=False, Visible:=False)
    With wdDoc
      With .Range.Find
        .ClearFormatting
        .Replacement.ClearFormatting
        .Forward = True
        .Wrap = wdFindAsk
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchByte = True
        .MatchWildcards = True
        .MatchSoundsLike = False
        .MatchAllWordForms = False
        .Text = "[text to replace here 1]"
        .Replacement.Text = "[replacement text here 1]"
        .Execute Replace:=wdReplaceAll
        .Text = "[text to replace here 2]"
        .Replacement.Text = "[replacement text here 2]"
        .Execute Replace:=wdReplaceAll
      End With
      Application.Run MacroName:="NEWMACROS"
      .Fields.Unlink
      .RemoveDocumentInformation (wdRDIAll)
      .Close SaveChanges:=True
    End With
  End If
  strFile = Dir()
Wend
Set wdDoc = Nothing
Application.ScreenUpdating = True
End Sub
If you want to run the code from your userform, change:
Sub UpdateDocuments()
to:
Sub CommandButton1_Click()

Note: it's impossible to know for sure where:
Application.Run MacroName:="NEWMACROS"
should go as I have no idea what that code is or what it is supposed to do. Furthermore, whatever service it provides could undoubtedly be incorporated into the same macro.

PS: When posting code, please use the code tags, indicated by the # button on the posting menu. Without them, your code loses much of whatever structure it had.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #3  
Old 01-26-2017, 07:54 AM
Jude24Joy Jude24Joy is offline Combining 3 Modules into 1 Windows 8 Combining 3 Modules into 1 Office 2013
Novice
Combining 3 Modules into 1
 
Join Date: Dec 2016
Posts: 15
Jude24Joy is on a distinguished road
Default

Thank you so much. This is amazing!

And thank you for fixing my message with the code tags. I'll try to do it right next time.

Reply With Quote
  #4  
Old 01-27-2017, 09:24 AM
Jude24Joy Jude24Joy is offline Combining 3 Modules into 1 Windows 8 Combining 3 Modules into 1 Office 2013
Novice
Combining 3 Modules into 1
 
Join Date: Dec 2016
Posts: 15
Jude24Joy is on a distinguished road
Default

One more question...

I am getting multiple dialog boxes that say "Word has reached the end of the document. # replacements were made. Do you want to continue searching at the beginning?"

Is there a setting on one of the original modules that prevented that?

Thanks!
Reply With Quote
  #5  
Old 01-27-2017, 02:33 PM
macropod's Avatar
macropod macropod is offline Combining 3 Modules into 1 Windows 7 64bit Combining 3 Modules into 1 Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

I coded that way because your own code had that stipulation. You could change:
wdFindAsk
to:
wdFindContinue
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #6  
Old 02-01-2017, 04:20 PM
Jude24Joy Jude24Joy is offline Combining 3 Modules into 1 Windows 8 Combining 3 Modules into 1 Office 2013
Novice
Combining 3 Modules into 1
 
Join Date: Dec 2016
Posts: 15
Jude24Joy is on a distinguished road
Default

That worked! Thank you!
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Win10 Outlook keyboard error reduced all text all modules progdram wide markg2 Outlook 1 05-10-2016 08:17 AM
Combining 3 Modules into 1 Declaring a variable that is known across modules Officer_Bierschnitt Excel Programming 5 11-18-2015 12:28 PM
Combining 3 Modules into 1 Modelling training modules Vincent Project 3 09-03-2014 05:37 PM
Combining 3 Modules into 1 How do I stop making duplicates vba modules? chrisd2000 Excel Programming 1 06-28-2014 11:33 AM
Combining 3 Modules into 1 Empty Modules Greg S. Excel Programming 2 07-30-2013 01:38 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 09:20 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft