Determine actual and displayed size of picture object in VBA - vba

In VBA, I'm trying to determine both the actual and the displayed size of a picture object on a worksheet. The displayed size can be different from the actual size due to scaling factors. So far, I've found the methods ScaleWidth and ScaleHeight, but I don't want to actually modify the picture object. Any ideas?

Unfortunatelly, it seems that the original measurements are not a public property of a picture. If you do not want to modify the original picture, you can create a duplicate of the said picture just for the scaling purpose.
This function accepts a shape (a picture in our case) and returns an array of Single type (width and height)
Private Function GetOriginalMeasurements(ByRef myShape As Excel.Shape)
Dim shpCopy As Excel.Shape
Dim measurements(1) As Single
Set shpCopy = myShape.Duplicate
' Reset original measurements
shpCopy.ScaleHeight 1, msoTrue
measurements(0) = shpCopy.width
measurements(1) = shpCopy.height
shpCopy.Delete
GetOriginalMeasurements = measurements
End Function
The Main() procedure is just a basic example of how to use it
Sub Main()
Dim myShape As Excel.Shape
Dim measurements() As Single
Dim width As Single
Dim height As Single
Set myShape = ActiveWorkbook.ActiveSheet.Shapes(1)
measurements = GetOriginalMeasurements(myShape)
width = measurements(0)
height = measurements(1)
Debug.Print width
Debug.Print height
End Sub
On my computer the duplicating and deleting of the shape is instant, but if you see some flickering, you may wish to turn off screen updating in that function.

Related

Inserting a image in first page header, and defining its position in VBA

I'm trying to insert a image in the first page header of a document, trough VBA.
There are multiple lines that can do this, but each has it problem, which I will list:
This is my favorite method, but it inserts the image not in the header of first page, but all the remaining ones, and it also doesn't allow me to set the position:
ActiveDocument.Sections(1).Headers(2).Shapes.AddPicture ("C:\1.jpg")
This returns an out of bounds error:
Set shpCanvas=ActiveDocument.Shapes.AddCanvas(Left:=0, Top:=0, Width:=180, Height:=50)
shpCanvas.CanvasItems.AddPicture FileName:="C:\1.jpg", LinkToFile:=False, SaveWithDocument:=True
Inserts the image directly, but its usually out of position, stays in the middle of the header where I'd rather have it on the left
ActiveDocument.Sections(1).Headers(wdHeaderFooterFirstPage).Range.InlineShapes.AddPicture ("C:\1.jpg")
I'm just a beginner with VBA and word, I apologize for any grotesque ideas I might have
The first code example does work for me - I see the picture on the first page. But since you don't describe how your document is structured I may not be testing what you're using...
You should not try to use a canvas.
The difference between a Shape and an InlineShape is that Word handles the latter like a text character. If the third line is positioning the picture in the middle of the line that paragraph is probably formatted as "centered", rather than "left". Try changing the paragraph formatting.
To position the result when using a Shape an object variable is required to be able to handle what has been inserted. For example:
Dim shp As Word.Shape, ils As Word.InlineShape
Set shp = ActiveDocument.Sections(1).Headers(2).Shapes.AddPicture("C:\1.jpg")
shp.Top = 0
shp.Left = 0
An object is declared, then the picture being inserted is assigned to the object, in one step. Subsequently, the object variable can be used to address the picture.
Thanks for your help, more correctly it worked like this
Dim shp2 As Word.Shape
Dim shp3 As Word.InlineShape
Set shp3 = ActiveDocument.Sections(1).Headers(wdHeaderFooterFirstPage).Range.InlineShapes.AddPicture("C:\1.jpg")
Set shp2 = shp3.ConvertToShape
shp2.Top = 0
shp2.Left = 0

VBA macro to increment rotation of selected shape/picture in powerpoint

Basically, I am not much of a programmer and do a lot of drawing and diagramming in PowerPoint for education purposes. I currently use PowerPoint 2016. To increase my workflow speed, I map keyboard shortcuts to macro keys on my keyboard so I get the functionality just by hitting a key on the keyboard.
I am trying to find a macro that I can link to a keyboard shortcut allowing me to increment the rotation of the currently selected shape to … let’s say 2 degrees each time I hit the shortcut.
I'm new to ppt vba. After doing some research so far here is what I came up with. But it doesn't seem to be working.
Public Sub RotateCW2()
Dim shp As Shape
Set shp = ActiveWindow.Selection.ShapeRange(1)
shp.Rotate = shp.Rotate + 2
End Sub
Appreciate the help!
After mix and matching things arround, I think this one is working.
Sub Rotate()
With ActiveWindow.Selection.ShapeRange
.IncrementRotation 2
End With
End Sub
and it works as intended. Thanks guys for your answers.
You were almost there. Try this instead:
Public Sub RotateCW2()
Dim shp As Shape
Set shp = ActiveWindow.Selection.ShapeRange(1)
shp.Rotation = shp.Rotation + 2
End Sub
From Thomas' answer I figured I might try this.
Public Sub RotateCW2()
Dim shp As Shape
Set shp = ActiveWindow.Selection.ShapeRange(1)
shp.Rotate = shp.IncrementRotation(2)
End Sub
This time I get the error "Compole error: Expected Function or variable" and it highlights (.IncrementRotation).
The Shape Object has a series of Increment properties to choose from.
Note: Descriptions copied from MSDN
IncrementRotation( Increment )
"Specifies how far the shape is to be rotated horizontally, in degrees. A positive value rotates the shape clockwise; a negative value rotates it counterclockwise."
IncrementRotationX( Increment )
"Specifies how much (in degrees) the rotation of the shape around the x-axis is to be changed. Can be a value from ? 90 through 90. A positive value tilts the shape up; a negative value tilts it down."
IncrementRotationY( Increment )
"Specifies how much (in degrees) the rotation of the shape around the y-axis is to be changed. Can be a value from ? 90 through 90. A positive value tilts the shape to the left; a negative value tilts it to the right."
Public Sub RotateCW2()
Dim shp As Shape
Set shp = ActiveWindow.Selection.ShapeRange(1)
shp.Rotate = shp.IncrementRotation 2
End Sub

catia vba Drafting sheet format frame display

I have a little macro that adds a new sheet to a drawing intended for creating a .dxf file for the laser cut-out of sheet-metal parts.
Sub CATMain()
Dim drawingDocument1 As DrawingDocument
Set drawingDocument1 = CATIA.ActiveDocument
Dim drawingSheets1 As DrawingSheets
Set drawingSheets1 = drawingDocument1.Sheets
Dim drawingSheet1 As DrawingSheet
Set drawingSheet1 = drawingSheets1.Add("Laser")
drawingSheet1.PaperSize = catPaperA0
drawingSheet1.[Scale] = 1#
drawingSheet1.Orientation = catPaperLandscape
CATIA.StartCommand "Unfolded View"
End Sub
I'd like to make an addition to this macro where it removes the border of the sheet format.
The manual method of doing this is shown in the following screenshot:
So I either need to find a VBA command to untick that box, or a command to use Sheet Style "NoBorderTest" (as seen in the screenshot).
I couldn't find a way to do either, any help would be appreciated.
I'm trying to accomplish exactly the same thing you are, I'm making drawings that contain a view scaled 1:1 that can be exported as a DXF for 3 axis machining. The format is very annoying, it makes it difficult to see the part profile if its size is similar to the paper dimensions. The drawing format "shadow" hides the geometry.
The work around that I came up with was to set the paper height and paper width to very small numbers, 0.0000001 seemed to work fine. The paper height and paper width properties are exposed APIs that you can work with:
Dim DXFRoot As DrawingRoot = DXFRepRef.GetItem("CATDrawingAccess")
Dim DXFSheets As DrawingSheets = DXFRoot.Sheets
Dim DXFSheet As DrawingSheet = DXFSheets.ActiveSheet
DXFSheet.PaperSize = CatPaperSize.catPaperUser
DXFSheet.Scale = 1
DXFSheet.SetPaperHeight(0.0000001)
DXFSheet.SetPaperWidth(0.0000001)

Change comment size relative to original size of fill picture in Excel vba

Trying to write a VBA excel macro that will allow me to insert a picture as a pop-up on mouseover of a cell.
I'm accomplishing this by inserting a comment in the cell and setting the comment's fill to be a specified picture.
I would like the picture to maintain its original scaling
After setting the comment to use the picture as a fill background, I can manually right-click the cell, click edit comment, right-click the comment, go to the "size" tab, select "Relative to original picture size" checkbox, and set scale height and size to be 100%, which achieves the desired effect, as shown below:
Recording a macro to see what the VBA to replicate this is results in nothing being recorded.
Using the targetComment.Shape.ScaleHeight 1, msoTrue results in an error:
Run-time error '-2147024891 (80070005)':
The RelativeToOriginalSize argument applies only to a picture or an OLE object
Here is a screenshot of the VBA code that generates this error:
Does anyone know how to access what is in the dialog box via VBA???
Using a comment to show an image with scaling can be done. The trick is to calculate the scaling factor yourself and apply it to the image. I've used the Windows Image Acquisition Automation Layer to access the image file's dimensions.
The example below accesses a JPG image in my Temp directory and adds it to a cell's comment with appropriate scaling.
Option Explicit
Sub test()
'--- delete any existing comment just for testing
If Not Range("C5").Comment Is Nothing Then
Range("C5").Comment.Delete
End If
InsertCommentWithImage Range("C5"), "C:\Temp\laptop.jpg", 1#
End Sub
Sub InsertCommentWithImage(imgCell As Range, _
imgPath As String, _
imgScale As Double)
'--- first check if the image file exists in the
' specified path
If Dir(imgPath) <> vbNullString Then
If imgCell.Comment Is Nothing Then
imgCell.AddComment
End If
'--- establish a Windows Image Acquisition Automation object
' to get the image's dimensions
Dim imageObj As Object
Set imageObj = CreateObject("WIA.ImageFile")
imageObj.LoadFile (imgPath)
Dim width As Long
Dim height As Long
width = imageObj.width
height = imageObj.height
'--- simple scaling that keeps the image's
' original aspect ratio
With imgCell.Comment
.Shape.Fill.UserPicture imgPath
.Shape.height = height * imgScale
.Shape.width = width * imgScale
End With
End If
End Sub

Word VBA: ConvertToShape method makes image disappear

I wrote some code for a client which isn't working correctly on his machine (Win 10, Office 365) but is on mine (Win 10, Office 2016). The code inserts an image to the header then positions it and resizes it. I use the ConvertToShape method so I can access properties like width, height and position of the Shape class.
Dim pic As Shape
Dim shp As Word.InlineShape
Set shp = thisDocument.Sections.Item(1).Headers(wdHeaderFooterPrimary).Range.InlineShapes.AddPicture(fpImage) ' insert the image to the header
Set pic = shp.ConvertToShape ' THIS LINE CAUSES THE PROBLEM
The method causes the image to disappear. 'Pic' is still available and setting it's properties causes no error, but it is not visible. It's .visible property returns true.
Any ideas? Thanks.
Answer provided to cross-post at Microsoft Community
There is a way to do this with only an inline shape, by setting up a table to position the text on the left and the picture on the right. An additional advantage of this method is that, if you set the table's AutoFitBehavior property to wdAutoFitFixed and set the column width to the width you want for the shape, Word will automatically resize the picture to that width and keep the aspect ratio.
Here's a little sample macro:
Sub x()
Dim fpImage As String
Dim strExistingHeaderText
Dim tbl As Table
Dim shp As InlineShape
fpImage = "D:\Pictures\bunnycakes.jpg"
With ActiveDocument
strExistingHeaderText = _
.Sections(1).Headers(wdHeaderFooterPrimary).Range.Text
Set tbl = .Tables.Add( _
Range:=.Sections(1).Headers(wdHeaderFooterPrimary).Range, _
numrows:=1, numcolumns:=2, _
AutoFitBehavior:=wdAutoFitFixed)
tbl.Columns(2).Width = InchesToPoints(1.5)
tbl.Columns(1).Width = InchesToPoints(5#)
tbl.Cell(1, 1).Range.Text = strExistingHeaderText
'tbl.Borders.Enable = False
Set shp = tbl.Cell(1, 2).Range.InlineShapes.AddPicture(fpImage)
End With
End Sub