How to insert image with the size W 50%, H 50% with macro

J

Jan Kratochvil

I insert lot of images with size 100*600, 1200*700, etc. my document.

They are screenshots.

How is it possible to insert those images with size 50% to have they in my
doc displayed in true size?

I tried to make this macro but I don't understand the properties:

Selection.InlineShapes(1).Height = 50
Selection.InlineShapes(1).Width = 50

What does mean the value 50?

If I run the macro on images with different size, than I get in Word every
time the same size.
It means .Height and .Size works not in %?

Here is the macro:

Sub SetImgSize()
'
' SetImgSize Makro
' Set image site on size 50%
'Shift+Alt+S
'
Selection.InlineShapes(1).Fill.Visible = msoFalse
Selection.InlineShapes(1).Fill.Solid
Selection.InlineShapes(1).Fill.Transparency = 0#
Selection.InlineShapes(1).Line.Weight = 0.75
Selection.InlineShapes(1).Line.Transparency = 0#
Selection.InlineShapes(1).Line.Visible = msoFalse
Selection.InlineShapes(1).LockAspectRatio = msoTrue
Selection.InlineShapes(1).Height = 50
Selection.InlineShapes(1).Width = 50
Selection.InlineShapes(1).PictureFormat.Brightness = 0.5
Selection.InlineShapes(1).PictureFormat.Contrast = 0.5
Selection.InlineShapes(1).PictureFormat.ColorType = msoPictureAutomatic
Selection.InlineShapes(1).PictureFormat.CropLeft = 0#
Selection.InlineShapes(1).PictureFormat.CropRight = 0#
Selection.InlineShapes(1).PictureFormat.CropTop = 0#
Selection.InlineShapes(1).PictureFormat.CropBottom = 0#
End Sub

Thanks
 
L

Lene Fredborg

If the only thing you want to do is to reduce the size of the selected inline
shape to 50%, you can use this code:

With Selection.InlineShapes(1)
.ScaleHeight = 50
.ScaleWidth = 50
End With

Alternatively, you could divide the current height and width by 2:

With Selection.InlineShapes(1)
.Height = .Height / 2
.Width = .Width / 2
End With

--
Regards
Lene Fredborg
DocTools - Denmark
www.thedoctools.com
Document automation - add-ins, macros and templates for Microsoft Word
 
J

Jan Kratochvil

Thank you

Is it possible also open with macro certain folder for insert an image.
I have all my images for doc in subfolder called "images":
INCLUDEPICTURE ""images/

I do it all the time manually - I need to save time.

Now I use this macro which insert IcludePicture field in my document. I
would be great to have than opened window with e.g. folder Images to insert
image easily.
I do it now as I wrote above manually. I hope you understand what I mean.

Sub InsertIncludeImageFieldWithImageStyle()
'
' InsertINCLUDEPICTURE With Image Style Makro
'
'Insert INCLUDEPICTURE field
Selection.Style = ActiveDocument.Styles("image")
Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:= _
"INCLUDEPICTURE ""images/"" ", PreserveFormatting:=True
Selection.MoveLeft Unit:=wdCharacter, Count:=1, Extend:=wdExtend
Selection.Fields.ToggleShowCodes
' Image frame
With Selection.InlineShapes(1)
With .Borders(wdBorderLeft)
.LineStyle = wdLineStyleSingle
.LineWidth = wdLineWidth050pt
.Color = wdColorAutomatic
End With
With .Borders(wdBorderRight)
.LineStyle = wdLineStyleSingle
.LineWidth = wdLineWidth050pt
.Color = wdColorAutomatic
End With
With .Borders(wdBorderTop)
.LineStyle = wdLineStyleSingle
.LineWidth = wdLineWidth050pt
.Color = wdColorAutomatic
End With
With .Borders(wdBorderBottom)
.LineStyle = wdLineStyleSingle
.LineWidth = wdLineWidth050pt
.Color = wdColorAutomatic
End With
.Borders.Shadow = False
End With
With Options
.DefaultBorderLineStyle = wdLineStyleSingle
.DefaultBorderLineWidth = wdLineWidth050pt
.DefaultBorderColor = wdColorAutomatic
End With

End Sub
 
L

Lene Fredborg

You can use e.g. the Insert Picture dialog box via VBA. Try to replace the
first part of the code in your macro until "Image frame" by the following:

Dim strPictureName As String
Dim strPicturePath As String

'Replace the path below by the path to your image folder
strPicturePath = "C:\Images"

ChangeFileOpenDirectory strPicturePath
'Select picture via Insert Picture dialog box
With Dialogs(wdDialogInsertPicture)
.Update
If .Display <> 0 Then
'File selected - get full name (incl. path)
strPictureName = WordBasic.FilenameInfo$(.Name, 1)
Else
MsgBox "No picture selected."
'Stop now
Exit Sub
End If
End With

'Insert INCLUDEPICTURE field
With Selection
.Style = ActiveDocument.Styles("image")

.InlineShapes.AddPicture _
FileName:=strPictureName, _
LinkToFile:=True, _
SaveWithDocument:=True

.MoveLeft Unit:=wdCharacter, Count:=1, Extend:=wdExtend
End With

Note that ChangeFileOpenDirectory is used to make the Insert Picture dialog
box open with the specified folder selected. The ".Display" method does not
insert the selected file. It is only used to get the name of the selected
file so that a field can be inserted afterwards.

--
Regards
Lene Fredborg
DocTools - Denmark
www.thedoctools.com
Document automation - add-ins, macros and templates for Microsoft Word
 

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