How To: Hover Over Shape Shows TextBox - excel

I am trying to make it so when a user hovers over a triangle as shown below, a textbox pops up with certain information pertaining to that triangle.
Triangles are drawn with the following function...
Public Sub drawTriangle(theRow As Integer, theColumn As Integer, Optional myColor As System.Drawing.Color = Nothing)
myColor = System.Drawing.Color.Black
Dim theShape As Microsoft.Office.Interop.Excel.Shape = xlWorkSheet.Shapes.AddShape(MsoAutoShapeType.msoShapeIsoscelesTriangle, (xlWorkSheet.Cells(theRow, theColumn)).Left + 18, (xlWorkSheet.Cells(theRow, theColumn)).Top, 15, 14)
theShape.Fill.ForeColor.RGB = ToBgr(myColor)
theShape.Line.ForeColor.RGB = ToBgr(myColor)
End Sub
I haven't been able to find any examples or good documentation about how I would do this, so I thought I'd ask here. Any and all help is much appreciated!!

According to MSDN, comments can be added to ranges. For this to work for your application, you simply need to select the range that corresponds to your shape, and then call AddComment().
The numeric coordinates of a cell can be used to determine the actual Cell name (i.e. E5) by using code like the following(source):
address = xlWorkSheet.Cells(RowVariable, ColVariable).Address
This can be followed up with:
xlWorkSheet.Range(address).AddComment("This is a comment")

Related

VBA Excel - Type Mismatch (Err 13) in Shapes.AddPicture

This one is driving me nuts.
I'm trying to place an image in a spreadsheet.
The following is a created snippet to show what I'm trying to do, but fails at .AddPicture with a TypeMismatch error. What is curious is that despite the failure it still places the image in the correct position, correctly sized.
`
Private Sub cmdLayImage_Click(y as Integer, x as Integer)
Dim lsF As String
Dim loShps As Excel.Shapes
Dim loS As Shape
Dim c As Integer
lsFilename = "Moorland.jpg"
goXsTT.Cells(Y, x).Select
'Fails with Err 13 at this line
Set loShps = goXsTT.Shapes.AddPicture(gsTerrainImagePath & lsFilename, _
msoFalse, msoTrue, ActiveCell.Left, ActiveCell.Top, -1, -1)
c = loShps.Count
loS = loShps(c)
loS.Name = c
MsgBox "Shape Number/Name = " & loS.Name
End Sub
`
I've tried various settings for Link To File and Save To Document, I've used absolute figures for height, width, top and bottom I also tried Csng() each to be sure; nothing works - yet the image is laid, but loShps never gets set.
Any help greatly appreciated.
Thanks Dave
I'm attempting to use the Shapes object as placing an image directly into a selected cell using:
Set loPicInCell = goXsTT.Pictures.Insert(gsTerrainImagePath & lsFilename)
whilst it works, is limiting in the methods available (in particular .OnAction)

Draw Shape Based on Cell Values

All,
I have code that creates a shape based on based on inputted values in the macro itself. I am wanting to have the values of shape type, width, and height specified by the user (location of shape on the sheet is indifferent to me right now). The user would input the aforementioned numerical values for width and height into the cells and click a button which would output the shape type and size the user wants.
In my case, there will be a drop down box for "rectangle" and "circle". I don't know how to get the code to read those words and convert it '1' and '9', respectively. I may just have the user choose 1 or 9 to create the shape.
I would also like to add text to the center of the shape. Again, I have created a code for this but it is within the macro. I would like to have the code reference a cell value instead. I assume it would be the same as above.
Thank you for any assistance.
Sub AddShape()
Dim s As Shape
Dim ws As Worksheet
Set ws = Sheets("Deck Layout")
'add a shape
Set s = ws.Shapes.AddShape(1, 80, 80, 75, 75)
'make it nearly white
s.Fill.ForeColor.RGB = RGB(245, 245, 255)
'show text within it
s.TextFrame.Characters.Text = "1"
s.TextFrame.Characters.Font.ColorIndex = 2
With s.TextFrame.Characters(0, 0)
s.TextFrame.HorizontalAlignment = xlHAlignCenter
s.TextFrame.VerticalAlignment = xlVAlignCenter
.Font.Color = RGB(0, 0, 0)
End With
End Sub
Since you've already got parts of the answer in the comments, I'll focus on the shape picking.
Have a look at this:
Dim ShapeType As MsoAutoShapeType
Select Case LCase(ws.Range("b1").Value)
Case "rectangle"
ShapeType = msoShapeRectangle
Case "circle"
ShapeType = msoShapeOval
End Select
Set s = ws.Shapes.AddShape(ShapeType, 80, 80, 75, 75)
It will find the value in B1, convert it to lower case and the test it for "rectangle" and "circle" and the set the ShapeType to a corresponding value.
You can use 1 and 9 instead, but that is bad practice. Use the defined constants - it will make your code much easier to read.

Is it possible to address a shape with VBA code?

I am replacing all my ActiveX controls with Excel shapes, because of this well know problem. Therefore I replaced each ActiveX Button with a rectangular shape assigning a macro to each shape:
My question is if I can address those 'shape buttons' with my vba code. Something simple like change the backgroung color of the "Review Start" button should be possible, right?
I'm thinking of something like:
Activesheet.shapes("Review Start").background.colorindex = 1
(This code is obviously not working)
One way is this. Assign a variable to the shape and then you can access its properties and methods easily. I'm not sure there's a way without using RGB.
By declaring the variable as Shape type, Intellisense will show you the properties and methods. Also you can use the Object Browser (F2).
Sub x()
Dim s As Shape
Set s = ActiveSheet.Shapes("Review Start") 'better to specify a sheet name
With s
.Fill.ForeColor.RGB = RGB(255, 255, 255)
.TextFrame.Characters.Font.Color = vbBlack
.TextFrame.Characters.Text = "Fred"
End With
End Sub

Add multiple signature blocks into excel document using vba

I'm using vba to create an excel document and fill it in dynamically (already completed and working perfectly). What I need is: to figure out how to add, size, position, and prefill (suggested signer, email, but not the signature itself) the signature block at multiple locations in this document.
I don't even know if this can be done with vba (my searches on the subject have been unhelpful), but I'm hopeful as it will save me a lot of time and tedious work in the future. Any help on this would be welcome.
You may want to place simple text boxes across defined cells (as anchor points) and fill it with some text. To get you started here's the bare minimum that you need:
the actual text box creating Sub which takes all info as parameters:
Sub CreateShapeText(NailToCell As Range, w_pt As Single, h_pt As Single, DTxt As String)
Dim TB As Shape
' create a text box shape
' note: shapes belong to worksheets, therefore we derive a WS from cell.parent
Set TB = NailToCell.Parent.Shapes.AddLabel(msoTextOrientationHorizontal, NailToCell.Left, NailToCell.Top, w_pt, h_pt)
' make its border visible
TB.Line.Visible = msoTrue
' switch off that annoying auto-resize when text is entered
TB.TextFrame2.AutoSize = msoAutoSizeNone
' enter text ... and yes - this object tree is crazy
TB.TextFrame2.TextRange.Characters.Text = DTxt
' as it should be - text is vertical bottom
' but to have more control over the TB, this could be a parameter, too
TB.TextFrame2.VerticalAnchor = msoAnchorBottom
End Sub
and you would call that from wherever in your code as in below example
Sub CallCreate()
CreateShapeText [A1], 132, 32, "sign: me"
CreateShapeText [C12], 132, 32, "sign: you"
End Sub
You take it from here and research what these objects can do for you (e.g. make dotted lines instead of solid for the frame, experiment with font sizes, alignments etc.) and come back with more questions in case ...

Is is possible to insert an image into an excel cell via COM?

I have used automation to insert values into a cell, however I have never seen any documentation, for example, that demonstrate inserting anything other than text and/or formula's.
Has anybody been able to insert an image from an external application?
Dim FileName as string
FileName="c:\text.jpg"
Set NewPic = ActiveSheet.Pictures.Insert(FileName)
NewPic.top=100
NewPic.left=100
If you want to position the picture to a specific cell then select that cell as a range and use that ranges top/left/with to position the picture.
Samples: http://exceltip.com/st/Insert_pictures_using_VBA_in_Microsoft_Excel/486.html
Note: In Excel cells cannot contain pictures. The pictures live on an invisible drawing layer that floats about the cells. They can be positioned based on the cell coordinates, which makes it feel like they are living "in" the cells.
I see it's already been answered, but see my post here.
Basically rather than use the Worksheet.Pictures.Insert method (which the MSDN recommends you don't use directly, and which returns a raw COM object), try the Worksheet.Shapes.AddPicture method instead.
Dim range As Microsoft.Office.Interop.Excel.Range
Dim pic as Microsoft.Office.Interop.Excel.Shape
Dim filePath as String
range = ...
filePath = ...
pic = range.Worksheet.Shapes.AddPicture(filePath, Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoCTrue, range.Left, range.Top, 300, 300)
It's not quite as straightforward because you have to specify the exact position and dimensions, but otherwise, pretty cool!
Sure, the following code gives a good example using the Microsoft Interop libraries:
string excelfilename = #"C:\excelfile.xlsx";
string picturename = #"C:\image.jpg";
object missing = Type.Missing;
Microsoft.Office.Interop.Excel.Application app = new ApplicationClass();
Workbook book = app.Workbooks.Add(missing);
Worksheet sheet = (Worksheet)book.ActiveSheet;
Pictures pics = (Pictures)sheet.Pictures(missing);
pics.Insert(picturename, missing);
book.SaveAs(excelfilename, missing, missing, missing, missing, missing, XlSaveAsAccessMode.xlNoChange,
missing, missing, missing, missing, missing);
app.Quit();
app = null;

Resources