difficulty in creating an icon that can be sized - excel

I am using the combination of:
Application.DocumentExport SaveFileName, pjPDF, FromDate:=EarliestStart-30, ToDate:=LFin+30
and
xlbook.activesheet.OLEObjects.Add(FileName:="C:\Macros\Target-" & t & ".pdf", Link:=False, _
DisplayAsIcon:=True, IconFileName:= _
"C:\windows\Installer\{AC76BA86-1033-FFFF-7760-0E0F06755100}\_PDFFile.ico", _
IconIndex:=0, IconLabel:="Target-" & t, Left:=1082, Top:=TOffset, Width:=5, Height:=5).Select
xlbook.activesheet.OLEObjects.Placement = 1 'Move and Size with cells
with the intention of placing an icon on an excel worksheet in a specific cell. When run with these commands, I get a square Icon, measuring about 50 x 50 points with the acrobat symbol displayed above the text i.e. "189.PDF, identified as the IconLabel above. I am trying to size the icon to be 8 x 18 points so it fits inside the cell, which measures 10 x 20 points. I would prefer not to also display the Acrobat picture, jut the IconLabel text.
The Application.DocumentExport command above works exactly as it shot, saving a copy of the pdf source to a pdf, even if many pages long.
The OLEObjects.Add command works, but does not allow me to resize the icon so it will fit within the excel cell where I want to place it.
TOffset is the sum of the points from the top of the excel page to the top of the cell that I want to place the icon into. Width and Height above are supposed to represent size values in points for the Icon, but they do not work, either with or without the .OLEObjects.Placement command above.
I tried running the same 2 commands but leaving out the IconIndex field and value out of the OLEObjects string of commands. This produced desired rectangular boxes of the desired dimensions but caused some internal excel issues with being able to save the excel file--- the app did not allog me to save the file due to severe damage to the excel file. I traced the error to not having the IconIndex fieldname in the OLEObjects string. IconIndex is shown to be an optional entry on the OLEObjects.Add Microsoft page
If I assign the IconValue any integer value other than 0, then the icon is created as clear but the dimensions are the same as if IconIndex:=0.
The end goal is to take the saved PDF and place an icon holding the copy of the pdf file into a specific cell on the excel worksheet that operates similar to a button and allows me to write a short identifier to the caption for the icon.
The end result is a standalone workbook that can be distributed with need to also copy/paste all of the pdf's created by the application with it.
Would be appreciative for some leads as to how I can resize the icons, use buttons instead of OLEOPbjects.add, etc. I have tried hyperlinks, but then I have a large workbook of many, many sheets, each holding a single PDF. What I want in the end is a single worksheet which I can use to hold PDF's within Icons for all of the different printouts I have created.
Thanks

Dim TOffset As Integer
xlbook.Sheets("Task Graphics").Select
With xlbook.ActiveSheet
.Range("B2").Select
TOffset = .Range("A1:A" & row - 3).Height - 11
.Cells(row - 3, 18) = Format(LFin - 30, "mm/dd/yy")
.OLEObjects.Add(FileName:="C:\Macros\Target-" & t & ".pdf", Link:=False, _
DisplayAsIcon:=True, IconFileName:= _
"C:\windows\Installer\{AC76BA86-1033-FFFF-7760-0E0F06755100}\_PDFFile.ico", _
IconIndex:=0, IconLabel:="Target-" & t, Left:=1080, Top:=TOffset).Select
.OLEObjects.Placement = 1
.OLEObjects.ShapeRange.LockAspectRatio = msoFalse
.OLEObjects.Width = 14
.OLEObjects.Height = 10
sbDeleteAFile (SaveFileName)
End With
This answer positions the icon on the left side of the cell, leaving space in the same cell to enter some text (LFin-30), which is a date. Not sure why I had to separate the Width and Height declarations separately from the OLEObjects statement, as the OLEObjects.add reference displays that Width and Height are properties of the Icon.
The only issue I have remaining with this is that when you click the icon, the "edit" symbols (the four corners of the icon have edit circles) and if you drag the icon at all, it may resize.

Related

Can I make a photo follow a number on excel or move it from cell to cell by VBA?

I just finished building a chess game on excel by VBA for my school project.
The way I built it is every piece has a number and a color (Green/Red):
1 - Pawn (x16)
2 - King (x2)
3 - Queen (x2)
4 - Bishop (x4)
5 - Night (x4)
6 - Rook (x4)
Just so it will look nicer I wanted to add pictures of each piece that will follow the number, Or that will move by a VBA commend that will come with the commend of moving the piece.
I prefer the first option if possible but the second one is good as well.
The way I move the pieces right now is like that:
MoveNum = Range(Range("j2").Value).Value
Range(Range("j2").Value).Value = 0
Range(Range("j2").Value).FormulaR1C1 = ""
Range(Range("j2").Value).Font.Color = -0
Range(Range("k2").Value).Value = MoveNum
Range(Range("k2").Value).Font.Color = RGB(210, 0, 0)
(This is the one that moves red pieces, the only difference between it and the green is that the RGB is (0, 175, 20) and not (210, 0, 0)
BTW my entire code is based on the numbers so changing/not using them isn't an option.
A time-saving alternative could be to use Unicode characters, instead of images:
♔♕♖♗♘♙♚♛♜♝♞♟
These are Unicode 9812 to 9823 and you can add them to Excel using either the UNICHAR worksheet function, or the ChrW function in VBA.
Also, you could just copy and paste them from above, right into your worksheet, and then manipulate them like you would any other text, including font size.
Here's the knight at 80pt:
My preferred place to look when I need a symbol or icon is (or to identify one) is https://codepoints.net. As of March 2019, Unicode contains almost 140,000 characters, with more being constantly added.
(Be sure to sign the petition to have a pot-leaf emoji added!) 🍁🍂🌿🍃🍀
If your assignment specifically requires you to use images then you can:
move the images with the .Top and .Left properties of the Shapes object
find the position of the cell with the .Top and .Left properties of the Range object.
Example:
Option Explicit
Sub movePicToCell()
'move the image named "Picture 2" on worksheet "Sheet1" to cell "C3"
Dim ws As Worksheet
Set ws = ThisWorkbook.Sheets("Sheet1")
ws.Shapes("Picture 2").Top = ws.Range("C3").Top
ws.Shapes("Picture 2").Left = ws.Range("C3").Left
End Sub

Excel ComboBox/Control text does not show/recognize special characters when active, but does when inactive

I have run into a weird issue.
I have assigned a string value to the list of a combobox that includes a unicode character. In my case, these are triangles up/down (ChrW(&H25B2) = ▲ & ChrW(&H25BC) = ▼) to denote the ASC/DESC of the desired data sort.
Now, the problem is that Excel successfully displays the unicode character in the combobox only when the control is active (when I select or click on it). BUT, when I click away from the control or change focus it displays a question mark (?), as if I am trying to display an unknown character. The character always displays properly inside of a cell, but not a combobox. I was thinking that maybe two different fonts are being displayed depending on active status, but this has not been set, and I see no option for this in the properties. I also tried the decimal method for adding the characters (ChrW(9660) & ChrW(9650)), just in case, but that didn't change anything
What is going on here? How can I have ▲ & ▼ always display properly.
EDIT: Having learned that VBA controls are limited to ANSI characters, and can't display Unicode characters, Is there a way to display the triangles like I want despite ANSI not having triangles or up/down arrows in its character set?
See code and image below to see what I mean.
Sub PopulateSortList()
'Populate Sort Dropdown control
With Sheet1.ComboBox_Sort
.Clear
.AddItem "Time - IN " & ChrW(&H25B2) 'Ascending
.AddItem "Time - IN " & ChrW(&H25BC) 'Decending
.AddItem "Time - OUT " & ChrW(&H25B2) 'Ascending
.AddItem "Time - OUT " & ChrW(&H25BC) 'Decending
.Font.Size = 11
.TextAlign = fmTextAlignLeft
.SelectionMargin = False
.ListIndex = 1
End With
End Sub

From a selected line in a chart, filter a table or pivot table

I am facing a challenge. In a chart with multiple lines, I would like to able when I click on a line or mouse over a line to see the corresponding datapoint in the the table or pivot table...So basically, filtering a table based on the element i click or select on a chart with my mouse.
Do you think that it is achievable ? What would be the methodology ? Is there a VBA code for this ? I have seen examples, but they are working on the oppiste way; click or mouse over an observation and the line is highlighted...
Thanks in advance
saskap
This is really complicated, you have to customize the code for each chart, this example code can be a starting point:
Dim p As Series
Dim pc As Long
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim ch As ChartObject: Set ch = Me.ChartObjects("Chart 1")
With ch
Dim s As Series: Set s = Nothing
On Error Resume Next: Set s = .Chart.SeriesCollection(Target.Value): On Error GoTo 0
If Not p Is Nothing And Not p Is s Then
p.Format.Fill.ForeColor.RGB = pc
End If
If Not s Is Nothing Then
Set p = s
pc = s.Format.Fill.ForeColor.RGB
With s
s.Format.Fill.ForeColor.RGB = RGB(255, 0, 0)
End With
End If
End With
End Sub
The first two rows are global variables that store which Series
object was highlighted last time and what was its original color,
this is needed to restore the original color when a different cell is
selected. Unfortunately global variables in VBA loose their value when the project is reset (e.g. the Stop button is pressed or an error occurs), so it is possible that this code colors a bar and then cannot color it back. If important, these information may be stored in invisible Cells or Chart data but that complicates the code.
The next line means that this is an event handler, a function that is called in response to a certain event, in this case when the selection changes on a certain worksheet (the one on which you insert this - you have to insert this into a worksheet module not a standard code module).
Next we look up the chart based on its name and assign it to a variable, which is of type ChartObject, so early binding will allows us to depend on the support of intellisense (if you type a . it shows members of interfaces).
Then we look up the Series inside the chart based on the name found in the newly selected cell's contents. Since we don't know if the new cell will have a valid name, we have to protect this line with disabling error handling and checking later if the s has a non-nothing value. - This part depends largly on the type of chart and how it represents data, it is possible that you will have to select data based on Series::XValues.
We check if there is a saved value for a previously highlighted bar and if it is different from the current selection, restore its original color.
Finally if the looking up the Series earlier was successful then s is non-null and we save the color of the current bar and highlight it with red fill.

Focus Rectangle Appears on All Listbox Items in VBA

I've created a Userform in Excel VBA, on which there is an unbound Listbox that has its MultiSelect property set to Extended. When that listbox receives focus by any means other than clicking a list item, all items in that list appear with the dotted focus rectangle around them.
Here is some code that shows the phenomenon beside another listbox with MultiSelect set to Single for comparison. Create a Userform, put two Listboxes on it, and add the code to the form. When you launch the form, tab between listboxes to see what I've described.
Private Sub UserForm_Activate()
ListBox1.MultiSelect = fmMultiSelectSingle
ListBox2.MultiSelect = fmMultiSelectExtended
Dim i As Integer
For i = 1 To 15
ListBox1.AddItem String(i, Chr(i + 64))
ListBox2.AddItem String(i, Chr(i + 64))
Next
End Sub
Is there a way to remove the focus rectangles or prevent their appearing?
Thanks,
I have experimented with your code in Excel 2010 and confirm your observation. If I create two list boxes, enter the code provided, start the form and press tab to focus on ListBox2, the dotted lines appear around all rows.
If I create the two list boxes as before, manually set ListBox2/Properties/Multiselect to 2 - fmMultiSelectExtended, run and tab to ListBox2 the nasty lines disapperar.
For me this is rather stable, the form now survives multiple window activation changes, jumpng back/forth etc.
don't ask me why ...

How to plot chart values outside axis maximum?

In previous versions of Excel there was a registry entry that you could create to allow Excel to display values/labels that would be positioned outside the axis min/max using QFE_Bonn dword=1. This is what I have used for Excel 2003: Plot lines that contain labels disappear ...)
I have not been able to find a similar patch or native functionality in Excel 2010 (Office Pro Plus). Any ideas how this can be accomplished, or did MS remove this functionality altogether?
Here are screenshots of examples in Excel 2003. I create a series of data which uniformly exceeds the y-axis maximum. This series' color fill has been removed already
To finish the look, remove the series' border so that it appears invisible. Then replace the series' value labels with the relevant data.
There is a workaround using the DataLabels.Left property which positions the DataLabel relative to the ChartArea.
Here is an example VB solution:
sub FakeLabels()
Dim sF As Double
Dim lOff As Double
Dim p As Double
ActiveSheet.ChartObjects(1).Activate
With ActiveChart
For sF = 1 To .SeriesCollection.Count
If .SeriesCollection(sF).Name = "FakeSeries" Then
'Define the lOff variable by adding 100, or some other value
lOff = .SeriesCollection(sF).Points(1).DataLabel.Left + 100
For p = 1 To .SeriesCollection(sF).Points.Count
.SeriesCollection(sF).Points(p).DataLabel.Left = lOff
Next p
End If
Next sF
End With
It yields the same results, the only new requirement is to keep the values for the "dummy" series within the axis min/max values for the chart.
A pleasant surprise is that re-sizing the chart doesn’t appear to affect the relative placement of the labels.
UPDATED 9-25-2013
I have used the "textbox" approach since first asking this question. But it is extremely clunky to manage the interplay between the labels' position and the textbox positions, their relative position of the PlotArea i.e., when to use .InsideWidth vs. .Width or .InsideLeft vs. .Left and whether there needs to be any sort of hedonic "adjustments" to the points values, as always seem to be the case, they are never quite perfectly aligned.
While perusing the PPT object model reference for some other chart-related inquiries, I stumbled upon this property which appears to replicate the functionality of the previous hotfix/registry hack.
.ShowDataLabelsOverMaximum

Resources