How to tell if a Paragraph Object contains a picture or table

N

Nick Transier

I am looping through my documents with the simple code

Dim para as Paragraph

for each para in ActiveDocument.paragraphs
para.Range.select
'do stuff
next para

I need to be able to check whether the selection is a table or an inline
image. Is it possible to do this? If not, how can I loop through the code
selecting the tables and images?

Thanks-
Nick
 
J

Jean-Guy Marcil

Bonjour,

Dans son message, < Nick Transier > écrivait :
In this message said:
I am looping through my documents with the simple code

Dim para as Paragraph

for each para in ActiveDocument.paragraphs
para.Range.select
'do stuff
next para

I need to be able to check whether the selection is a table or an inline
image. Is it possible to do this? If not, how can I loop through the code
selecting the tables and images?

If your goal is just to get to every picture/table, then I would suggest
that you did not cycle through every paragraph, it could take much longer
than necessary. Try something like:

'_______________________________________
Sub CycleShapesTables()

Dim MyShape As Shape
Dim MyInlinshape As InlineShape
Dim MyTable As Table

With ActiveDocument

If .Shapes.Count > 0 Then
For Each MyShape In .Shapes
If MyShape.Type = msoPicture Then
'do your stuff
End If
Next MyShape
End If

If .InlineShapes.Count > 0 Then
For Each MyInlinshape In .InlineShapes
If MyInlinshape.Type = wdInlineShapePicture Then
'do your stuff
End If
Next MyInlinshape
End If

If ActiveDocument.Tables.Count > 0 Then
For Each MyTable In .Tables
'do your stuff
Next MyTable
End If

End With

End Sub

'_______________________________________

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
N

Nick Transier

Thanks for your help!

I have to loop through the code extracting the following, heading titles,
text under the headings, pictures, and tables. That's why I am using the for
each para setup. I can write different loops for each type, but then I loose
the order that they were coming out in.

Another question, if I select all the pictures i.e. for each pic in
activedocument.inlineshapes and I have defined a new word doc, docx, then
how do I paste the selection into doc x. Example code below:

For Each pic In DocA.InlineShapes
DoEvents
Dim DocC As Document
Set DocC = Word.Documents.Add
Set rng = DocC.Range
rng.Collapse wdCollapseEnd
'need something here to paste the picture
iPictureCount = iPictureCount + 1
Next pic

Thanks again!
Nick
 
J

Jean-Guy Marcil

Bonjour,

Dans son message, < Nick Transier > écrivait :
In this message said:
Thanks for your help!

I have to loop through the code extracting the following, heading titles,
text under the headings, pictures, and tables. That's why I am using the for
each para setup. I can write different loops for each type, but then I loose
the order that they were coming out in.

Another question, if I select all the pictures i.e. for each pic in
activedocument.inlineshapes and I have defined a new word doc, docx, then
how do I paste the selection into doc x. Example code below:

For Each pic In DocA.InlineShapes
DoEvents
Dim DocC As Document
Set DocC = Word.Documents.Add
Set rng = DocC.Range
rng.Collapse wdCollapseEnd
'need something here to paste the picture
iPictureCount = iPictureCount + 1
Next pic

Something like this perhaps:

'_______________________________________
Dim DocA As Document
Dim DocC As Document
Dim RngC As Range
Dim Pic As InlineShape
Dim iPictureCount As Long

Set DocA = ActiveDocument
Set DocC = Word.Documents.Add

For Each Pic In DocA.InlineShapes
Pic.Range.Copy
Set RngC = DocC.Range
RngC.Collapse wdCollapseEnd
RngC.Paste
RngC.InsertAfter Chr(13)
iPictureCount = iPictureCount + 1
Next Pic

RngC.InsertAfter Chr(13) & iPictureCount & " picture(s) " _
& "were pasted from " & DocA.Name & "."
'_______________________________________

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
N

Nick Transier

Thanks again!

What does the line RngC.InsertAfter Chr(13) do?

Also, do you know how to grab the caption text from an inlineshape?

Thanks,
Nick
 
J

Jean-Guy Marcil

Bonjour,

Dans son message, < Nick Transier > écrivait :
In this message said:
Thanks again!

What does the line RngC.InsertAfter Chr(13) do?

Inserts a paragraph mark (¶) after each inline shape.
Also, do you know how to grab the caption text from an inlineshape?

You mean a caption that was added with Insert > References > Caption...?
If so, it will not be that simple because there is not actual connection
between the two, except that they are next to each other.
You could get the paragraph following the inline shape, check if its style
is set to "Caption", if so, there you go, if not, there is no caption...
--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top