VSTO VB.NET Excel Chart, Subtitle for Chart - How to access chart text box from code? - vb.net

I have a chart in Excel that I'd like to use a subtitle on. There is no SubTitle Property on the chart object, so I looked in the Excel User interface and found that you can only create a sub-title by using a text box. The text box looks like its associated with the chart, not the worksheet, so how would I access that text box from code?
Thanks...

There is another possibility, and that is to include the subtitle in the chart title, for example:
Dim co As ChartObject
Set co = Sheet1.ChartObjects(1)
co.Chart.HasTitle = True
co.Chart.ChartTitle.Text = "First Quarter Sales" & vbCrLf & "Subtitle"
co.Chart.ChartTitle.Characters(22, 8).Font.FontStyle = "Bold Italic"

Related

Populate Local Branch Address in Footer

I'd like a Word document template that, when opened, requests office location name then enters the proper address and phone number for that location in the footer.
I thought to use a drop down list to select the location, I.E. Denver, LA, SF, NY etc. then have the code enter the address and phone number in the footer.
I can get the code to put an address or phone number in the footer but not stacked. The last one overwrites the first.
Sub FooterAddress()
FooterAddress Macro
With ActiveDocument.Sections(1)
.Footers(wdHeaderFooterPrimary).Range.Text = "Local Office City"
.Footers(wdHeaderFooterPrimary).Range.Text = "123 My Street | City, ST 12345-6789"
.Footers(wdHeaderFooterPrimary).Range.Text = "Phone 800.123.4567"
End With
End Sub
I tried to use formatting codes for headers and footers to choose the font and center the text but just got errors. Didn't try changing the color yet.
I can't get the dropdown to launch the script and I can't get the template to launch the dropdown on open either.
For example:
Sub FooterAddress()
Application.ScreenUpdating = False
ActiveDocument.Sections(1).Footers(wdHeaderFooterPrimary).Range.Text = _
"Local Office City" & vbCr & _
"123 My Street | City, ST 12345-6789" & vbCr & _
"Phone 800.123.4567"
Application.ScreenUpdating = True
End Sub
In the footer, create bookmarks for each line (Insert>Bookmark). Then write to the bookmarks with:
ActiveDocument.Sections(1).Footer(wdHeaderFooterPrimary).Range.Bookmarks("Bookmarkname").Range.Text = "AddressText"
You can set the footer up in the template and format it as you require so that you don't need to apply formatting in your code.
Place your cursor Where you want the office address to be and go to Insert | Quick Parts | Document Property | Company Address. Without changing the selection go to the Developer tab and click on Properties to display the Content Control Properties dialog. Click the box marked "Allow carriage returns", as shown below, and click OK.
Now move your cursor to where the phone number should be and from the same menu as before add Company Phone. You don't need to do anything else with this content control.
You can use the code below to set the values of the content controls.
Public Sub WriteOfficeAddress(ByVal officeAddress As String, ByVal officePhone As String)
Dim cxpTarget As CustomXMLPart
Dim strNamespace As String
strNamespace = "http://schemas.microsoft.com/office/2006/coverPageProps"
strNodeName = "/ns0:CoverPageProperties[1]/ns0:"
Set cxpTarget = ActiveDocument.CustomXMLParts.SelectByNamespace(strNamespace).Item(1)
cxpTarget.SelectSingleNode(strNodeName & "CompanyAddress").Text = officeAddress
cxpTarget.SelectSingleNode(strNodeName & "CompanyPhone").Text = officePhone
End Sub
For your code to respond to a new document being created from your template you need to add code into the ThisDocument module. Use the drop downs at the top of the code window to create a Document New event, as shown below.

Edit Form Control Label Caption on VBA

Trying to edit the text of a form control label named lblsearchreminder and make sure that the font is Arial and size 20. I am pulling the edited text from an ActiveX textbox1 and trying to make that the caption of the label. If anyone has any insight I would greatly appreciate it.
Sub btnAltCustomSearch_Click()
Dim strTextBox As String
If Worksheets("User Interface").OLEObjects("TextBox1").Object.Value = "" Then
ErrorX.Show
Else
strTextBox = Worksheets("User Interface").OLEObjects("TextBox1").Object.Value
Worksheets("Muscle Wasting Database").Shapes("lblsearchreminder") = vbCrLf & "Disease: All" & vbCrLf & vbCrLf & "Keyword: " & strTextBox
lblsearchreminder.Object.Characters.Text = "Arial"
lblsearchreminder.Object.Font.Size = 20
End If
End Sub
Yes the textbox1 is an ActiveX textbox control, but I will ideally use a Form Control label. So I am trying to pull the typing from the ActiveX textbox and use it in the Form Control label – Thor Nagel 3 hours ago
Unfortunately you can't manipulate the font-size/name, color or style of a Form Control Label. If you notice the formatting items have been "grayed out" in the Font group on the Excel Ribbon.
To set a Text is easy
Dim lblsearchreminder As Shape
Set lblsearchreminder = Sheet1.Shapes("Label 1")
lblsearchreminder.TextFrame.Characters.Text = "Hello"
But you cannot do (Even though Intellisense allows it)
lblsearchreminder.TextFrame.Characters.Font.Name = "Arial"
or
lblsearchreminder.TextFrame2.TextRange.Characters.Font.Name = "Arial"
I would recommend using a TextBox shape or an ActiveX Label instead.
Similarly you cannot change the font using
lblsearchreminder.TextFrame.Characters.Font.Size= 20
I don't think you're using a Form Control. TextBox1 would be the default name for an ActiveX control, which would be coherent with using .OLEObject.Object to retrieve it.
Declare a MSForms.TextBox variable for it.
Dim box As MSForms.TextBox
Now assign it to the .OLEObject.Object:
Set box = Worksheets("User Interface").OLEObjects("TextBox1").Object
If the sheet "User Interface" exists in ThisWorkbook at compile-time, give it a code name (F4; set the (Name) property to e.g. UserInterfaceSheet) - then you can use that identifier directly, without needing to pull the worksheet from the Worksheets collection:
Set box = UserInterfaceSheet.OLEObjects("TextBox1").Object
Now you have an early-bound object reference to play with, you'll have IntelliSense to guide you. MSForms.TextBox does not have a .Characters property. It does have a .Font property though, so you can start exploring that:
So the Font property is an object of type NewFont; using the Object Browser (F2) you can browse its members:
Thus:
box.Font.Name = "Arial"
box.Font.Size = 20
Should do it.
Watch out for misleading names and prefixes: lblsearchreminder reads like you're looking at a MSForms.Label control, not a TextBox. txtSearchReminder would be more appropriate, or if you prefer control-agnostic names, SearchReminderBox works as well.
Also Error is a function from the VBA.Conversion module, that you are shadowing here.

Convert Figure Alt Description to Picture Caption in Word

I need a way to get an image's Alt Description (specified in HTML) into the Word DOCX image's Caption. This needs to be done with many images in a document, so a macro would be best.
Assume an HTML doc with an img tag similar to this
<img src="http://www.example.com/path/to/image/picture01.jpg"
title="picture 01 title"
alt="this is alt text"
caption="this is a caption field">
This HTML doc is imported into Word 2010 (via File, Open). The image will show in the doc.
Now, how to get the 'title' attribute (which shows up in the Format Picture's Alt-Text dialog as the Description - see screenshot below) into the image's Caption?
Note that the caption parameter in the image tag is not converted to a Word Caption for that image.
Sample Alt-Text dialog for an image which shows the image's alt value as the Description
Microsoft Word has a Range.InsertCaption method, which will insert field codes to automatically number images. As you were asking to insert Alternative Text, my feeling is you were using the term caption simply as a directive to get the text beneath each image on its own carriage return. So that's what this code does:
Sub GenerateAltTextCaptions()
Dim AltTextCaption As String
Dim ImageRange As Range
Selection.GoTo What:=wdGoToSection, Which:=wdGoToFirst
With ActiveDocument
For i = 1 To .InlineShapes.Count
AltTextCaption = .InlineShapes(i).AlternativeText
Set ImageRange = .InlineShapes(i).Range
ImageRange.Collapse Direction:=wdCollapseEnd
ImageRange.InsertAfter vbCr
ImageRange.InsertAfter AltTextCaption
Next
End With
End Sub
Yes, I think you've hit on a good way to make the conversion. Glad you were able to find it!
The code below will loop all the InlineShapes in the document and, if the Alternative Text is not empty, create a caption using that text.
InsertCaption is only available in VBA for InlineShapes. The user can insert captions for Shapes (graphics with text wrap formatting) because Word evaluates the selected graphic and creates a Textbox of the same width and positions it immediately below the graphic. The Shape and textbox are not, however, linked together in any way. So this functionality is not offered in VBA and would require some "creative" programming.
Sub InsertCaptionFromAltText()
Dim doc as Word.Document
Dim ils As word.InlineShape
Dim captionText As String
Set doc = ActiveDocument
Set ils = doc.InlineShapes(1)
For each ils in doc.InlineShapes
captionText = ils.AlternativeText
If Len(captionText) > 0 Then
ils.Range.InsertCaption Label:=wdCaptionFigure, _
Title:=captionText, _
Position:=wdCaptionPositionBelow
End If
Next
End Sub

Setting Title of Chart automatically

Is there anyway to have the title of a chart be equal to string variable. I recorded me manually changing the title and it came up as first activating it, then showing my text edit, but how do I do this if I don't want to activate it. I know the name of the chart is chart 1, so was trying to find a way to have the title of chart 1 be set to my string variable (which I generate from another loop elsewhere). Thx
activesheet.chartobjects("chart 1").activate
activesheet.charttitle.text="my text edit"
ActiveSheet.ChartObjects("Chart 1").Chart.ChartTitle.Text = "my text edit"
You can read more about chartObjects here
I prefer this solution:
Dim var As String
Set objChrt1 = Sheets("sheet1").ChartObjects(1)
Set myChart1 = objChrt1.chart
myChart1.ChartTitle.Text = "my text edit"
var = myChart1.ChartTitle.Text
MsgBox var

Entering information into the Notes section of a PowerPoint slide using VBA

I am trying to find out how you write VBA to enter a text box into a slide, and enter text. I am also trying to find vba for entering text into the notes section of a PowerPoint slide.
Any help would be greatly appreciated. I have tried to find a site specifically for this, but have not been able to do so
Entering text into a PPT slide is about the same as entering into the notes section.
You have to start out with a Slide object reference, which represents the slide you're adding to; and you add a text box shape to the slides' shapes collection.
Example:
Sub AddTextBoxToSlide()
Dim oDestSlide As PowerPoint.Slide
Set oDestSlide = ActivePresentation.Slides(1)
Dim slideWidth As Single
Dim slideHeight As Single
slideWidth = oDestSlide.Parent.PageSetup.SlideWidth
slideHeight = oDestSlide.Parent.PageSetup.SlideHeight
Dim oTextBox As PowerPoint.Shape
Set oTextBox = oDestSlide.Shapes.AddTextbox( _
Orientation:=msoTextOrientationHorizontal, _
Left:=0, _
Top:=0, _
Width:=slideWidth, _
Height:=slideHeight / 12)
oTextBox.TextFrame.TextRange.Text = "Shape text here"
End Sub
All this does is adds a text box shape to the first slide in the active presentation at the top of the slide. It is as wide as the slide and 1/12th the height of the slide. The parameters for Shapes.AddTextbox() are pretty self-explanatory...
To add to the notes section, I just use the NotesPage object on the slide your notes page is in...so the above code would be about the same, except:
Set oTextBox = DestSlide.NotesPage.Shapes.AddTextbox(msoTextOrientat...
This is an old question, but since you can't record macros in PowerPoint, people will be searching for questions like this until you can.
I didn't need this for adding text to slides, but I tried it for adding text to Notes. However, in Outline View, nothing appeared in my Notes section. It wasn't until I went to View-->Notes Page, and I saw the message I'd added -- at the top of the screen.
You see, when you change Set oTextBox = oDestSlide.Shapes to Set oTextBox = oDestSlide.NotesPage.Shapes, you're not adding text to the Notes. You're adding a textbox to the notes. And that textbox appears only in Notes Page view (at the top of the screen, until you move it).
What we really want to do is add our text to Placeholder 2 (the notes area) on the notes page, like this:
oDestSlide.NotesPage.Shapes.Placeholders(2).TextFrame.TextRange.InsertAfter "Notes text here"