Applescript - Preserving Image Dimensions When Inserting Images into Excel - excel

I am currently in the final stages of creating a workflow for a very tedious and drawn-out weekly process involving using data in an excel to insert images.
I have managed to work out a combination of Automator and Applescript to achieve a workflow that gets the images in the excel, in their proper place but with my current applescript, they are coming in at a set size, 100 x 100
Here is the issue:
I need all of the images to come in at a height of 100px but I need the width to be flexible. I can't seem to figure out the last piece of this which I assume would be an Applescript command for excel to lock the aspect ratio.
Here is the current applescript I am using for the actual image import:
tell application "Finder"
set imageFolder to "Macintosh HD:Users:adlife:Desktop:Temporary_Image_Hold - Copyright Filings"
end tell
tell application "Finder"
set imageList to files of folder imageFolder
end tell
tell application "Microsoft Excel"
set imageHeight to 100
set imageWidth to 100
set imageCount to count of imageList
set theRange to active cell of worksheet 1 of workbook 1
set thisStep to 0
set theLeft to left position of active cell
repeat with imageFile in imageList
set theTop to (top of active cell)
set newPic to make new picture at beginning of worksheet 1 of workbook 1 with properties {file name:(imageFile as text), height:imageHeight, width:imageWidth, top:theTop, left position:theLeft, placement:placement move}
set thisStep to thisStep + 1
end repeat
end tell
I am running Microsoft Excel 2011 on El Capitan
Any help here would be greatly appreciated!
Thanks!

There is no image property to force the aspect ratio. The check box in user interface is to lock the ration when user moves or resize the image.
The solution is to get image dimensions and apply the ratio (width / height) to your defined image height (=100). Dimensions are provided using the "Image Events" instructions. Then you can used the new width as the "width" property in the "make new picture".
set imageFolder to "Macintosh HD:Users:adlife:Desktop:Temporary_Image_Hold - Copyright Filings"as alias
tell application "Finder" to set imageList to files of folder imageFolder
tell application "Microsoft Excel"
set imageHeight to 100
set imageWidth to 100 -- just a default value, not really needed
set imageCount to count of imageList
set theRange to active cell of worksheet 1 of workbook 1
set thisStep to 0
set theLeft to left position of active cell
repeat with imageFile in imageList
set theTop to (top of active cell)
tell application "Image Events" -- open the image and get dimensions
set theFile to imageFile as alias
set myImage to open theFile -- don't worry, it will not open the fiel for user, only for the script !
set {W, H} to dimensions of myImage
set imageWidth to imageHeight * W / H -- set width using the proportion of image = W/H
end tell
set newPic to make new picture at beginning of worksheet 1 of workbook 1 with properties {file name:(imageFile as text), height:imageHeight, width:imageWidth, top:theTop, left position:theLeft, placement:placement move}
set thisStep to thisStep + 1
end repeat --next image to place
end tell
I tested that script (of course with my own image folder !), and it works with El Capitain and Excel 2011.
As you can see, I also changed the first lines of your code:
To Set imageFolder, you do not need to do it in a tell "Finder" block.However, it must be set as "alias".
To get all files, you can tell "Finder" in single line, without tell / end tell block. (more simple)

Related

Set the font of PPT text from VBA Excel

I am trying to copy the excel content to PPT. using this command
`pptSlide.Shapes.PasteSpecial DataType:=ppPasteHTML, Link:=msoFalse '2 = ppPasteEnhancedMetAEile
Set myShape = pptSlide.Shapes(pptSlide.Shapes.Count)
If myShape.Height <> ExcRng.Height Then
myShape.Table.ScaleProportionally ExcRng.Height / 285
End If`
While I am doing it sets the font of the content gets bigger or smaller depending on the amount of text in the shape.
Can some one tell me if I can fix the size of the font to "8" irrespective of the amount of content.
Since its not a textarea but just a shape.
Shapes containing text in Powerpoint have the property Autosize:
ActiveWindow.Selection.ShapeRange().TextFrame.Autosize = ppAutoSizeNone
With this you can make the text stick to the size you determine in
ActiveWindow.Selection.ShapeRange().TextFrame.TextRange.Font.Size

Resizing and positioning Excel windows correctly on a specific monitor

On opening Excel I want to programmatically create a new window of the workbook, similar to the View Menu -> New Window command in Excel.
Then I want to position and resize these windows to 60% and 40% of the screen resolution respectively and and they should be arranged side by side on a horizontal monitor in a dual monitor system.
The other monitor could either be vertical or horizontal.
All this works well when the horizonal monitor is the primary monitor but if its the secondary monitor.
The first window is not resized properly by width. and so there is an unused area left at the right end of the monitor as shown in the picture.
The resize code that I use is as under
Sub Resize()
Dim mainWindow As Window
Dim secondWindow As Window
'Recalculate new widths and positions
Dim mainWindowWidth As Double
Dim secondWindowWidth As Double
mainWindowWidth = 0.6 * screenWidthPoints
secondWindowWidth = 0.4 * screenWidthPoints
'Assign window variables
If Not IsNull(ActiveWorkbook.Windows(1)) Then
Set mainWindow = ActiveWorkbook.Windows(1)
'switch to normal state before setting width, else will throw error if the window state is maximized and then try to set width
mainWindow.WindowState = xlNormal
mainWindow.Left = horzMonPoints.Left
mainWindow.Top = horzMonPoints.Top
mainWindow.Width = mainWindowWidth
End If
If Not IsNull(ActiveWorkbook.Windows(2)) Then
Set secondWindow = ActiveWorkbook.Windows(2)
secondWindow.Top = mainWindow.Top
secondWindow.Width = secondWindowWidth
secondWindow.Left = mainWindow.Left + mainWindow.Width
secondWindow.Height = mainWindow.Height
End If End Sub
The code to calculate number of monitors and monitor widths and heights, I used windows API calls in VBA.
From this link.

VBA code to reset command button sizes and place them at specific named ranges

I'm using Excel 2010 - I have a number of named Command Buttons and Toggle Buttons that tend to move around or get set to a height and width of 0 so I want to add vba code to reset the command buttons and toggle buttons sizes and locate them at a specific named range.
For example names cb1, cb2, cb3, cb4 to be set at range names cbrn1, cbrn2, cbrn3, cbrn4 respectively with a width of 20 and height of 20 and tb1, tb2, tb2, tb4 to be set at range names tbrn1, tbrn2, tbrn3, tbrn4 with a width of 10 and height of 10. How would I do this by using an if then loop?
Your help would be more than appreciated :-)
I am not sure exactally on your question. But I am just trying to be helpful. You are trying to set the sizes in code? you can make a default size function size(20,20) for the typeof(control) If I had a small code clip I could be more helpful. you said 2010 so I am assuming .net even though VBA usually refers to pre-.net ie vb 4,5,6
For each obj as Control in Me.Controls
Select case typeof obj
if typeof (obj) is ComboBox then
obj.size = (20,20)
elseif typeof(obj) is ToggleButton then
obj.size = (0,0)
end if
next
tried to use spaces there to make it more readable. This code is not exact. but if I am correct on the type of question you are asking this "pseudo-code" should help it did not format correct hope it does this time if not. should still get the idea

How do I make an Excel ActiveX label to be transparent ... at runtime?

I want to put a transparent label on top of a sheet in Excel, so that I can take advantage of the MouseMove event of the label to "draw cells" (aka change their fill color and so on) by mouse click / drag / etc. - since I can't do that on the cells per se.
Now everything works just fine, except that I can't make the label transparent at runtime (aka in VBA) ... while by doing exactly the same thing in Design Mode works as expected. Specifically, I have the code (more or less):
Dim MapLabel As OLEObject
On Error Resume Next
Sheet2.OLEObjects("MapLabel").Delete
Set MapLabel = Sheet2.OLEObjects.Add("Forms.Label.1")
MapLabel.name = "MapLabel"
MapLabel.Placement = xlMoveAndSize
MapLabel.Object.Caption = ""
' Problem line below
MapLabel.Object.BackStyle = fmBackStyleTransparent
' Problem line above
MapLabel.Left = Sheet2.cells(2, 6).Left
MapLabel.Top = Sheet2.cells(2, 6).Top
MapLabel.Width = Sheet2.cells(2,6).Width * 10
MapLabel.Height = Sheet2.cells(2,6).Height * 10
So, in words, I first delete the label named 'MapLabel', then recreate it (the above code goes into a "init" Sub). All the code lines except the one marked produce the desired result. The marked one does set the BackStyle property of the label to fmBackStyleTransparent ... but it doesn't actually make the label transparent. This is frustrating, because it's the same approach that works flawlessly at design time!
Do you have a solution to this? I read about solving similar problems by declaring the label as MsForms.Label or as Control, but the sheet object doesn't have those properties, plus, there are far more label properties which can be set using the OLEObject than with the help of MsForms.Label or Control.
All you need to do after this line:
MapLabel.Object.BackStyle = fmBackStyleTransparent
put this line:
ActiveSheet.Shapes(MapLabel.Name).Fill.Transparency = 1
I hope I helped.
P.S. If you need explanation i will edit my answer.
I had the same problem as you but in Word. The solution for me was to do the following:
In design mode:
Right click on the object
Navigate to Switch to automatic form/Image > Wrapping > In front of the text
Add an empty picture to your label

Get Dimensions of Cell Comment Background Image (.Shape.Fill.UserPicture)

How do I get the dimensions of a cell comment background image?
I have created a comment composed solely of a background image using,
ActiveCell.Comment.Shape.Fill.UserPicture l_strFullPathOfImagePNG
I would like to be able to,
Select the cell.
Run a macro that resizes the comment so the background image is not fuzzy, but rather sized exactly pixel for pixel.
To do this I need to know the dimensions of the background image.
If I know the full path of the image file that created the background image, I can resize the cell comment to achieve what I want:
Sub CommentResizeToFitBackgroundImage()
Dim l_strFullPathOfImagePNG As String
Dim l_lImageWidth_pixels As Long, _
l_lImageHeight_pixels As Long
l_strFullPathOfImagePNG = "C:\……\BackgroundImage.png"
'Get the width and height in pixels of the image to be inserted
GetPNGDimensions _
l_strFullPathOfImagePNG, _
l_lImageWidth_pixels, _
l_lImageHeight_pixels
With ActiveCell.Comment.Shape
'0.75 scale factor was arrived at empirically
'and probably true only for my machine
.Width = l_lImageWidth_pixels * 0.75
.Height = l_lImageHeight_pixels * 0.75
End With
End Sub
However, I would like to do this to existing comments that no longer have a valid image file to read, so I need to get the dimensions of the background image.
.Comment.Shape.ScaleHeight 1, msoTrue looks promising, but fails with the error "The RelativeToOriginalSize argument applies only to a picture or an OLE object".
.Comment.Shape.Fill.Type = MsoFillType.msoFillPicture; so I do indeed have a picture, and not a solid background, gradient, pattern, or texture; so I don't know what the compiler is complaining about.
Lastly, evidently, .Comment.Shape.Fill.PictureEffects doesn't work for comments.

Resources