PDA

View Full Version : Create caption for image



wpanssi
09-29-2009, 02:51 PM
I have been working with VBA mainly with Excel but now I'm facing a word problem.

This is want I want my macro to do:
Loop through all pictures. Take the text above each picture and make it the caption of the picture.

Any help on this?

Thanks in advance!!

macropod
09-29-2009, 07:34 PM
Hi wpanssi,

Unless the pictures are formatted as 'in-line with text', this could be quite difficult as there isn't any necessary relationship between the position of an image on the page and the paragraph it's anchored to.

When you say you want to "make it the caption", do you mean you want to apply the Caption Style to it? Do you also need to insert the caption number (or replace a manual number with a 'proper' caption number?

wpanssi
09-30-2009, 03:07 AM
The pictures are formatted as inline text. And I need to insert the caption number (the same thing that happens when you use Insert Caption from theReferences -tab).

fumei
09-30-2009, 01:26 PM
Normally Captions have standard labels (Figure 1, Table 1 etc.).

If i understand correctly, you want to take the existing text of the previous paragraph and use THAT as the Label.

Yadda yadda
<InlineShape>

text
text
text

Blah blah blah
<InlineShape>


would become:


Yadda yadda 1
<InlineShape>

text
text
text

Blah blah blah 2
<InlineShape>

Is this correct?

wpanssi
09-30-2009, 02:15 PM
No, I want the standard label. Like this:
Yadda yadda
<InlineShape>

text
text
text

Blah blah blah
<InlineShape>


would become:


Figure 1: Yadda yadda
<InlineShape>

text
text
text

Figure 2: Blah blah blah
<InlineShape>

Thanks for your replies so far.

fumei
09-30-2009, 03:29 PM
Do you mean like this? Click "Caption Stuff" on the top toolbar.

wpanssi
10-03-2009, 06:55 AM
Yes, thank you! This is exactly what I want! Would you mind posting the actual code? Tha attachment's project is password protected..

fumei
10-05-2009, 09:22 AM
What have you tried so far?

wpanssi
10-05-2009, 10:49 AM
This is what I use to loop through all the pictures and to add a caption:

Sub loopPictures()

Dim i As InlineShape

For Each i In ActiveDocument.InlineShapes
i.Select
Selection.InsertCaption Label:="Figure", _
Title:=" caption text", Position:=wdCaptionPositionBelow, ExcludeLabel:=0
Next i

End Sub

However, I don't know how to grab the caption text from the paragraph above the image.

fumei
10-05-2009, 12:59 PM
For one thing, I did not do any Selecting, of anything. Here are my variables.

Dim strText As String
Dim r As Range
Dim oInlineShape As InlineShape
Notice it uses a RANGE object. Now to process through each InlineShape, you use:
For Each oInlineShape In ActiveDocument.InlineShapes
Set r = oInlineShape.Range
Thus making the range object the Inlineshape. This is then Collapsed, to the .Start, and moved backwards, one character. Thus, the range object is now just BEFORE the InlineShape.

The range object then uses .Expand to get the paragraph just BEFORE the InlineShape.
.Expand Unit:=wdParagraph

That range will include the paragraph mark, so that has to be stripped off using standard string manipulation functions. That value goes into the string variable strText.

Blah blah blah (this is now in the variable strText)
<InlineShape>

The Caption is added to the current InlineShape using InsertCaption:
oInlineShape.Range.InsertCaption
with the Title parameter including that string variable.

Now, you will have:

Yadda yadda
Figure 1 Yadda yadda
<InlineShape>

Notice the original "Yadda Yadda" is still there? Therefore, if you do not want that - and I assumed you did not - that original "Yadda yadda" (which IS the range object, needs to made to go away. Thus ending up with:


Figure 1 Yadda yadda
<InlineShape>