Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Loop Through a folder, and clear all word documents formatting from Excel.

Status
Not open for further replies.

umxir

Technical User
Oct 21, 2018
9
PK
Hello Fellows,

I want to Loop through a folder and remove all formatting of MS Word documents from Excel.

I am using below attached code, but it's not working. I am stuck with actual command execution.

Please help me in this regards

Thanks



Code:
Sub clrfmt()
Dim wdapp As New Word.Application
Dim myDoc As Word.Document
Dim myFolder As String, strFile As String

With Application.FileDialog(msoFileDialogFolderPicker)
    .Title = "Please select a folder"
    .Show
    .AllowMultiSelect = False
Tha            If .SelectedItems.Count = 0 Then 'If no folder is selected, abort
            MsgBox "You did not select a folder"
            Exit Sub
            End If
    myFolder = .SelectedItems(1) & "\" 'Assign selected folder to MyFolder
End With

Application.ScreenUpdating = False

strFile = Dir(myFolder & "\*.doc", vbNormal)

While myFile <> ""
        
    Set myDoc = wdapp.Documents.Open(Filename:=myFolder & "\" & strFile, AddToRecentFiles:=False, Visible:=False)
        
        With myDoc
            
            ActiveDocument.Select
            Selection.ClearFormatting
            
            
        End With
        
    myDoc.Close SaveChanges:=True
    strFile = Dir()
Wend

wdapp.Quit
Set myDoc = Nothing: Set wdapp = Nothing: Set myWkSht = Nothing
Application.ScreenUpdating = True
 
End Sub
 
Code:
            ActiveDocument.Select
            Selection.ClearFormatting

Formatting does not apply to a document.

It applies to Range objects.

And possibly Story objects, which are sort of predefined meta-ranges...

Also, using Select simply slows things down by mucking around with the GUI. You don't need to do that. Work with objects directly.

 
Thanks mintjulep for your reply,

I am sorry, I don't get what you are saying. (New to Codes)
Moreover, the code you are provided already exists in the code. It is really helpful if you can modify the code for me.

Thanks
 
Doing this from memory. I think that EntireStory descroes the Range of the whole document, but I could be wrong. And I’m not sure there’s ClearFormatting...
Code:
While myFile <> ""
        
    Set myDoc = wdapp.Documents.Open(Filename:=myFolder & "\" & strFile, AddToRecentFiles:=False, Visible:=False)
        
    myDoc.EntireStory.ClearFormatting
        
    myDoc.Close SaveChanges:=True
    strFile = Dir()
Wend

Skip,
[sub]
[glasses]Just traded in my OLD subtlety...
for a NUance![tongue][/sub]
 
There is no 'EntireStory' property or 'ClearFormatting' method for it in Word. There is a 'WholeStory' property, but even that has no 'ClearFormatting' method.

In any event, there is no way to remove all formatting from a Word document unless you save it as a plain text file - in which case it's no longer a Word document. The most you can do in Word is to apply a single Style to all the content and remove any direct formatting. Hence:
Code:
While myFile <> ""
  Set myDoc = wdapp.Documents.Open(FileName:=myFolder & "\" & strFile, AddToRecentFiles:=False, Visible:=False)
  With myDoc
    .Range.Style = wdStyleNormal
    .Range.ParagraphFormat.Reset
    .Range.Font.Reset
    .Close SaveChanges:=True
  End With
  strFile = Dir()
Wend

Cheers
Paul Edstein
[MS MVP - Word]
 
>There is a 'WholeStory' property
Whole Story is a method, not a property

So to strip formatting in a document you can do

Code:
[blue]Public Sub StripFormatting()
    Dim oldselection As Range
    
    Application.ScreenUpdating = False
    Set oldselection = Selection.Range
    Selection.WholeStory
    Selection.ClearFormatting
    oldselection.Select
    Application.ScreenUpdating = True
End Sub[/blue]
 
Thanks all the experts, Let me try these methods. Will report back soon.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top