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