Insert Word Headings in ListBox including numbers with VBA - vba

Currently I use VBA to create headlines at a certain level. For this I use the following code:
Private Sub InsertButton_Click()
'Insert Title
With Word.Selection
.TypeText Text:=FeatureBox.Text & " " & "-" & " " & ComponentBox.Text & " " & "-" & " " & TitleBox.Text
.Font.size = 12
.Font name = "Calibri"
.Font.Bold = True
.style = wdStyleHeading3
.type paragraph
End With
End Sub
I would like to automatically insert each heading on level 3 into a Multicolumn ListBox. Copying the text is less of a problem, but I would like to insert the automatically generated heading number into the ListBox as well. As an additional function I would like to jump to the element selected in the ListBox by double clicking.
Is this even possible? I would be very grateful for your help.
Many thanks and best regards
Ben

Related

Inserting text from VBA UserForm textbox into part of a string

I'm new to VBA and hoping someone could help, if this might even be possible.
A date will be manually added by the user into UserForm TEXTBOX1 which will be placed at a bookmark in the document.
.Bookmarks("BOOKMARK1").Range _
.InsertBefore TEXTBOX1
I have option buttons for the user to select, which will place specific text (depending on the button selected) into the document as follows:
Private Sub OptionButton2_Click()
If Me.OptionButton2.Value = True Then
Set oRng = ActiveDocument.Bookmarks("BOOKMARK2").Range
oRng.Text = "EXAMPLE SENTENCE 1" & Chr(11) & Chr(9) & _
"EXAMPLE SENTENCE 2" & Chr(11) & _
"EXAMPLE SENTENCE 3" & vbNewLine & " "
ActiveDocument.Bookmarks.Add "BOOKMARK2", oRng
End If
End Sub
I am trying to get the date that was entered in TEXTBOX1 to appear at the end of the sentence of EXAMPLE SENTENCE 2 before the & CHR(11) &. Can anybody please help with this? Thank you!
I've tried numerous online searches to find the answer for my problem but haven't come across anything so far unfortunately.
"EXAMPLE SENTENCE 2" & TEXTBOX1.Text & Chr(11)

Insert formatted text in word document with VBA Userform

I have a userform which contains some textboxes and a button.
when button is pressed it insert text at the end of the document. however I need to insert the text as following format:
First line bold - textbox1
Second line is a web link so it should be converted into hyperlink i.e blue, underline - textbox2
Thrid line is simple text not bold. - textbox3 & 4
the following code does insert the text but not formatted as required. how do I insert the text with least code possible?
Private Sub CommandButton2_Click()
Dim CR As Range
Selection.EndKey wdStory
Set CR = Selection.Range
CR.Text = vbCr & TextBox1.Text _
& vbCr & TextBox2.Text _
& vbCr & TextBox3.Text & " at " & TextBox4.Text
End Sub
Possible formatting:
For example:
With ActiveDocument.Range
With .Characters.Last
.Style = wdStyleStrong
.Text = TextBox1.Text & vbCr
End With
.Characters.Last.Font.Reset
.Hyperlinks.Add Anchor:=.Characters.Last, Address:=TextBox2.Text
.Characters.Last.Text = vbCr & TextBox3.Text & " at " & TextBox4.Text
End With
FINAL EDIT - two lines of code to add
If you truly want to make the new code as short as possible
CR.Paragraphs(CR.Paragraphs.Count - 2).Range.Sentences(1).Font.Bold = True
CR.Paragraphs(CR.Paragraphs.Count - 1).Range.Hyperlinks.Add _
CR.Paragraphs(CR.Paragraphs.Count - 1).Range, _
CR.Paragraphs(CR.Paragraphs.Count - 1).Range.Text

Inserting bullet (Chr(183) or Chr(149)) in Word doc using VBA results in very small bullet

I'm using Access VBA code like the following to create a Word doc and insert some formatted text.
One thing I need to do is separate some metadata with a bullet symbol. We do this currently in an Access Report (where we use the symbol explicitly in the Access SQL statement that builds the data for the report), but now need to build a different type of document using VBA.
My research suggests that the bullet is Character Code 183 or 149, but when we use both of those in our VBA code, it inserts small bullets instead of the beefy bullet that we can get from inserting a bullet symbol directly through the Insert>Symbol menu.
Below is some example code, and a screen shot from the output of that code (with the last line manually added to show the size of the bullet we can add manually). Any suggestions on how we can get a big bullet through VBA code?
' declare vars and set up
Dim objWord As Word.Application
Dim doc As Word.Document
Dim WordHeaderFooter As HeaderFooter
Set objWord = CreateObject("Word.Application")
' create doc and insert sample text
With objWord
.Visible = True
Set doc = .Documents.Add
doc.SaveAs CurrentProject.Path & "\TestDoc.doc"
End With
With objWord.Selection
.Font.Name = "Calibri (Body)"
.Font.Size = 12
.TypeText "Here is an example test line with Chr 183. Date: " & Format(Now(), "Long Date") & " " & Chr(183) & " Time: " & Format(Now(), "Medium Time")
.TypeParagraph
.TypeText "Here is an example test line with Chr 149. Date: " & Format(Now(), "Long Date") & " " & Chr(149) & " Time: " & Format(Now(), "Medium Time")
.TypeParagraph
End With
doc.Save
doc.Activate
You need to split your string, insert the symbol in between and then the time.
Try this:
With objWord.Selection
.Font.Name = "Calibri (Body)"
.Font.Size = 12
.TypeText "Here is an example test line with Chr 183. Date: " & Format(Now(), "Long Date") & " "
.InsertSymbol CharacterNumber:=8226, Unicode:=True
.TypeText " Time: " & Format(Now(), "Medium Time")
.TypeParagraph
End With
It produces this:

Adding event code to checkbox in PowerPoint VBA

I am developing a PowerPoint 2010 deck that presents the user with a series of pages containing one statement, one checkbox (built from a label element to enable changing the size of the checkbox) and forward/back arrows on each page.
Since this will be used on numerous projects with varying numbers of pages I am building the “deck” dynamically using PowerPoint VBA to construct the pages dynamically from an Excel spreadsheet containing the list of individual statements.
I have been able to write the VBA code to open the Excel file, read the statements into an array in PowerPoint and construct the appropriate number of pages with all of the elements on the page. To this point everything works fine. Where I am having difficulty is in assigning the click action to the checkbox.
Here is the code that is called by the page building routine to insert the checkbox (obviously there is more code prior to this for accessing the Excel file, creating the pages and adding the “statement” text boxes...all of which works):
Sub AddSelectBox(Index As Integer, pptBuildingSlide As Slide)
'Add Checkbox
With pptBuildingSlide.Shapes.AddOLEObject(Left:=342, Top:=294, Width:=42, Height:=42, ClassName:="Forms.Label.1")
.Name = "label" & Index
.OLEFormat.Object.Font.Name = "Wingdings 2"
.OLEFormat.Object.Font.Charset = "2"
.OLEFormat.Object.Caption = "£"
.OLEFormat.Object.Font.Size = 40
End With
'Add Checkbox Click Code
'(CODE FOR ADDING CLICK EVENT TO EACH BOX GOES HERE)
End Sub
The checkbox on each page has a discreet name keyed to the page number (e.g. Label1, Label2, etc.). I need to add the following code to each checkbox on each page to toggle the checkmark so later in the program I can see which were checked by reading the “caption” attributes. (The font is set to “Wingdings 2” to give a blank box and a checked box on click)
Private Sub Label1_Click()
If Label1.Caption = "£" Then
Label1.Caption = "R"
Else
Label1.Caption = "£"
End If
End Sub
I have searched the web looking for any references to add event code dynamically and found a number of examples (e.g. Assign on-click VBA function to a dynamically created button on Excel Userform) but almost all are for Excel or Access. I should point out that coding is “not my day job” and I have managed to get this far reading “Mastering VBA for Office 2003” and web searching…so my ability to translate those examples to PowerPoint has come up short. Thanks for any help you can offer.
5/29 Additional information:
I came across the .CreateEventProc method as a way to write code into VBA. The example I found was written for Excel at this site. I've gotten this far with it (the message box code would be replaced with the click code but I was just using this for testing to avoid introducing other errors)...
Sub CreateEventProcedure()
Dim VBProj As VBIDE.VBProject
Dim VBComp As VBIDE.VBComponent
Dim CodeMod As VBIDE.CodeModule
Dim LineNum As Long
Const DQUOTE = """" ' one " character
Set VBProj = ActivePresentation.VBProject
Set VBComp = VBProj.VBComponents(Slides(1))
Set CodeMod = VBComp.CodeModule
With CodeMod
LineNum = .CreateEventProc("Click", "Label1")
LineNum = LineNum + 1
.InsertLines LineNum, " MsgBox " & DQUOTE & "Hello World" & DQUOTE
End With
End Sub
...but get a "Compile Error: Sub or Function not defined" at (slides(1)). Any help cleaning it up (if it is in fact an appropriate solution) would be appreciated.
Do you have to use a label? (I understand the size thing but you can maybe add a shape which would be easier.)
Something based on:
This can only work if you allow access to the VBE in Security (cannot be done in code)
Sub makeBox()
Dim strCode As String
With ActivePresentation.Slides(1).Shapes.AddShape(msoShapeRectangle, 10, 10, 20, 20)
.Fill.Visible = False
.Line.Visible = False
With .TextFrame.TextRange.Font
.Name = "Wingdings 2"
.Size = 40
.Color.RGB = vbBlack
End With
.TextFrame.TextRange = "£"
With .ActionSettings(ppMouseClick)
.Action = ppActionRunMacro
.Run = "chex"
End With
End With
strCode = "Sub chex(oshp As Shape)" & vbCrLf & "If oshp.TextFrame.TextRange =" & Chr(34) & "£" & Chr(34) & "Then" _
& vbCrLf & "oshp.TextFrame.TextRange = " & Chr(34) & "R" & Chr(34) & vbCrLf _
& "Else" & vbCrLf & "oshp.TextFrame.TextRange =" & Chr(34) & "£" & Chr(34) & vbCrLf & "End If" & vbCrLf & "End Sub"
With ActivePresentation.VBProject.VBComponents.Add(vbext_ct_StdModule)
.CodeModule.AddFromString (strCode)
End With
End Sub

Using word vba is it possible to detect the font when a sentence is selected?

Does anyone know how to determine the font size of a sentence using word vba?
Try this
Sub FontDetails()
With Selection.Font
MsgBox .Name & ", " & .Size & " points"
End With
End Sub