PDA

View Full Version : Macro works well on single file but not on all files in a single directory



dnganatra
08-17-2013, 02:47 AM
I’m facing quite a unique challenge in running a macro in word 2007. The macro is designed to crop unwanted parts of an image. This macro works perfectly well when I run it on a single file. However, if I invoke the macro to run on all the files within a given directory, it just doesn’t seem to work. It doesn’t even give me any error. It just crops the first few images, and that is all.


I am just a novice, and would appreciate absolutely any help from the learnt members of this forum, in figuring out how to apply this code to run successfully on all the files. All help is welcome, and will be received with thanks and grace.


-Diu

This is what I have specifically: the code for the crop image macro I have is below.



Sub CropImageMacro()
'
' CropImageMacro Macro
' This Macro Crops unwanted parts of an image



Selection.MoveDown Unit:=wdLine, Count:=1
Selection.MoveRight Unit:=wdCharacter, Count:=20, Extend:=wdExtend
Selection.MoveDown Unit:=wdLine, Count:=1, Extend:=wdExtend
Selection.Delete Unit:=wdCharacter, Count:=1


Application.Templates.LoadBuildingBlocks
Dim sngHeight, sngWidth, sngCropTop, sngCropBottom As Single
sngCropTop = 0.154
sngCropBottom = 0.05


Dim DocPageCount As Integer
Dim TotalPages As Integer


DocPageCount = 1


'Getting total number of pages
'ActiveDocument.Repaginate
TotalPages = ActiveDocument.BuiltInDocumentProperties("Number of Pages")


Do While True
If (DocPageCount > TotalPages) Then
Exit Do
Else

' If (DocPageCount > 1) Then
' sngCropTop = 0.014
' sngCropBottom = 0.069

' Else

With ActiveDocument.InlineShapes(DocPageCount)
sngHeight = .Height
sngWidth = .Width
With .PictureFormat
.CropTop = sngHeight * sngCropTop
.CropBottom = sngHeight * sngCropBottom
End With
.Height = .Height
.Width = .Width
End With

sngCropTop = 0.014
sngCropBottom = 0.069


' End If

End If


DocPageCount = DocPageCount + 1
Loop
'End With
End Sub


The above works well on a single file. This below macro is what I run to apply the CropImageMacro on all files in a directory.




Sub FreeMeNow()
Dim file
Dim path As String
path = "C:\Test\"
file = Dir(path & "*.docx")
MsgBox file
Do While file <> ""
Documents.Open FileName:=path & file
Call CropImageMacro
ActiveDocument.Save
ActiveDocument.Close
' set file to next in Dir
file = Dir()
Loop
End Sub

fumei
08-17-2013, 10:45 PM
I am confused as to what exactly is happening. Are you saying all the FILES in the directory are not being processed - with the Sub FreeMeNow, or that all the IMAGES in a given file are not being processed - with the Sub CropImagemacro.

dnganatra
08-18-2013, 08:13 AM
@ Fumei- Thank you for writing bacl. The sub cropimagemacro runs perfectly well when i run it on a single open docx. It crops every image perfectly. I have about 300 odd files in a directory, where the images within those documents need to be cropped. I could open each one individually, but that would be too time consuming. Hence i run the FreeMeNow macro which is a macro designed to run on all files within a given directory and applying the macro assigned within it with the call function. When I run it on a directory it just crops the first 2-3 images within every document, saves the file and exits with no error.
Any help would be appreciated, thanks!

rollis13
08-18-2013, 02:55 PM
Cross-post: http://www.excelforum.com/word-programming-vba-macros/948189-macro-works-well-on-single-file-but-not-on-all-files-in-a-single-directory.html

dnganatra
08-18-2013, 11:09 PM
Hi rollis13, apologes if I have cross posted. I thought these were 2 different forums altogether hosted on 2 different websites.

Aussiebear
08-18-2013, 11:22 PM
Hi rollis13, apologes if I have cross posted. I thought these were 2 different forums altogether hosted on 2 different websites. That is exactly why you SHOULD indicate that you have posted the same issue elsewhere. Contributors to any of the forum/s where you may have posted the same issue, do so on a voluntary basis and shouldn't be taken for granted in such a way.

fumei
08-19-2013, 04:14 PM
Yes. It is precise BECAUSE they are different forums that you should mkention if you have posted in multiple locations.

For cross-posting etiquette, please read: http://www.excelguru.ca/content.php?184


When I run it on a directory it just crops the first 2-3 images within every document, saves the file and exits with no error.

So you are saying there is MORE than 3 images, and the ones past 3 are not being processed. But in the original starting if there are more than 3 images they are all processed.

fumei
08-19-2013, 04:20 PM
I have to ask. Why are you doing the following for each and every file:


Selection.MoveDown Unit:=wdLine, Count:=1
Selection.MoveRight Unit:=wdCharacter, Count:=20, Extend:=wdExtend
Selection.MoveDown Unit:=wdLine, Count:=1, Extend:=wdExtend
Selection.Delete Unit:=wdCharacter, Count:=1