Word macro VBA: Fit the image to the shape - vba

I would like to fit the image to the shape. The code is simple:
Function CmPt(cm As Single) As Single
' Convert centimeters to points.
CmPt = Application.CentimetersToPoints(cm)
End Function
Sub InsertCanvas()
' Insert puzzle image canvas to the document.
Dim edge As Single
edge = CmPt(4)
Dim canvas As Shape
Set canvas = ActiveDocument.Shapes.AddShape(Type:=msoShapeRectangle, Left:=CmPt(2.5), Top:=CmPt(2.5), Width:=edge, Height:=edge, Anchor:=Selection.Paragraphs(1).Range)
Dim image_path As String
image_path = ActiveDocument.Path & Application.PathSeparator & "images" & Application.PathSeparator & "image.jpeg"
With canvas
.Line.Weight = 1
.Line.ForeColor.RGB = RGB(64, 64, 64)
.Fill.Visible = msoTrue
.Fill.BackColor.RGB = RGB(255, 255, 255)
.Fill.UserPicture image_path
End With
End Sub
But now, the image is filling the square. I would like to fit the image. I know that Word can do it, but I believe I have to compute itself from the original aspect ratio. Is possible to get original size of the .UserPicture? Or is possible to get the width and height of any picture on the hard drive without inserting the image into the document? Thank you

Please, try the next function. It will extract the image dimensions without importing it in any way:
Function ImgDimensions(ByVal sFile As String) As Variant
Dim oShell As Object, oFolder As Object, oFile As Object, arr
Dim sPath As String, sFilename As String, strDim As String
sPath = Left(sFile, InStrRev(sFile, "\") - 1)
sFilename = Right(sFile, Len(sFile) - InStrRev(sFile, "\"))
Set oShell = CreateObject("Shell.Application")
Set oFolder = oShell.Namespace(CStr(sPath))
Set oFile = oFolder.ParseName(sFilename)
strDim = oFile.ExtendedProperty("Dimensions")
strDim = Mid(strDim, 2): strDim = Left(strDim, Len(strDim) - 1)
arr = Split(strDim, " x ")
ImgDimensions = Array(CLng(arr(0)), CLng(arr(1)))
End Function
It may replace your importing lines from the code above, and picture declaration:
Set picture = ActiveDocument.Shapes.AddPicture(image_path, LinkToFile:=False, SaveWithDocument:=True)
width = picture.width
height = picture.height
picture.Delete
with:
Dim arr
arr = ImgDimensions(sFile)
width = arr(0): height = arr(1)

I found suitable solution for me. I know it is not ideal, and I can't say I like it, but it is enough and it is working correctly. I post only a snippet here:
Dim width As Long
Dim height As Long
Set picture = ActiveDocument.Shapes.AddPicture(image_path, LinkToFile:=False, SaveWithDocument:=True)
width = picture.width
height = picture.height
picture.Delete
Edit:
The whole vba code for Word macro
Function CmPt(cm As Single) As Single
' Convert centimeters to points.
CmPt = Application.CentimetersToPoints(cm)
End Function
Sub InsertPuzzleCard()
' Insert puzzle card to the document.
Dim edge As Single
edge = CmPt(4)
Dim canvas As Shape
Set canvas = ActiveDocument.Shapes.AddShape(Type:=msoShapeRectangle, Left:=CmPt(2.5), Top:=CmPt(2.5), width:=edge, height:=edge, Anchor:=Selection.Paragraphs(1).Range)
Dim image_path As String
image_path = ActiveDocument.Path & Application.PathSeparator & "images" & Application.PathSeparator & "image.jpeg"
Dim picture As Shape
Dim width As Long
Dim height As Long
Dim ratio As Single
Dim new_width As Long
Dim new_height As Long
Set picture = ActiveDocument.Shapes.AddPicture(image_path, LinkToFile:=False, SaveWithDocument:=True)
width = picture.width
height = picture.height
picture.Delete
ratio = width / height
If ratio < 1 Then
new_width = width * edge / height
new_height = edge
Else
new_width = edge
new_height = height * edge / width
End If
With canvas
.Line.Weight = 1
.Line.ForeColor.RGB = RGB(64, 64, 64)
.Fill.Visible = msoTrue
.Fill.UserPicture image_path
.PictureFormat.Crop.PictureWidth = new_width
.PictureFormat.Crop.PictureHeight = new_height
End With
End Sub

Related

How do I export a jpg or pdf image to a word document using ms-Access and vba? [duplicate]

Given is the path of a picture. How can I add the picture to a word document by using VBA code?
This is the concept of adding image to word document.
Create a template document ,let say in c:\path\file.docx
add an image where ever you like (this will be the frame to hold the new image )
select the image and insert a bookmark and name it something like "someBookmarkName".
And now from access use this code
Sub insertImageToWord()
Dim Word As Object
Dim doc As Object
Dim filePath As String: filePath = "c:\path\file.docx"
Dim SHP As Object
Dim strTmp As String: strTmp = "someBookmarkName"
Dim strPath As String: strPath = "c:\path\image_file.png"
Set Word = CreateObject("Word.Application")
Set doc = Word.Documents.Open(filePath)
Set SHP = doc.Bookmarks(strTmp).Range.InlineShapes.AddPicture(Filename:=strPath, _
LinkToFile:=False, _
SaveWithDocument:=True)
With SHP
'this will keep ratio
' .WrapFormat.type = 1 'wdWrapTight
' .WrapFormat.type = 7 'wdWrapInline
.LockAspectRatio = -1 ' msoTrue
'this will adjust width to 0.5 inch
'.Width = wd.InchesToPoints(2.5)
' .Width = wd.CentimetersToPoints(2.66) * 2.5
' .Height = wd.CentimetersToPoints(3.27) * 2.5
' .ScaleHeight = 150
End With
End Sub

Word Macro: Set page orientation after section break

This question is about a new problem that came up while I was trying to get an addition to something to work I already asked a question about.
What I want my macro to do/what it's already kind of doing:
Add a header to a Word document (same one for the whole document)
Read image files from a specific folder from the HDD and insert them into the document
Add a section break if the image orientation (landscape or portrait) differs from the previous one and set the page orientation for the new section accordingly (BEFORE adding the image)
Add a line break and the file name of the image
Add a page break (each image gets its own page, no matter its size)
To ensure that the name doesn't get pushed to the next page (if the image fills the whole page), I set the bottom margin to a higher value before adding the image and the name and then set the margin back to the original value. This way the image is a little bit smaller and leaves enough space for the name.
My code (see below) does add section breaks but it seems like it sets the orientation for the whole document, not just the current section, so I end up with the same orientation on all pages. The images are also only added in the very last section without any page/section breaks in between.
How do I fix this?
In the other question someone already posted full code to set the orientation but I'd prefer understanding why my code doesn't work as intended to just copying someone else's completely different one.
My code:
Sub ImportImages(path As String)
Dim fs As Object
Dim ff As Variant
Dim img As Variant
Dim i As Long
Dim fsize As Long
Dim bottomMarginOriginal As Single
Dim topMarginOriginal As Single
Dim vertical As Boolean
Dim objShell As New Shell
Dim objFolder As Folder
Dim objFile As ShellFolderItem
Dim width As Integer
Dim height As Integer
Set fs = CreateObject("Scripting.FileSystemObject")
Set ff = fs.GetFolder(path).Files
i = 0
fsize = ff.Count
vertical = True
Set objFolder = objShell.NameSpace(path)
With ActiveDocument
bottomMarginOriginal = .PageSetup.BottomMargin
topMarginOriginal = .PageSetup.TopMargin
For Each img In ff
Select Case Right(img.name, 4)
Case ".bmp", ".jpg", ".gif", ".png", "tiff", ".tif"
Set objFile = objFolder.ParseName(img.name)
width = objFile.ExtendedProperty("{6444048F-4C8B-11D1-8B70-080036B11A03} 3")
height = objFile.ExtendedProperty("{6444048F-4C8B-11D1-8B70-080036B11A03} 4")
If width > height Then
If vertical = False Then 'Already landscape -> just add page break
.Characters.Last.InsertBefore Chr(12)
Else 'Set to landscape
Selection.InsertBreak Type:=wdSectionBreakNextPage
.PageSetup.Orientation = wdOrientLandscape
.PageSetup.TopMargin = topMarginOriginal 'Adjust margins to new orientation
.PageSetup.RightMargin = bottomMarginOriginal
.PageSetup.BottomMargin = bottomMarginOriginal
.PageSetup.LeftMargin = bottomMarginOriginal
.Sections(1).Headers(wdHeaderFooterPrimary).Range.Text = "test " & i 'Set header
vertical = False
End If
ElseIf height > width Then
If vertical = True Then 'Already portrait -> just add page break on page 2+
If i <> 0 Then
.Characters.Last.InsertBefore Chr(12)
End If
Else 'Set to portrait
Selection.InsertBreak Type:=wdSectionBreakNextPage
.PageSetup.Orientation = wdOrientPortrait
.PageSetup.TopMargin = topMarginOriginal 'Adjust margins to new orientation
.PageSetup.RightMargin = bottomMarginOriginal
.PageSetup.BottomMargin = bottomMarginOriginal
.PageSetup.LeftMargin = bottomMarginOriginal
.Sections(1).Headers(wdHeaderFooterPrimary).Range.Text = "test " & i 'Set header
vertical = True
End If
Else
If i <> 0 Then
.Characters.Last.InsertBefore Chr(12)
End If
End If
.PageSetup.BottomMargin = bottomMarginOriginal + Application.CentimetersToPoints(1) 'Add 1cm to the bottom margin
i = i + 1
.Characters.Last.InlineShapes.AddPicture filename:=img
.Characters.Last.InsertBefore Chr(11) & img.name
.PageSetup.BottomMargin = bottomMarginOriginal 'Reset bottom margin to default
End Select
Next
End With
End Sub
Here is the concept code based around putting images in Tables. A habit I have acquired from long long use of Word.
At the moment the ParseName keyword isn't being recognised even though I added a reference to Microsoft Shell etc etc.
Not a pagebreak in sight as they are not needed.
Option Explicit
Const PortraitPictureHeight As Long = 0 ' change to cm value
Const PortraitTextHeight As Long = 0 ' change to a cm value
Const LandscapePictureHeight As Long = 0 ' change to a cm value
Const LandscapeTextHeight As Long = 0 ' change to a cm value
Const HeightOfLineAfterTable As Long = 0 ' change to a points
Sub test()
ImportImages "C:\\Users\\slayc\\Pictures"
End Sub
Sub ImportImages(path As String)
Dim fs As Scripting.FileSystemObject
Dim ff As Variant
Dim img As Variant
Dim objShell As Shell
Dim objFolder As Folder
Dim objFile As ShellFolderItem
Dim width As Long
Dim height As Long
Set fs = New Scripting.FileSystemObject
Set ff = fs.GetFolder(path).Files
Set objShell = New Shell
Set objFolder = objShell.NameSpace(path)
' The assumption is that we are adding sections to the end of the document
' so we add the Heder to the last document
' this header will be copied to each section we add to the document
' when we use Activedocument.sections.add
ActiveDocument.Sections.Last.Headers(wdHeaderFooterPrimary).Range.Text = "This is your header"
For Each img In ff
If InStr(".bmp,.jpg,.gif,.png,.tiff", Right(img.Name, 4)) = 0 Then GoTo Continue_img
Set objFile = objFolder.ParseName(img.Name)
width = objFile.ExtendedProperty("{6444048F-4C8B-11D1-8B70-080036B11A03} 3")
height = objFile.ExtendedProperty("{6444048F-4C8B-11D1-8B70-080036B11A03} 4")
' every image gets its own section with its own orientation
If width > height Then
InsertLandscapeSection
Else
InsertPortraitSection
End If
FormatLastTable
With ActiveDocument.Sections.Last.Range.Tables(1).Range
.Rows(1).Range.Cells(1).Range.Characters.Last.InlineShapes.AddPicture FileName:=img
.Rows(2).Range.Cells(1).Range.Text = img.Name
End With
Continue_img:
Next
End Sub
Public Sub InsertLandscapeSection()
Dim my_range As Word.Range
With ActiveDocument.Sections
' Deal with the case where the first section is the last section
If .Last.Range.Tables.Count > 0 Then
.Add
.Last.Range.Previous(unit:=wdParagraph).Font.Size = HeightOfLineAfterTable
End If
.Last.PageSetup.Orientation = wdOrientLandscape
With .Last
Set my_range = .Range.Duplicate
my_range.Collapse direction:=wdCollapseStart
.Range.Tables.Add my_range, 2, 1
With .Range.Tables(1).Range
.Rows.HeightRule = wdRowHeightExactly
.Rows(1).height = CentimetersToPoints(LandscapePictureHeight)
.Rows(2).height = CentimetersToPoints(LandscapeTextHeight)
End With
End With
End With
End Sub
Public Sub InsertPortraitSection()
Dim my_range As Word.Range
With ActiveDocument.Sections
If .Last.Range.Tables.Count > 0 Then
.Add
.Last.Range.Previous(unit:=wdParagraph).Font.Size = HeightOfLineAfterTable
End If
.Last.PageSetup.Orientation = wdOrientPortrait
With .Last
Set my_range = .Range.Duplicate
my_range.Collapse direction:=wdCollapseStart
.Range.Tables.Add my_range, 2, 1
With .Range.Tables(1).Range
.Rows.HeightRule = wdRowHeightExactly
.Rows(1).height = CentimetersToPoints(PortraitPictureHeight)
.Rows(2).height = CentimetersToPoints(LandscapeTextHeight)
End With
End With
End With
End Sub
Sub FormatLastTable()
With ActiveDocument.Sections.Last.Range.Tables(1)
' turn off all borders
.Borders.Enable = False
'Do any additional formatting of the table that is not related to row height
End With
End Sub

Powerpoint VBA to insert image and change size

I think this could solve problems for a lot of people doing tedious pasting of images from one directory into powerpoint then resizing them.
My problem is that I have 16 images all in one directory which need updating each month and it's very slow to do it one by one. The task is:
Open directory
Open first image
Paste image into powerpoint
Reposition image to top left
Resize image to height 550 by width 960 (fills A4 page)
Send image to back
Move to next slide
Repeat for second image
Continue until no more images in directory
Directory is (e.g.) "C:\Users\xxxxxx\Documents\Work\Procurement Project\Slides"
First image name is (e.g.) "01 Summary", second is "02 Client Contracts" etc etc
I think I need a str and a path and a table for the str to add to path to create each new path using i and i + 1 etc
I think I then need some code that's a bit like this:
Sub Picture_size_and_position()
Dim oShape As Shape
Dim oPresentation As Presentation
Dim oSlide As Slide
Dim oSelection As Selection
ActiveWindow.View.GotoSlide oSlide.SlideIndex
With ActiveWindow.Selection.ShapeRange
.LockAspectRatio = msoFalse
.Height = 550
.Width = 960
.Left = 0
.Top = 0
End With
End Sub
Then I'm sure I need a looping function to repeat this until there's nothing left in the directory using some combination of i and j...but the whole code is way beyond me, very frustratingly.
Could someone offer some tips, please? Much much appreciated!
Thank you!
Sub ImportABunch()
Dim strTemp As String
Dim strPath As String
Dim strFileSpec As String
Dim oSld As Slide
Dim oPic As Shape
' Edit these to suit:
strPath = "C:\Users\username\"
strFileSpec = "*.png"
strTemp = Dir(strPath & strFileSpec)
i = 1
Do While strTemp <> ""
Set oSld = ActivePresentation.Slides(i)
Set oPic = oSld.Shapes.AddPicture(FileName:=strPath & strTemp, _
LinkToFile:=msoFalse, _
SaveWithDocument:=msoTrue, _
Left:=0, _
Top:=0, _
Width:=960, _
Height:=550)
i = i + 1
With oPic
.LockAspectRatio = msoFalse
.ZOrder msoSendToBack
End With
' Or (with thanks to David Marcovitz) make the picture as big as possible on the slide
' without changing the proportions
' Leave the above commented out, uncomment this instead:
' With oPic
' If 3 * .width > 4 * .height Then
' .width = ActivePresentation.PageSetup.Slidewidth
' .Top = 0.5 * (ActivePresentation.PageSetup.Slideheight - .height)
' Else
' .height = ActivePresentation.PageSetup.Slideheight
' .Left = 0.5 * (ActivePresentation.PageSetup.Slidewidth - .width)
' End If
' End With
' Optionally, add the full path of the picture to the image as a tag:
'With oPic
' .Tags.Add "OriginalPath", strPath & strTemp
'End With
' Get the next file that meets the spec and go round again
strTemp = Dir
Loop
End Sub
Credit to http://www.pptfaq.com/index.html - Great little site!
Have an idea to automate it/or upon manual launch of a new Macro Enabled PowerPoint Template file. To automate macro upon file open, add customUI: onLoad="ImagesToPowerPoint". Search "CustomUI Editor" for it.
Note I have not fully tested the automation part.
Option Explicit
Sub ImagesToPowerPoint()
Const FileType As String = "*.png"
Dim sSaveFilePath As String, sSaveFileName As String, sImagesFolder As String
Dim oLayout As CustomLayout, oSlide As Slide, i As Long, sFile As String
sImagesFolder = Environ("USERPROFILE") & "\Documents\Work\Procurement Project\Slides\"
' Prepare auto save PowerPoint file name
sSaveFilePath = Environ("USERPROFILE") & "\Documents\Work\PowerPoints\"
sSaveFileName = Format(Now, "yyyy_mmdd") & "_Procurement.pptx"
With ActivePresentation
' Use the first layout for all new slides
Set oLayout = .SlideMaster.CustomLayouts(1)
' Start processing all files in the folder
sFile = Dir(sImagesFolder & FileType)
Do Until sFile = ""
' Add new slide
Set oSlide = .Slides.AddSlide(.Slides.Count, oLayout)
' Delete all the shapes from that layout
For i = oSlide.Shapes.Count To 1 Step -1
oSlide.Shapes(i).Delete
Next
' Add the image to slide
With oSlide.Shapes.AddPicture(sImagesFolder & sFile, msoFalse, msoTrue, 0, 0, oLayout.Width, oLayout.Height)
.LockAspectRatio = msoFalse
.AlternativeText = Now & " | " & sImagesFolder & sFile
End With
sFile = Dir
Loop
.SaveAs sSaveFilePath & sSaveFileName
End With
Presentations(sSaveFileName).Close
If Presentations.Count = 0 Then Application.Quit
End Sub

Add a TextBox below an Inline Shape on MS Word using VBA

I'm trying to insert a textbox below a chosen image with Inline text wrapping and position it at the left bottom of the image. I'm using the below code without much success. I'm not too sure whether to use a ShapeRange or an InlineShape. Any pointers?
Dim shp As Object
'Set shp = Selection.ShapeRange(1)
'Set rng = shp.Anchor
Set shp = Selection.InlineShapes(1)
Set rng = shp.Range
With ActiveDocument.Shapes.AddTextbox(Orientation:=msoTextOrientationHorizontal, Left:=shp.Left, Top:=(shp.Top + shp.Height), Width:=shp.Width / 3, Height:=shp.Height / 6, Anchor:=rng)
.TextFrame.TextRange.Font.Size = 14
.TextFrame.TextRange.Font.Color = RGB(186, 14, 29)
.TextFrame.TextRange.Font.Name = "Sabon MT"
.TextFrame.TextRange = "T"
End With
I managed to find a solution for getting the coordinates for the shape from here: http://www.vbaexpress.com/forum/archive/index.php/t-48831.html
Here's my code:
Sub AddTextBox
Set shp = Selection.InlineShapes(1)
Set rng = shp.Range
Set tb = ActiveDocument.Shapes.AddTextbox(1, fcnXCoord, fcnYCoord + shp.Height, shp.Width, shp.Height / 6)
End Sub
Function fcnXCoord() As Double
fcnXCoord = Selection.Information(wdHorizontalPositionRelativeToPage)
End Function
Function fcnYCoord() As Double
fcnYCoord = Selection.Information(wdVerticalPositionRelativeToPage)
End Function

PowerPoint Shape Export Constant Image Dimensions

I am trying to export a PPT Shape into an image file, however, PowerPoint is re-sizing the shape to the text length.
I know there is an Autosize feature in VBA, however I cannot get the msoAutoSizeTextToFitShape feature working in PowerPoint 2013.
My code is as follows
Sub RunMe()
Dim MyShape As Shape
Dim i As Integer
Dim S(0 To 2) As String
Set MyShape = ActivePresentation.Slides(1).Shapes.AddShape(msoShapeRectangle, 50, 50, 100, 40)
S(0) = "short text"
S(1) = "Medium length text"
S(2) = "Really Really Long and descriptive Text"
For i = 0 To 2
With MyShape
'.TextFrame.AutoSize = PowerPoint.ppAutoSizeMixed
.TextFrame.TextRange.Text = S(i)
.Export "C:\temp\" & "\" & S(i) & ".png", ppShapeFormatPNG
End With
Next i
End Sub
As you will see, the generated image dimensions are different. Is there a way to create images of the same size?
You could either adjust the text size to make sure it fits within the shape or adjust the shape to fit the text size. My guess is that you'd want the former, so have a shot with this:
Sub RunMe()
Dim MyShape As Shape
Dim i As Integer
Dim S(0 To 2) As String
Dim sngOriginalSize As Single
Set MyShape = ActivePresentation.Slides(1).Shapes.AddShape(msoShapeRectangle, 50, 50, 100, 40)
S(0) = "short text"
S(1) = "Medium length text"
S(2) = "Really Really Long and descriptive Text"
For i = 0 To 2
With MyShape
.TextFrame.TextRange.Text = S(i)
' store original text size
sngOriginalSize = .TextFrame.TextRange.Font.Size
' decrement font size until the text fits
' within the shape:
Do While .TextFrame.TextRange.BoundHeight > MyShape.Height
.TextFrame.TextRange.Font.Size = .TextFrame.TextRange.Font.Size - 1
Loop
.Export "C:\temp\" & "\" & S(i) & ".png", ppShapeFormatPNG
' reset the text to original size
.TextFrame.TextRange.Font.Size = sngOriginalSize
End With
Next i
End Sub
I have version 2003 installed on my current PC so the following is NOT TESTED.
According to some websites, TextFrame2 is a new property from 2007 onwards.
You may try msoAutoSizeTextToFitShape on TextFrame2 instead.
EDIT :
I tried this in my home PC with version 2010 and it looks okay. Give it a try.
Replace TextFrame in your code by TextFrame2