Stop macro from looping over itself with files in folder

H1SOKA

New Member
Joined
Mar 17, 2016
Messages
38
Hi everyone, :)

I have the following problem: when I load word files in the directory C:\Test, it creates an endless loop of files which gets converted as the following:

1.doc
Copy_1.doc
Copy_Copy_1.doc

I only want the files to loop through the originals once.

Alternatively, is there a way to modify this code to be use input and output folders? So the input files will be moved to C:\test\input and then the converted documents to C:\test\output. How do we do this? I've searched endlessly for an answer but can't seem to find what I'm looking for.

The code is below, any help is much appreciated!

Thanks again.

Code:
Sub Image()


Dim WdDoc As Document, sFile As String
Dim sPath, pName, File As String
Dim bDoc, SourceDoc, TargetDoc As Document
Dim AppPath As String
Dim iPgNum As Integer


Application.ScreenUpdating = False


sPath = "C:\Test\"
sFile = Dir(sPath & "*.*")




    Do While sFile <> ""
        Set WdDoc = Application.Documents.Open(sPath & sFile)
    
    ActiveDocument.SaveAs2 FileName:=sPath & "Copy_" & WdDoc, FileFormat _
        :=wdFormatDocument, LockComments:=False, Password:="", AddToRecentFiles:= _
        True, WritePassword:="", ReadOnlyRecommended:=False, EmbedTrueTypeFonts:= _
        False, SaveNativePictureFormat:=False, SaveFormsData:=False, _
        SaveAsAOCELetter:=False, CompatibilityMode:=0
            
        Selection.WholeStory
        Selection.Delete Unit:=wdCharacter, Count:=1
                 
        Set TargetDoc = ActiveDocument
        
        Documents.Open FileName:=sPath & sFile
     
        Set SourceDoc = ActiveDocument
        SourceDoc.Activate
    
        Selection.HomeKey Unit:=wdStory, Extend:=wdMove
        ActiveDocument.Repaginate


        For iPgNum = 1 To Selection.Information(wdNumberOfPagesInDocument)
    
            Selection.WholeStory
            Selection.Copy
    
                TargetDoc.Activate
                    Selection.PasteSpecial Link:=False, dataType:=wdPasteEnhancedMetafile, _
                    Placement:=wdInLine, DisplayAsIcon:=False
                Selection.TypeParagraph
    
            SourceDoc.Activate
            ActiveDocument.Bookmarks("\Page").Range.Delete
    
        Next
    
    SourceDoc.Close savechanges:=wdDoNotSaveChanges
    
    TargetDoc.Activate
    
    Selection.HomeKey Unit:=wdStory
    
    
        On Error Resume Next
  '      Call Deleemptyparagraphs
        Call DeleteBlankPage
        On Error Resume Next




    TargetDoc.Close savechanges:=wdSaveChanges
        
        sFile = Dir
    Loop


    
    Selection.HomeKey Unit:=wdStory
   ' ActiveDocument.Bookmarks("\Page").Range.Delete
    'ActiveDocument.Bookmarks("\Page").Range.Delete
        
Application.ScreenUpdating = True






    MsgBox "Document pages have been converted as images"


End Sub


Public Sub DeleteBlankPage()






Selection.GoTo What:=wdGoToBookmark, Name:="\page"


If isBlankSelection Then


Selection.Delete


End If


End Sub


Public Function BlankPageSelection()


For Each c In Selection.Characters


If (c <> vbCr And c <> vbTab And c <> vbFormFeed And c <> " ") Then


BlankPageSelection = False


Exit Function


End If


Next


BlankPageSelection = True


End Function


Sub Deleemptyparagraphs()
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "^p^p"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchByte = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = False
.MatchFuzzy = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
End Sub
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
have you tried renaming it with a space at the front so it saves itself above the file in question and that way doesnt get hit again when you move down the list?
 
Upvote 0
have you tried renaming it with a space at the front so it saves itself above the file in question and that way doesnt get hit again when you move down the list?

That's a great idea, however, I don't know how to do that. Any suggestions? :confused:
 
Upvote 0

Forum statistics

Threads
1,215,767
Messages
6,126,781
Members
449,336
Latest member
p17tootie

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top