PDA

View Full Version : Working with images in footer using VBA



arisley28
05-30-2013, 08:58 AM
After programming with VBA in Excel for the last 10+ years, and now trying to translate my skills to Word, I can say I have a newfound respect for anyone who can use VBA with Word. So far, anything I have tried to do has been like herding cats.

With that bit out of the way, here is the problem I am facing: I have taken on a new project where I have to work out the kinks of a system-generated report built in Word. Not sure which version it was built for initially, but the current version we use is 2007, which may or may not be part of the problem. I will admit I am a novice with Word, and my Excel VBA skills have helped me very little here so far. (Trying to record a macro in Word is so much harder it is making my head spin.)

There are two main tasks that I am charged with. The immediate task is to build a macro to find and fix footers with image size and/or placement issues. Issues can range from the appearance of the image to its position relative to another image - both in terms of white space and overlapping -to images covering text. This doesn't happen every time, but when it does, it takes our sales staff hours to fix the footers themselve, or the admins/support staff/anyone else but them to whom they delegate the task. I realize that there are a laundry list of issues that I may have to be able to address, but I will start with two simple (I hope) tasks to ask for help with:

1.) How can I determine the position (both absolute position in the footer and relative to another) of 2-3 images in footers?

2.) How can I resize images in the footer if they overlap?

The long-term task I am charged with is to become a content area expert for this process and document with zero background information, and no one else left knows anything about this process. All of the folks who built the report template or the application that uses it are long gone and didn't document anything. I am going to have to reverse engineer some of the updates to this template, and I would like to find out what I am working with, which brings me to my next request - Is there anyone who has built a procedure that can show me exactly what is in a single page of a document and where? This would be an exhaustive list of text, headers, footers, comments, formats, images, objects, tables, and literally anything else present.

If anyone is willing to help, either within this thread or by PM, I would appreciate it. If I need to break this thread up, I can do that. I also know that I don't know what specifics to ask or even where to start and am feeling a little overwhelmed at this point, so I would appreciate any help anyone can provide.

macropod
05-31-2013, 05:13 PM
Hi arisley,

Find out whether one shape overlaps another is easy enough. Deciding what to do about it in each case is quite another matter. The following code will report each overlap on a case-by-case basis.
Sub Demo()
Dim i1Shp As Shape, i1Top As Single, i1Lft As Single, i1Rght As Single, i1Btm As Single
Dim i2Shp As Shape, i2Top As Single, i2Lft As Single, i2Rght As Single, i2Btm As Single
Dim i As Long, j As Long, Sctn As Long, HdFt As HeaderFooter
With ActiveDocument
For Sctn = 1 To .Sections.Count
With .Sections(Sctn)
For Each HdFt In .Footers
With HdFt
If .LinkToPrevious = True Then Exit For
With .Range
For i = 1 To .ShapeRange.Count - 1
Set i1Shp = .ShapeRange(i)
With i1Shp
i1Top = .Top
i1Lft = .Left
i1Rght = .Width + .Left
i1Btm = .Height + .Top
End With
For j = i + 1 To .ShapeRange.Count
Set i2Shp = .ShapeRange(j)
With i2Shp
i2Top = .Top
i2Lft = .Left
i2Rght = .Width + .Left
i2Btm = .Height + .Top
End With
If i2Top < i1Btm And i2Lft < i1Rght Then
MsgBox "In Section " & Sctn & ", Footer " & HdFt.Index _
& ", " & i1Shp.Name & " overlaps " & i2Shp.Name
End IfNext
Next
End With
End With
Next
End With
Next
End With
End Sub
Note: The above code only considers floating shapes. It is also possible for floating shapes to overlap inline shapes (though those can't overlap each other). There are also good reasons why shapes/inlineshapes might sometimes overly one another.

arisley28
06-05-2013, 11:22 AM
Thanks for the example. This will help me quite a bit.

And, after working with this, I have more questions. Aren't all images files (jpg, png, gif) converted to Inlineshape objects? (I thought I read that in the help documentation somewhere, but I am not sure.) If so, how would it affect the example you provided?

macropod
06-05-2013, 04:09 PM
Aren't all images files (jpg, png, gif) converted to Inlineshape objects?
No. If they were:
a) you wouldn't have your current overlapping problems; and
b) the macro wouldn't find any shapes to compare.