PDA

View Full Version : [SOLVED:] move images to specific position



Ethen5155
01-02-2019, 06:32 AM
Hello,


well, I'm trying to get a possible way for a macro can move all non inline images (flowing images) with a specific criteria as following for ex.






A= get the page width size (215.9 mm) whatever it is
23490




B= get the image width size (60.96 mm) whatever it is

23491




C= get the Absolute position for image in page (32.3 mm) whatever it is

23492



then make (A) - (B) - (C) = the new position for image in page


215.9 - 60.96 - 32.3 = 122.64


23493




Notes:


- all changes should be to the page not margin


- all changes should be applied for non inline images only (flowing images)



samples file is attached for before and after.
23494

Thanks in advance


Cross-posting link : here (https://www.excelforum.com/word-programming-vba-macros/1258424-move-images-to-specific-position.html#post5036382)

gmaxey
01-02-2019, 08:05 AM
You can try this:


Sub ScratchMacro()
'A basic Word macro coded by Greg Maxey, http://gregmaxey.com/word_tips.html, 1/2/2019
Dim sngPW As Single, sngPH As Single
Dim oShp As Shape
sngPW = ActiveDocument.PageSetup.PageWidth
For Each oShp In ActiveDocument.Shapes
oShp.RelativeHorizontalPosition = wdRelativeHorizontalPositionPage
oShp.Left = sngPW - oShp.Width - oShp.Left
Next
lbl_Exit:
Exit Sub
End Sub

Ethen5155
01-02-2019, 08:41 AM
AMAZING!

it works very well, but there are just two matters

The first is that the final position should be 122.6 mm as shown on the last screenshot on main topic

but in your macro i found it be 122.7 mm !!

the second thing, can you add a counter at the end of macro to count how many images have been moved


really your help is highly appreciated

Thanks in advance

gmaxey
01-02-2019, 09:04 AM
You're welcome.

I suppose it is a rounding issue but can't say for sure. Absolute position is not something that you can return directly with VBA.


Sub ScratchMacro()
'A basic Word macro coded by Greg Maxey, http://gregmaxey.com/word_tips.html, 1/2/2019
Dim sngPW As Single, sngPH As Single
Dim oShp As Shape
Dim lngCount As Long
sngPW = ActiveDocument.PageSetup.PageWidth
For Each oShp In ActiveDocument.Shapes
oShp.RelativeHorizontalPosition = wdRelativeHorizontalPositionPage
oShp.Left = sngPW - oShp.Width - oShp.Left
lngCount = lngCount + 1
Next
MsgBox lngCount
lbl_Exit:
Exit Sub
End Sub

Ethen5155
01-03-2019, 02:34 AM
many thanks again for your generous help Greg