convert integer to string vba - vba

I can't seem to get a simple line of code to work.
lblTotalPts.Caption = CStr(x)
This is a powerpoint label and x is an accumulator. X is declared as Long. X is holding the correct value when I hover but won't cast to a string.
thanks.

You access the properties of ActiveX shapes a bit differently than normal PPT shapes.
Change this:
lblTotalPts.Caption = CStr(x)
To this:
lblTotalPts.OLEFormat.Object.Caption = CStr(x)

Related

ShapeSheet cell reference error when using Shape.SetFormulas in Visio

When I try to use .SetFormulas to reference Connector shapes on different page, I keep getting the #Name? error. The error would occur in both Visio 2010 and 2016. The code below works for circles and grouped shapes but not my connectors.
Dim formArray as String() = BuildFormulaArray(PreviousPageShapeIDs)
Dim theStream as Short() = StreamBuilder(CurrentShapeIDs, CustPropSection, Row, Cell)
Dim objArray as Object() = formArray.ConverToObject 'convenience function to convert each item to an object
vPage.SetFormulas(theStream, objArray, VisGetSetArgs.visSetBlasGuards)
However, the code below works with the connectors
For i = 0 to formArray.GetUpperBounds(0)
Dim streamInd as Integer = 4*i
vPage.Shapes(theStream(streamInd)).CellsSRC(theStream(streamInd+1),theStream(streamInd+2),theStream(streamInd+3)).FormulaForce = formArray(i)
Next
My formula is referencing cells using the Pages[PageName]!Sheet.ShapeID!CellName method.
I know I can make this work by using CellsSRC or using the shape name in my formula, but I would rather stick with using SetFormulas and shape IDs for speed reasons. Any ideas on why Visio is behaving inconsistently?

VBA Function Not Storing String [duplicate]

This question already exists:
VBA Function not storing variable
Closed 5 years ago.
I am reposting this question, because I haven't gotten an answer and I still can't figure out what I'm doing wrong. My latest efforts to resolve the problem on my own are detailed below the code.
Original post: VBA Function not storing variable
I have included the code of my function. I mostly scrapped this together from things I found online, because I am very much an amateur coder. I am trying to take the trendline of a graph and use it for a mathematical calculation. When I step through this code, it works great. However, when I call the function from another sub, it gives me an error. Error 9: Subscript out of range. When I debug, it shows me the line a = spl(0). The real problem is that the variable "s" remains empty. Why?
Function TrendLineLog() As Double
Dim ch As Chart
Dim t As Trendline
Dim s As String
Dim Value As Double
' Get the trend line object
Set ch = ActiveSheet.ChartObjects(1).Chart
Set t = ch.SeriesCollection(1).Trendlines(1)
' make sure equation is displayed
t.DisplayRSquared = False
t.DisplayEquation = True
' set number format to ensure accuracy
t.DataLabel.NumberFormat = "0.000000E+00"
' get the equation
s = t.DataLabel.Text '<--------- ACTUAL PROBLEM HERE
' massage the equation string into form that will evaluate
s = Replace(s, "y = ", "")
s = Replace(s, "ln", " *LOG")
s = Replace(s, " +", "")
s = Replace(s, " - ", " -")
spl = Split(s, " ")
a = spl(0) '<----------- DEBUG SAYS HERE
b = spl(1)
c = spl(2)
y = 0.5
..... Math stuff
End Function
I have tried adding the creation of the chart to the function to avoid an error with "Active Sheet". I also tried pasting this code into my sub instead of calling a separate function. Still nothing. When I debug and highlight the t.DataLabel.Text, it shows me the correct value, but for some reason s is not saving that value. In the Locals window, t has value, but s is blank (" ").
Yes, of course you will get an error on the line you pointed out. You are calling spl(0) as though it is its own function, though you did not define spl() as a sub (function) anywhere in this code. Or, alternatively (more likely) you are calling it as an array, which also throws up some flags.
Make sure you are defining spl in your code. You never do this. Add line:
Dim spl(1 to 3) As String
Then you should find that spl(1), spl(2), and spl(3) are what you desire.

How to export a PowerPoint shape as a good quality image file

I am trying to generate a few hundred basic graphics where the shape text and background color is based on a text string.
For this i have chosen to use PowerPoint, because i feel that the image styling is quite comprehensive for my function. The only program that i know how to do this is Adobe Photoshop, however i do not have that software.
I have got the export image function to work, however the image quality of the exported graphic is terrible
How could i get this done with a better image processor?
As can be seen, i have a powerpoint slide with a textbox to hold the stringvalues (Rectangle 5) and my "shape" which will be styled by the two RGB values in the text string.
the string value has the following format (pipe delimited)
Textbox 4.Name | Rounded Rectangle 7.Color | Rounded Rectangle 3.Color
Code used:
Private Sub btnProcess_Click()
Dim i As Integer
Dim StringsArray As Variant
Dim StringItems As Variant
' Call getlines to break all lines into separate records in stringsarray
StringsArray = getlines()
For i = 0 To UBound(StringsArray)
StringItems = Split(StringsArray(i), "|")
ActivePresentation.Slides("Slide1").Shapes("TextBox 4").TextFrame.TextRange.Text = StringItems(0)
ActivePresentation.Slides("Slide1").Shapes("Rounded Rectangle 7").Fill.ForeColor.RGB = StringItems(1)
ActivePresentation.Slides("Slide1").Shapes("Rounded Rectangle 3").Fill.ForeColor.RGB = StringItems(2)
ActivePresentation.Slides("Slide1").Shapes("Group 6").Export "C:\temp\file.emf", ppShapeFormatEMF, 150, 150, ppRelativeToSlide
Next i
End Sub
Function getlines() As Variant
Dim mylines As Variant
Dim mytext As String
mytext = ActivePresentation.Slides("Slide1").Shapes("Rectangle 5").TextFrame.TextRange.Text
mylines = Split(mytext, vbCr)
getlines = mylines
End Function
The vector graphics based PPT object as seen on the PowerPoint slide is not affected by the scale so at all zoom level the object will not appear distorted.
I could not find a way to output the object graphic as a true Metafile even the ppShapeFormatEMF format does not generate a vector based EMF, just a much larger image.
My best solution at the end, was to increase the base size of the PPT object and export the shape object using the ppShapeFormatPNG format thereby increasing the level of detail of the image.
Kinda Obvious.

Combobox assignment of variable

If I put:
variableName = namecombobox.selectedItem
or
Dim variablename as type = namecombobox.SelectedIndex
Visual Studio gives me the error
Option Strict disallows conversions from object to string.
I can fix this by putting:
variableName = convert.ToString(namecombobox.SelectedItem)
Are all values contained in a combobox automatically treated as a non-string even when they are string values (in this case "Male" & "Female") and what is the correct way of assigning the value selected in a combobox to a variable?
This is normal, the ComboBox.Items property is a collection of System.Object. You should use the item's ToString() method, just like ComboBox does to generate the visible text.
Dim variableName As String = namecombobox.SelectedItem.ToString()
Or use CStr(), the VB.NET way.
If you are using this, assuming you select 'Selection1' on the combo box:
Dim x As Boolean
Dim MyVariable As String = ""
MyVariable = ComboBox1.SelectedItem.ToString()
If MyVariable = "Selection1" Then
x = True
Else
x = False
Pretend the above code is YOUR code... This is CORRECT for selecting strings from a ComboBox. Insert a breakpoint on the IF statement checking "MyVariable"- you will see the variable content if you hover your mouse over the variable name. It is a quick way to view the contents of your variable. If hovering above the variable shows an empty string ("") or simply Nothing, then it hasn't picked up any selected item.
In my code above, if I clicked an item containing the words "Selection1", the 'MyVariable' would contain a String of "Selection1" and the boolean variable 'x' would also read as TRUE.
If you're getting reading errors by comparing the variable you have issues elsewhere in your code.

Vba - Set transparent color or direct hex value of label backcolor?

I want to insert a label into a PowerPoint presentation. But I don't want any background on there, or, have the background color be the same as the what is underneath.
I've found that 082F68 is the hex code I want. The RGB code is: 8, 47, 104
This color is supposed to be bluish, but when I insert it, it just gets brown.
I really don't want that. I also tried setting label.backcolor to Color.Transparent. But that isn't recognized. Neither is System.Drawing.Color.Transparent either. It just says it needs an object reference.
But really, isn't it possible to use direct hex values for label backgrounds?
(super late response, but in case others have this issue)
This will create a label on slide 1 in the upper-left hand corner. On my system, I get the bluish background color you are talking about.
ActivePresentation.Slides(1).Shapes.AddLabel(Orientation:=msoTextOrientationHorizontal, _
Left:=0, Top:=0, Width:=50, Height:=50).name = "TestLabel"
Dim sh As Shape
Set sh = ActivePresentation.Slides(1).Shapes("TestLabel")
sh.TextFrame.TextRange.Text = "Hello"
sh.Fill.BackColor.RGB = RGB(8, 47, 104)
You can also set the fill transparency to 100% (fill will be see-through):
sh.Fill.Transparency = 1#
I'm not sure what you're using as a "placeholder", but any Shape object will have an ID:
MsgBox "Label ID = " + CStr(sh.Id)
but it is probably easier to refer to it by name as I do above. The ID is a numerical value and isn't the same as the shape index, making it harder to reference the shape by its ID.
Above I programmatically assign the name, but you can also name the shape yourself: Home -> Arrange -> Selection Pane. In the selection pane you can click on the names of all the shapes on the slide to edit them. You can now refer to these shape names in code.
Try *.BackgroundColor = -1 'Transparent