Search code examples
vbams-wordformatshapesinline

Find all table/figure in Word file not formatted wdWrapInline


I have few files (1000+ pages) and occasionally find table or shape not formatted "Inline with Text"... I tried to find a VBA that can help me identify every table or inline shape in my document that are not formatted wdWrapInline. My documents have both page orientations (portrait and landscape).

I tried simple

For sec To doc.Sections.count
     Selection.Find.WrapFormat = wdWrapInline 

but that's not correct.

Thanks in advance.

I tried simple Selection.Find.WrapFormat = 7 but that's not correct.


Solution

  • Is this what you want?

    Sub Find_all_table_figure_in_Word_file_not_formatted_wdWrapInline()
    
        'CheckWrapType()
        Dim shp As Shape ' Tables always in line so they don't have WrapFormat property.
        Dim sr As Range
        Dim d As Document
        Dim bk As Bookmark, i As Long 'you can bookmark them to navigate after
        Dim cln As New VBA.Collection ' or  you can store them in a collection object for further
        Dim ur As UndoRecord
        
        Set ur = Word.Application.UndoRecord
        Set d = ActiveDocument
        ur.StartCustomRecord "Find_all_table_figure_in_Word_file_not_formatted_wdWrapInline"
        
        Rem if you want to find all of they
        For Each sr In d.StoryRanges
            Rem if you just want all shapes in the main content of a doc only
            'If sr.StoryType = wdMainTextStory Then
            For Each shp In sr.ShapeRange
                If shp.WrapFormat.Type = wdWrapInline Then
                    'Debug.Print shp.Name & " is inline."
                Else
                    'Debug.Print shp.Name & " is not inline."
                    Rem If you want to convert they into in line.
                    'shp.ConvertToInlineShape
                    i = i + 1
                    d.Bookmarks.Add "shape" + VBA.CStr(i), shp.Anchor
                    cln.Add shp
                End If
            Next shp
            'end if
        Next sr
    
        ur.EndCustomRecord
    End Sub