0
votes

I'm in the process of writing a code to convert a PDF into DOCX. In the first step, i save the PDF as DOCX using acrobat object. The example shown in attachment is from one of the pages. Wrench Image is wrapped as "Behind the text" and it is not part of the table. My questions are,

  1. how do i move it in to or make it as part of the table cell above using VBA. I tried wrap tight etc. It works for some of the images and not for majority of them. As the code goes through 100's of images, user does not have a visual of result from change in wrap format.

  2. When i try to delete first blank paragraph using code, the wrench image shown in attachment gets deleted as it is not part of the table. How do i delete the first empty paragraph without deleting the image if the image is not part of table and is in "behind the text format".

Thanks

Edit1: Conversion of shape to inlineshape (with inline text wrap format) throws the image out of the table as shown in 2nd attachment.

Edit2:

Sub Resizeimage(iDoc As Word.Document)

Dim i As Long

On Error GoTo eh

With iDoc
'      For i = .Shapes.Count To 1 Step -1
'        With .Shapes(i)
'          If .Type = msoPicture Then
'            .ConvertToInlineShape
'          End If
'        End With
'      Next
  For i = .Shapes.Count To 1 Step -1
  'Application.StatusBar = "Resizing & formatting Images - " & 
Round((iDoc.Shapes.Count - (i + 1)) / iDoc.Shapes.Count * 100, 0) & "% 
completed..."
    With .Shapes(i)
        If .width > Application.InchesToPoints(6) Then
            .LockAspectRatio = msoTrue
            .width = Application.InchesToPoints(6.9)
            .Left = wdShapeCenter
            '.WrapFormat.Type = wdWrapTight
        End If
        If .width > Application.InchesToPoints(3) Then
            .RelativeHorizontalPosition = wdRelativeHorizontalPositionMargin
            '.ZOrder msoBringToFront
            .Left = wdShapeCenter
            '.WrapFormat.Type = wdWrapTight
        ElseIf .width > Application.InchesToPoints(1.75) And .width < 
    Application.InchesToPoints(2.75) And .WrapFormat.Type = wdWrapTight Then
            .RelativeHorizontalPosition = 
    wdRelativeHorizontalPositionLeftMarginArea
            .Left = Application.InchesToPoints(0.1)
             .ZOrder msoBringToFront
        ElseIf .width < Application.InchesToPoints(1.75) Then
            .RelativeHorizontalPosition = 
   wdRelativeHorizontalPositionRightMarginArea
            .Left = Application.InchesToPoints(-2)
        End If
    End With
  Next
End With

Exit Sub

eh:
Call Errorhandler("Resizeimage", Err)

End Sub

WORD Page saved as docx from PDF

InlineShape

1
You first need to check if the image is a shape or an inline shape. If the former you will need to convert it to an inline shape, then set the relevant property to make the inline shape actually inline with text. Start by Investigating if your document has content in activedocument.shapes or activedocument.inlineshapesfreeflow
That is one of the things i tried. converted all the shapes to inlineshape with inlinetext format. I edited my question added an attachment to show the result. Thanks.Kannan Rajan
The image was never in the table, it was a floating image anchored to the empty paragraph. That's why deleting the paragraph also deletes the image. If we could move anchors, the simple solution would be to move the anchor to the desired table cell, then convert to inline. But anchors are read-only, so you'll have to copy and paste to the table cell after converting to inline.John Korchok
@JohnKorchok To convert to inlineshape is the only way to remove anchor which moves the image out of the table. Then it gets highly impossible to copy paste the image into a cell using VBA as the document does not follow a standard format.Kannan Rajan
As I mentioned, the picture was never in the table. It's floating below the table. So you would still have the same issues of selecting and moving it whether it is inline or floating. That's not impossible, just difficult.John Korchok

1 Answers

0
votes

When a floating shape is converted to inline, it moves to the position where its anchor formerly was. So we can predict the image position by finding the anchor location. After conversion, expand the range to include the picture, then cut it and paste into the table:

Sub Float2Inline()
  Dim oRange As Range
  Set oRange = ActiveDocument.Shapes(1).Anchor
  ActiveDocument.Shapes(1).ConvertToInlineShape
  With oRange
    .Expand Unit:=wdParagraph
    .Cut
  End With
  ActiveDocument.Tables(1).Rows(2).Cells(3).Range.Paste
End Sub