VBA: Setting Layout Pictures With Automatically in Ms Word

Susanto3311
3StarLounger
Posts: 240
Joined: 17 Feb 2022, 05:16

VBA: Setting Layout Pictures With Automatically in Ms Word

Post by Susanto3311 »

hi all...
i have several pictures/object then copy paste into ms word. i need macro/vba can do to setting automatically the picture layout after pasting in ms word with desired setting/layout like this:
1) Position:
Horizontal
- absolut 1,96 cm ---> (to the right of Column)
Vertical
- absolut 0,18 cm ---> (below Paragraph)
2) Size
Height (absolute) 13,24 cm
Width (absolute) 23,41 cm
3) Scale
Height 84%, Width 84%
for detail information attachment this file
anyone help me out, thanks in advance
susan
You do not have the required permissions to view the files attached to this post.

User avatar
HansV
Administrator
Posts: 78236
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: VBA: Setting Layout Pictures With Automatically in Ms Word

Post by HansV »

The size and scale requirements appear to be contradictory.
Best wishes,
Hans

Susanto3311
3StarLounger
Posts: 240
Joined: 17 Feb 2022, 05:16

Re: VBA: Setting Layout Pictures With Automatically in Ms Word

Post by Susanto3311 »

hi hans.
you're right size & scale is contradictory..
i think can use scale Height 84%, Width 84% without size number setting

User avatar
HansV
Administrator
Posts: 78236
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: VBA: Setting Layout Pictures With Automatically in Ms Word

Post by HansV »

Hmmm... anyone else? I cannot get VBA to set this correctly. If I specify an absolute position, Word changes it to a percentage...
Best wishes,
Hans

User avatar
ChrisGreaves
PlutoniumLounger
Posts: 15498
Joined: 24 Jan 2010, 23:23
Location: brings.slot.perky

Re: VBA: Setting Layout Pictures With Automatically in Ms Word

Post by ChrisGreaves »

HansV wrote:
02 Jul 2022, 11:36
Hmmm... anyone else? I cannot get VBA to set this correctly. If I specify an absolute position, Word changes it to a percentage...
I am not sure that I understand this problem.
I downloaded the docx, saved it as a Word2003 doc, exited then reloaded Word2003 opening the doc.
susant010.png
I can manually (no VBA yet) scale the size of the (first) image to 84%, which forces a Height of 11.12cm and a Width of 10.52cm; so I think that the size/scale etc is not a problem.

In Word2003, right-click, Format Picture, Layout, Advanced I can set up like this:-
susant011.png
Am I close to requirements? I am not sure how to prove that I have laid out the image correctly. Perhaps interim Large Values might help me/us to see if in Word2003 the appropriate settings can be made to stick?

Once we are sure that I understand the problem/requirements I will try replicating the action in VBA.

Cheers, Chris
You do not have the required permissions to view the files attached to this post.
An expensive day out: Wallet and Grimace

Susanto3311
3StarLounger
Posts: 240
Joined: 17 Feb 2022, 05:16

Re: VBA: Setting Layout Pictures With Automatically in Ms Word

Post by Susanto3311 »

if my question is difficult, i think for this moment this code can be reference (code work fine for me)

Code: Select all

Sub ResizePics()
Dim shp As Word.Shape
Dim ishp As Word.InlineShape
If Word.Selection.Type <> wdSelectionInlineShape And _
Word.Selection.Type <> wdSelectionShape Then
Exit Sub
End If
If Word.Selection.Type = wdSelectionInlineShape Then
Set ishp = Word.Selection.Range.InlineShapes(1)
ishp.LockAspectRatio = False
ishp.Height = CentimetersToPoints(13.78)
ishp.Width = CentimetersToPoints(23.17)
Else
If Word.Selection.Type = wdSelectionShape Then
Set shp = Word.Selection.ShapeRange(1)
shp.LockAspectRatio = False
shp.Height = CentimetersToPoints(11.78)
shp.Width = CentimetersToPoints(23.17)
End If
End If
End Sub
but found the problem, if picture more than 1? you must select pict/shape 1 by 1 before running the code
how to combine or if possible to resize pict with multiple selected all pict at once?

User avatar
HansV
Administrator
Posts: 78236
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: VBA: Setting Layout Pictures With Automatically in Ms Word

Post by HansV »

Try this:

Code: Select all

Sub ResizePics()
    Dim shp As Shape
    Dim ishp As InlineShape
    For Each shp In ActiveDocument.Shapes
        shp.LockAspectRatio = False
        shp.Height = CentimetersToPoints(11.78)
        shp.Width = CentimetersToPoints(23.17)
    Next shp
    For Each ishp In ActiveDocument.InlineShapes
        ishp.LockAspectRatio = False
        ishp.Height = CentimetersToPoints(13.78)
        ishp.Width = CentimetersToPoints(23.17)
    Next ishp
End Sub
Best wishes,
Hans

Susanto3311
3StarLounger
Posts: 240
Joined: 17 Feb 2022, 05:16

Re: VBA: Setting Layout Pictures With Automatically in Ms Word

Post by Susanto3311 »

hi hans, thanks a lot..
but i want to add criteria setting:
1. Horizontal (Absolute position) is 0,5 cm
2. Vertical (Absolute position) is 1,2 cm

User avatar
HansV
Administrator
Posts: 78236
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: VBA: Setting Layout Pictures With Automatically in Ms Word

Post by HansV »

That is only possible for shapes, not for inline shapes. But I don't know how to do that.
Best wishes,
Hans

User avatar
macropod
4StarLounger
Posts: 508
Joined: 17 Dec 2010, 03:14

Re: VBA: Setting Layout Pictures With Automatically in Ms Word

Post by macropod »

Inlineshapes would need conversion to Shapes:

Code: Select all

Sub ReFormatPics()
Dim Shp As Shape
With ActiveDocument
  Do While .InlineShapes.Count > 0
    .InlineShapes(1).ConvertToShape
  Loop
  For Each Shp In .Shapes
    With Shp
      .LockAspectRatio = False
      .Height = CentimetersToPoints(11.78)
      .Width = CentimetersToPoints(23.17)
      .RelativeHorizontalPosition = wdRelativeHorizontalPositionMargin
      .RelativeVerticalPosition = wdRelativeVerticalPositionParagraph
      .Top = CentimetersToPoints(0.18)
      .Left = CentimetersToPoints(1.96)
    End With
  Next Shp
End With
End Sub
Paul Edstein
[Fmr MS MVP - Word]

Susanto3311
3StarLounger
Posts: 240
Joined: 17 Feb 2022, 05:16

Re: VBA: Setting Layout Pictures With Automatically in Ms Word

Post by Susanto3311 »

hi paul, thank you very much!!! Working well.