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