Thread: Batch re-naming
View Single Post
 
Old 06-16-2015, 06:01 AM
macropod's Avatar
macropod macropod is offline Windows 7 64bit Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 22,373
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

OK, so you have a badly-formatted document, with paragraph breaks instead of line breaks between consecutive lines, and yet more paragraph breaks for inter-paragraph spacing, along with pointless tabs!

Given that scenario, you could use something like:
Code:
Sub PolicyDocs()
Application.ScreenUpdating = False
Dim strFolder As String, strFile As String, StrName As String, wdDoc As Document
strFolder = GetFolder
If strFolder = "" Then Exit Sub
strFile = Dir(strFolder & "\*.doc", vbNormal)
While strFile <> ""
  If Not strFile Like "Policy[*].doc" Then
    StrName = ""
    Set wdDoc = Documents.Open(FileName:=strFolder & "\" & strFile, AddToRecentFiles:=False, Visible:=False)
    With wdDoc
      With .Range
        With .Find
          .ClearFormatting
          .Text = "Policy[!^13]{1,}"
          .MatchWildcards = True
          .Forward = True
          .Wrap = wdFindStop
          .Execute
        End With
        If .Find.Found Then StrName = .Text
      End With
      If StrName <> "" Then .SaveAs FileName:=.Path & "\" & StrName & ".doc"
      .Close
    End With
  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
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote