PDA

View Full Version : Solved: Numbering and positioning of picture captions



Hvorfor
10-28-2011, 06:19 AM
Hello guys.

(I'm using Word 2007 on Windows XP)


I'm trying to make a macro that goes through every picture in a document and gives the user the option to give the pictures a caption/figure name (Example: Figure 2.3: This is a figure). I also want to be able to cross-reference the captions manually at a later time.

This is my code so far:


Sub checkForPictures()
Dim numShapes As Integer
numShapes = ActiveDocument.InlineShapes.Count
Dim picName As String

CaptionLabels.Add Name:="Figure"

If numShapes > 0 Then
For Each Image In ActiveDocument.InlineShapes
Image.Select
Response = MsgBox("Do you want to edit the picture text/caption?", vbYesNo)
If Response = 6 Then
picName = InputBox("Input picture text below.", "Set Picture Text")
If picName = "" Then
Selection.Collapse
Else
Selection.Range.InsertCaption Label:="Figure", Title:=": " + picName, Position:=wdCaptionPositionBelow
End If
Else
Selection.Collapse
End If
Next Image
Else
MsgBox Prompt:="There are no pictures in this document."
End If
End Sub



This code is working just fine, but I'm having trouble with giving the correct chapter name. For example, if the document I'm working on is chapter 3 i want the figure to say "Figure 3.1" on the first picture etc. With this code every caption is named "Figure 1", "Figure 2" etc.

Every chapter will be written in its own word file. I'm planning on automating this, and opening the chapters in the correct order (first chapter 1, then chapter 2 etc), so there won't be a problem recording what chapter the macro is currently working on.

Do you guys have any idea how I can change my code so it will include the correct chapter number in the caption?

I hope I'm not asking too much here, but I also need to make sure the caption is positioned directly below the picture. What I mean by this is that if the picture is indented on the page I don't want the caption to start at the left margin, but at the same "indent" as the picture. I don't know if this is possible, and this is more of a "secondary objective", but any pointers on how to do this would be great.

If it's not clear what I want to do, please tell me and I will try to explain better.

PS: Is there a better way of handling the cases where I don't want to do anything to the picture, and jump to the next? Now I'm just using this:


Selection.Collapse


Thank you,

Even

Hvorfor
10-31-2011, 05:43 AM
A little update here:

I have now found a way to include the chapter number in the caption. However doing it this way means I get a space between the chapter number and the figure number.

An example on how it looks now is this:

"Figure 5. 1".

But I want it to look like this :

"Figure 5.1".

Is anyone able to give me some ideas now how to remove the "space"?

Here is my updated code:


Sub checkForPictures()
Dim numShapes As Integer
Dim picName As String
Dim chapNumb As Integer
Dim chapName As String

chapNumb = 5
chapName = "Figure " + CStr(chapNumb) + "."
CaptionLabels.Add Name:=chapName
numShapes = ActiveDocument.InlineShapes.Count

If numShapes > 0 Then
For Each Image In ActiveDocument.InlineShapes
Image.Select
Response = MsgBox("Do you want to edit the picture text/caption?", vbYesNo)
If Response = 6 Then
picName = InputBox("Input picture text below.", "Set Picture Text")
If picName = "" Then
Selection.Collapse
Else
Selection.Range.InsertCaption Label:=chapName, Title:=": " + picName, Position:=wdCaptionPositionBelow
End If
Else
Selection.Collapse
End If
Next Image
Else
MsgBox Prompt:="There are no pictures in this document."
End If
End Sub


Cheers,

Even

Hvorfor
10-31-2011, 06:59 AM
Another update:

I solved the issue with the "space". Because the caption title is fixed, insering this code after I insert the caption takes care if it.


Selection.GoTo What:=wdGoToLine, Which:=wdGoToNext, Count:=1
Selection.MoveRight Unit:=wdCharacter, Count:=9
Selection.Delete Unit:=wdCharacter, Count:=1


This is probably not the most efficient way of doing it. If there is better ways of solving it I would be very happy to hear about it.

So not much left now i gues. Anyone have any ideas on how to make sure that the caption is positioned below the picture?

Cheers,

Even

Hvorfor
11-01-2011, 03:29 AM
Solved:

Figured out how to move the text below the picture and also how to handle places where I didn't want to do anything.

The final code, if anyone else wants to do something similar, is not the most elegant - but it works.


Sub checkForPictures()
Dim numShapes As Integer
Dim picName As String
Dim chapNumb As Integer
Dim chapName As String
Dim picLeftIndent As Integer
chapNumb = 5
chapName = "Figure " + CStr(chapNumb) + "."
CaptionLabels.Add Name:=chapName
numShapes = ActiveDocument.InlineShapes.Count

If numShapes > 0 Then
For Each Image In ActiveDocument.InlineShapes
Image.Select
Response = MsgBox("Do you want to edit the picture text/caption?", vbYesNo)
If Response = 6 Then
picName = InputBox("Input picture text below.", "Set Picture Text")
If picName = "" Then

Else
picLeftIndent = Selection.ParagraphFormat.LeftIndent
Selection.EndKey
Selection.TypeParagraph
Selection.Range.InsertCaption Label:=chapName, Title:=": " + picName
Selection.MoveRight Unit:=wdCharacter, Count:=9
Selection.Delete Unit:=wdCharacter, Count:=1
Selection.ParagraphFormat.LeftIndent = picLeftIndent
End If
Else

End If
Next Image
Else
MsgBox Prompt:="There are no pictures in this document."
End If
End Sub


PS: The chapternumber variable is set to 5, this need to be changed according to your needs.