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.
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