How to Add Icon to a Excel Menu/Toolbar Button - excel

I need to add a image to a custom toolbar/menu item which is create through VBA.
For a toolbar item, I tried following code
Set NewBtn = TBar.Controls.Add(Type:=msoControlButton)
With NewBtn
.Picture = LoadPicture("mypic.bmp")
.OnAction = "'MyFunction""" & para1 & """'" //VBA Function
'.Caption = "MyFunction"
.TooltipText = "MyFunction"
.Style = msoButtonCaption
End With
In the above code LoadPicture() does not seem to be working. My toolbar is initializing at the workbook load up event. I noticed that the image is loading to the toolbar button, but in a fraction of second it disappears and only item text is displayed. My image is 16x16 pixel bmp one.
Any help appreciate to get around this problem
Thank you

Use MsoButtonStyle.msoButtonIcon or one of the MsoButtonStyle members that contain the word Icon.

In VBA I store the icons on a worksheet (oTemplate) and transfer them to the buttons using:
with NewBtn
oTemplate.Shapes("picCalcOpt").CopyPicture
.PasteFace

Related

Hide/unhide multiple shapes Excel VBA

I am very new to excel VBA & macros and I am trying to do something that I think is very simple, but I cannot for the life of me figure it out.
I have a shape ("shape 1") that when clicked should show/unhide two shapes ("shape 2" and "shape 3").
By default "shape 2" and "shape 3" should be hidden and only appear when "shape 1" is selected.
Any help will be much appreciated, remembering I am a complete novice here!
Edit:
I have managed to use the below, essentially a copy paste from here, I dont know what it means but it works for the single button. I cant figure out how to extend the code to include multiple objects being shown/hidden. An example of a second object to be shown/hidden at the same time as "july_2022" is "august_2022".
Public HIDE As Boolean
Sub fy ()
ActiveSheet.Shapes("july_2022").Visible = HIDE
If ActiveSheet.Shapes("july_2022").Visible = False Then
HIDE = True
Else
HIDE = False
End If
End Sub
ActiveSheet.Shapes("july_2022").Visible = HIDE is the part that sets the visibility of
a shape (july_2022). Another identical line but with something other than july_2022 would affect a second shape. The rest of the code (If.. Then.. Else.. End If) could be replaced with HIDE=Not(HIDE).
For example, the following code when run will 'toggle' the visibility of two shapes on the Active Sheet called 'Shape2' and 'Shape3'.
Public HIDE As Boolean
Sub fy()
ActiveSheet.Shapes("Shape2").Visible = HIDE
ActiveSheet.Shapes("Shape3").Visible = HIDE
HIDE = Not (HIDE)
End Sub

Set Button to Unlocked

SO I have a sheet that protect and unprotects in VBA. This works fine for the most part EXCEPT for my form reset button, which does not allow anyone to click the button (coded below).
Is there a way to set a property in the button to unlocked? So that when the macro prior to it creates it and then locks the sheet, you will still be able to click it?
With ActiveSheet.Buttons.Add(545, 42, 72, 18)
.Name = "NEWNCR"
.Caption = "New NCR #"
End With
ActiveSheet.Shapes.Range(Array("NEWNCR")).Select
Selection.OnAction = "populatencrnumber"
Maybe something like
ActiveSheet.Shapes.Range(Array("NEWNCR")).Locked = False
This exact example doesn't work of course. But something like that exists, right? Any help would be appreciated.

How to make Chart Legend Items interactive in mschart in a web application

How to make Chart Legend Items interactive in mschart in a web application. I have tried using HitTestResult class. However to get the coordinates for X and Y locations of click, MouseEventArgs class is not supported for charts.Could someone please answer this and preferably share a code snippet.
From the question I guess you want to hide/unhide series on click of legend items. HitResult is used with the desktop version, where we get access to clicked co-ordinates using MouseEventArgs object.
However, to achieve the same on web chart, you can follow the below,
Associate series details with Legend Item's post back property when the chart is being built
legendItem1.PostBackValue = ser.Name & ";" & Chart1.Legends(ChartArea1).CustomItems.Count - 1
This post back can be used to access the clicked series on Chart's click event,
Protected Sub Chart1_Click(ByVal sender As Object, ByVal e As ImageMapEventArgs)
Dim pointData As String() = e.PostBackValue.Split(";"c)
Dim selectedSeries As Series = Chart1.Series(pointData(0))
Dim selectedlegendItem As LegendItem = Chart1.Legends("Default").CustomItems(pointData(1))
If selectedSeries IsNot Nothing Then
If selectedSeries.Enabled Then
selectedSeries.Enabled = False
Else
selectedSeries.Enabled = True
End If
End If
End Sub

export Crystal report to Excel (empty rows)

I am trying to export a report to excel. When I export my report to Excel I am getting blank rows between each detail section. I assume this is because I have a context menu in form of a normal text element which overlies the "normal" text elements.
Does anybody have any advice on how I can stop the blank rows occurring? Is it possible to suppress a text element only when it is exported to Excel?
Thanks!
Try to make it compact.
It should be no space between each object. You should set every object on the same row has the same height and every object on column has the same width.
When there's a space between objects, it will create a cell on excel.
There are 2 articles from Ken Hamady that might be helpfull:
http://kenhamady.com/cru/archives/231
http://www.kenhamady.com/news0506.shtml (scroll to the bottom of the page)
Another option , if you are working with tabular data is to use a report extension like it is shown in this video: https://www.youtube.com/watch?v=3hk6FJ1dvb4
This approach will use the Crystal report as a datasource and will export the data from a grid with much better formatting. The video is using a 3rd party tool, but it is free - http://www.r-tag.com/Pages/CommunityEdition.aspx
Use This Code:
Public Shared Sub ExportDataSetToExcel(ByVal ds As DataTable, ByVal filename As String)
Dim response As HttpResponse = HttpContext.Current.Response
response.Clear()
response.Buffer = True
response.Charset = ""
response.ContentType = "application/vnd.ms-excel"
Using sw As New StringWriter()
Using htw As New HtmlTextWriter(sw)
Dim dg As New DataGrid()
dg.DataSource = ds
dg.DataBind()
dg.RenderControl(htw)
response.Charset = "UTF-8"
response.ContentEncoding = System.Text.Encoding.UTF8
response.BinaryWrite(System.Text.Encoding.UTF8.GetPreamble())
response.Output.Write(sw.ToString())
response.[End]()
End Using
End Using
End Sub
Finally I've solved this issue, after a long time researching. Make the fields inside the details section fill the whole height of the section... no spaces between fields and top and bottom edges.
instead of this

VBA Excel Button resizes after clicking on it (Command Button)

How can I stop a button from resizing? Each time I click on the button, either the size of the button or the font size changes.
Note: I cannot lock my sheet as my Macro will write into the sheet.
Autosize is turned off. I run Excel 2007 on Windows 7 (64 Bit).
I use the following for ListBoxes. Same principle for buttons; adapt as appropriate.
Private Sub myButton_Click()
Dim lb As MSForms.ListBox
Set lb = Sheet1.myListBox
Dim oldSize As ListBoxSizeType
oldSize = GetListBoxSize(lb)
' Do stuff that makes listbox misbehave and change size.
' Now restore the original size:
SetListBoxSize lb, oldSize
End Sub
This uses the following type and procedures:
Type ListBoxSizeType
height As Single
width As Single
End Type
Function GetListBoxSize(lb As MSForms.ListBox) As ListBoxSizeType
GetListBoxSize.height = lb.height
GetListBoxSize.width = lb.width
End Function
Sub SetListBoxSize(lb As MSForms.ListBox, lbs As ListBoxSizeType)
lb.height = lbs.height
lb.width = lbs.width
End Sub
I added some code to the end of the onClick thus:
CommandButton1.Width = 150
CommandButton1.Height = 33
CommandButton1.Font.Size = 11
Seems to work.
I got the issue a slightly different way. By opening the workbook on my primary laptop display, then moving it to my big monitor. Same root cause I would assume.
Seen this issue in Excel 2007, 2010 and 2013
This code prevents the issue from manifesting. Code needs to run every time a active X object is activated.
Sub Shared_ObjectReset()
Dim MyShapes As OLEObjects
Dim ObjectSelected As OLEObject
Dim ObjectSelected_Height As Double
Dim ObjectSelected_Top As Double
Dim ObjectSelected_Left As Double
Dim ObjectSelected_Width As Double
Dim ObjectSelected_FontSize As Single
ActiveWindow.Zoom = 100
'OLE Programmatic Identifiers for Commandbuttons = Forms.CommandButton.1
Set MyShapes = ActiveSheet.OLEObjects
For Each ObjectSelected In MyShapes
'Remove this line if fixing active object other than buttons
If ObjectSelected.progID = "Forms.CommandButton.1" Then
ObjectSelected_Height = ObjectSelected.Height
ObjectSelected_Top = ObjectSelected.Top
ObjectSelected_Left = ObjectSelected.Left
ObjectSelected_Width = ObjectSelected.Width
ObjectSelected_FontSize = ObjectSelected.Object.FontSize
ObjectSelected.Placement = 3
ObjectSelected.Height = ObjectSelected_Height + 1
ObjectSelected.Top = ObjectSelected_Top + 1
ObjectSelected.Left = ObjectSelected_Left + 1
ObjectSelected.Width = ObjectSelected_Width + 1
ObjectSelected.Object.FontSize = ObjectSelected_FontSize + 1
ObjectSelected.Height = ObjectSelected_Height
ObjectSelected.Top = ObjectSelected_Top
ObjectSelected.Left = ObjectSelected_Left
ObjectSelected.Width = ObjectSelected_Width
ObjectSelected.Object.FontSize = ObjectSelected_FontSize
End If
Next
End Sub
(Excel 2003)
It seems to me there are two different issues:
- resizing of text of ONE button when clicking on it(though not always, don't know why), and
- changing the size of ALL buttons, when opening the workbook on a display with a different resolution (which subsist even when back on the initial display).
As for the individual resizing issue: I found that it is sufficient to modify one dimension of the button to "rejuvenate" it.
Such as :
myButton.Height = myButton.Height + 1
myButton.Height = myButton.Height - 1
You can put it in each button's clicking sub ("myButton_Click"), or implement it
a custom Classe for the "onClick" event.
I experienced the same problem with ActiveX buttons and spins in Excel resizing and moving. This was a shared spreadsheet used on several different PC's laptops and screens. As it was shared I couldn't use macros to automatically reposition and resize in code.
In the end after searching for a solution and trying every possible setting of buttons. I found that grouping the buttons solved the problem immediately. The controls, buttons, spinners all stay in place. I've tested this for a week and no problems. Just select the controls, right click and group - worked like magic.
Use a Forms button rather than an ActiveX one, ActiveX controls randomly misbehave themselves on sheets
Do you have a selection command in the buttons macro?
Shortly after I renamed some cells in a worksheet including one that the toggle button selects after its toggle function, the font size shrunk. I fixed this by making sure Range("...").Select included the new cell name, not the coordinates.
It happens when the screen resolution / settings change after Excel has been open.
For example:
Open a workbook that has a button on it
Log in with Remote Desktop from a computer with different screen size
Click on the button => the button size will change
The only solution I found is to close Excel and reopen it with the new screen settings. All instances of Excel must be closed, including any invisible instance executed by other processes without interface must be killed.
Old issue, but still seems to be an issue for those of us stuck on Excel 2007. Was having same issue on ActiveX Listbox Object and would expand its size on each re-calculate. The LinkCells property was looking to a dynamic (offset) range for its values. Restructuring so that it was looking to a normal range fixed my issue.
I had this problem using Excel 2013. Everything for working fine for a long time and all of sudden, when I clicked on the button (ActiveX), it got bigger and the font got smaller at the same time.
Without saving the file, I restarted my computer and open the same Excel file again and everything is fine again.
Mine resized after printing and changing the zoom redrew the screen and fixed it
ActiveWindow.Zoom = 100
ActiveWindow.Zoom = 75
Found the same issue with Excel 2016 - was able to correct by changing the height of the control button, changing it back, then selecting a cell on the sheet. Just resizing did not work consistently. Example below for a command button (cmdBALSCHED)
Public Sub cmdBALSCHED_Click()
Sheet3.cmdBALSCHED.Height = 21
Sheet3.cmdBALSCHED.Height = 20
Sheet3.Range("D4").Select
This will reset the height back to 20 and the button font back to as found.
After some frustrated fiddling, The following code helped me work around this Excel/VBA bug.
Two key things to note that may help:
Although others have recommended changing the size, and then immediately changing it back, notice that this code avoids changing it more than once on single toggle state change. If the value changes twice during one event state change (particularly if the second value is the same at the initial value), the alternate width and height properties may not ever be applied to the control, which will not reset the control width and height as it needs to be to prevent the width and height value from decreasing.
I used hard-coded values for the width and height. This is not ideal, but I found this was the only way to prevent the control from shrinking after being clicked several times.
Private Sub ToggleButton1_Click()
'Note: initial height is 133.8 and initial width was 41.4
If ToggleButton1.Value = True Then
' [Code that I want to run when user clicks control and toggle state is true (not related to this issue)]
'When toggle value is true, simply change the width and height values to a specific value other than their initial values.
ToggleButton1.Height = 40.4
ToggleButton1.Width = 132.8
Else
' [Code that I want to run when user clicks control and toggle state false (not related to this issue)]
'When toggle value is false adjust to an alternate width and height values.
'These can be the same as the initial values, as long as they are in a separate conditional statement.
ToggleButton1.Height = 41.4
ToggleButton1.Width = 133.8
End If
End Sub
For a control that does not toggle, you may be able to use an iterator variable or some other method to ensure that the width and height properties alternate between two similar sets of values, which would produce an effect similar the toggle state changes that I used in this case.

Resources