Edit specific line in a textbox - vba

I wrote this macro to generate a textbox with more than one line:
Sub multipleLineTextBox()
Dim Box As Shape
Set Box = ActiveDocument.Shapes.AddTextbox( _
Orientation:=msoTextOrientationHorizontal, _
Left:=50, Top:=50, Width:=200, Height:=200)
Box.Line.Style = msoLineThinThin
Box.Line.Weight = 6
Box.TextFrame.TextRange.Text = "first line" & vbCrLf & "second line"
Box.TextFrame.TextRange.Font.Size = 20
End Sub
The last line edits all the text in the textbox to be size 20.
How can I edit each line separately?

TextRange has a Paragraphs collection. You can loop that or work with each item individually. For example
Dim bxRange As Word.Range
Set bxRange = Bix.TextFrame.TextRange
bxRange.Paragraphs(1).Range.Font.Size = 12
bxRange.Paragraphs(2).Range.Font.Size = 10

Use this:
Sub multipleLineTextBox()
Dim Box As Shape
Set Box = ActiveDocument.Shapes.AddTextbox( _
Orientation:=msoTextOrientationHorizontal, _
Left:=50, Top:=50, Width:=200, Height:=200)
With Box
.Line.Style = msoLineThinThin
.Line.Weight = 6
.TextFrame.TextRange.Text = "first line" & vbCrLf & "second line"
.TextFrame.TextRange.Paragraphs(2).Range.Font.Size = 20
End with
End Sub

Related

Content Control not recognizing content

I was hoping someone could help me work out why the the 'F' value in my code below continues to include my error label in the ErrorMessage String when the Count value is 5?
In the document, the content control contains text just like all the other controls (which work perfectly) but this content Control text value is not being recognised in the VBA code to map error labels.
Have tried just replacing the control and checking the properties match. Debug messages suggest the the value is just being set to the default Content Control Value of "Click or Tap here to input text".
Private Sub Create_Click()
Dim oCC As ContentControl
Dim oCC2 As ContentControl
Dim Mandatory(9) As String
Dim ErrorMessage As String
Dim ErrorCount As Integer
Dim ErrorLabel(9) As String
Dim objDoc As Document
Dim strFilename As String
Dim strFileString As String
Dim Number As String
Mandatory(0) = "A"
Mandatory(1) = "B"
Mandatory(2) = "C"
Mandatory(3) = "D"
Mandatory(4) = "E"
Mandatory(5) = "F"
Mandatory(6) = "G"
Mandatory(7) = "H"
Mandatory(8) = "I"
ErrorLabel(0) = "A Label"
ErrorLabel(1) = "B Label"
ErrorLabel(2) = "C Label"
ErrorLabel(3) = "D Label"
ErrorLabel(4) = "E Label"
ErrorLabel(5) = "F Label"
ErrorLabel(6) = "G Label"
ErrorLabel(7) = "H Label"
ErrorLabel(8) = "I Label"
ErrorMessage = ""
ErrorMessage = "The following mandatory fields are missing: "
For Count = 0 To 8
Set oCC = ActiveDocument.SelectContentControlsByTitle(Mandatory(Count)).Item(1)
MsgBox (oCC.Range.Text)
If Count = 0 Then
Number = ActiveDocument.SelectContentControlsByTitle(Mandatory(Count)).Item(1).Range.Text
End If
If oCC.Range.Text = "Click or tap here to enter text." Or oCC.Range.Text = "0.00" Then
ErrorMessage = ErrorMessage & vbCrLf & vbCrLf & "- " & ErrorLabel(Count)
MsgBox (oCC.Range.Text)
ErrorCount = ErrorCount + 1
End If
Next Count
If ErrorCount > 0 Then
MsgBox (ErrorMessage)
Else
strFileString = Number
MsgBox (strFileString)
strFilename = "Some Text Here" & " - " & strFileString & ".pdf"
With ActiveDocument
NewPath = .Path & "\" & strFilename
.SaveAs2 FileName:=NewPath, FileFormat:=wdFormatPDF
.ExportAsFixedFormat OutputFileName:=strFilename, ExportFormat:=wdExportFormatPDF, _
OpenAfterExport:=True, OptimizeFor:=wdExportOptimizeForPrint, _
Range:=wdExportAllDocument, Item:=wdExportDocumentContent
End With
End If
End Sub
Check there are no other content controls with the same title in the document.
I couldn't test your code for lack of data but from your description I guess that the ErrorMessage must be reset with each loop since it will be changed when used and would naturally retain the modified version thereafter.
Except for what follows the loop, I looked closely at your code in order to understand it. Perhaps, the changes I made will be of some use to you.
Option Explicit
Private Sub Create_Click()
Dim Doc As Document
Dim Mandatory() As String
Dim ErrorMessage As String
Dim ErrorCount As Integer
Dim strFilename As String
Dim strFileString As String ' this appears identical with 'Number'
Dim Number As String
Dim Count As Integer ' loop counter
Set Doc = ActiveDocument
Mandatory = Split("A B C D E F G H I")
Number = Doc.SelectContentControlsByTitle(Mandatory(0))(1).Range.Text
For Count = 1 To UBound(Mandatory) + 1
ErrorMessage = "The following mandatory fields are missing: "
With Doc.SelectContentControlsByTitle(Mandatory(Count))(1).Range
MsgBox "Number = " & Number & vbCr & .Text
If .Text = "Click or tap here to enter text." Or _
.Text = "0.00" Then
ErrorMessage = ErrorMessage & vbCrLf & vbCrLf & "- " & Mandatory(Count) & " Label"
MsgBox (.Text)
ErrorCount = ErrorCount + 1
End If
End With
If Count = 1 Then Exit For
Next Count
If ErrorCount > 0 Then
MsgBox (ErrorMessage)
Else
strFileString = Number
MsgBox (strFileString)
strFilename = "Some Text Here" & " - " & strFileString & ".pdf"
With Doc
NewPath = .Path & "\" & strFilename
.SaveAs2 FileName:=NewPath, FileFormat:=wdFormatPDF
.ExportAsFixedFormat OutputFileName:=strFilename, _
ExportFormat:=wdExportFormatPDF, _
OpenAfterExport:=True, _
OptimizeFor:=wdExportOptimizeForPrint, _
Range:=wdExportAllDocument, _
Item:=wdExportDocumentContent
End With
End If
End Sub
You can have VBA add the useful (some would say necessary) Option Explicit to all new code modules automatically. Select Tools > Options in the VBE window and check "Require Variable Declaration" on the Editor tab.

VBA Auto Update Image Dimensions in Excel

Edit: 17-07-2018 found a solution to retrieve image dimensions in Excel.
I've created a code to retrieve image files in Excel and it's working fine, but once I resize the image it doesn't automatically update its value I need to shift between images before and then go back to the resized imaged to get the value.
Private Sub Worksheet_Change(ByVal Target As Range)
Dim mypic As Picture
If Target.Address = "$A$4" Then
Me.Pictures.Visible = False
With Range("e2")
For Each mypic In Me.Pictures
If mypic.Name = .Text Then
mypic.Visible = True
mypic.Top = .Top
mypic.Left = .Left
Exit For
End If
Next mypic
End With
With ActiveSheet
s = Round(.Shapes(.Range("e2").Value).Height / 72 * 2.54, 2) & "cm"
y = Round(.Shapes(.Range("e2").Value).Width / 72 * 2.54, 2) & "cm"
MsgBox "Picture dimensions are " & vbLf & vbLf & _
"Height: " & s & vbLf & vbLf & _
"Width: " & y
.Range("Q5") = s
.Range("Q6") = y
End With
End If
End Sub
The code as above is there a way to automatically update the values without closing the Excel file or shifting between images.
Thank you in advanced!

Returning prompt value

Hi i'm working on a code that will return a prompt value of each case to a cell ("A1") but I can't seem to get it right, for example if I choose "case 1", I want the value on cell "A1" to be "1 - FCI18-0", is there a way to do this?. Here is my code below :
Sub TestFunction()
ans = InputBox("1 = FCI18-0" & vbCrLf & _
"2 = FCI18-1" & vbCrLf & _
"3 = FCI18-2" & vbCrLf & _
"4 = FCI18-3", "Model")
Sheets("Sheet1").Range("a1").Value = InputBoxPrompt
End Sub
Please help
How about:
Sub TestFunction()
'Declare variables
Dim test(3) As String
Dim i as long
'Set array with cases. Advantage of this set up is flexibility. In a later stage you can
'make a function that reads cases from a table in a (hidden) sheet into this array.
'This way you don't need to add new cases to your code,
'but you can add them in your excel sheet instead.
test(0) = "FCI18-0"
test(1) = "FCI18-1"
test(2) = "FCI18-2"
test(3) = "FCI18-3"
'read the input given by the user. You might want to add some checks here
i = InputBox ("Which case?")
Sheets("Sheet1").Range("a1").Value = test(i-1)
End Sub
Use Select Case
Sub TestFunction()
ans = InputBox("1 = FCI18-0" & vbCrLf & _
"2 = FCI18-1" & vbCrLf & _
"3 = FCI18-2" & vbCrLf & _
"4 = FCI18-3", "Model")
Select Case ans
Case 1: Sheets("Sheet1").Range("a1").Value = "1 = FCI18-0"
Case 2: Sheets("Sheet1").Range("a1").Value = "2 = FCI18-1"
Case 3: Sheets("Sheet1").Range("a1").Value = "3 = FCI18-2"
Case 4: Sheets("Sheet1").Range("a1").Value = "4 = FCI18-3"
End Select
End Sub

Change Text Font within Same Textbox in VBA

I have multiple subs within VBA that all have their output within the same text box (WarningData) in a PPT slide. For example, Sub 1 takes a user selection (a selection they made from a drop down menu within a GUI) and inserts that at the top of the text box. Sub 2 inserts another line of text below that line. Sub 3 inserts additional text below that. I need Sub 1 and 2 to have the same font style, but Sub 3 needs to have a different font.
Here is what Sub 1 and Sub 2 look like:
Private Sub 1() 'Sub 2 is very similar.
Call Dictionary.WindInfo
'Sets the font for the warning information text.
With ActiveWindow.Selection.SlideRange.Shapes("WarningData").TextFrame2.TextRange.Font
.Size = 24
.Name = "Calibri"
.Bold = msoTrue
.Shadow.Visible = True
.Glow.Radius = 10
.Glow.Color = RGB(128, 0, 0)
End With
ComboBoxList = Array(CStr(ComboBox3), CStr(ComboBox4))
For Each Ky In ComboBoxList
On Error Resume Next
'If nothing is selected in ComboBox4, do nothing and exit this sub.
If ComboBox4 = "" Then
Exit Sub
ElseIf ComboBox3 = "" Then
ActiveWindow.Selection.SlideRange.Shapes("WarningData").TextFrame2.TextRange = _
ActiveWindow.Selection.SlideRange.Shapes("WarningData").TextFrame2.TextRange & dict3.Item(Ky)(0)
'Otherwise, if it has a selection, insert selected text.
ElseIf ComboBox3 <> "" Then
ActiveWindow.Selection.SlideRange.Shapes("WarningData").TextFrame2.TextRange = _
ActiveWindow.Selection.SlideRange.Shapes("WarningData").TextFrame2.TextRange & vbCrLf & vbCrLf & dict3.Item(Ky)(0)
End If
Next
Set dict3 = Nothing
End Sub
The following sub is the one that I need to have a different font style:
Private Sub 3()
Call Dictionary.Call2Action
ComboBoxList = Array(CStr(ComboBox7))
For Each Ky In ComboBoxList
On Error Resume Next
'If nothing is selected in ComboBox7 and TextBox9, do nothing and exit this sub.
If ComboBox7 = "" And TextBox9 = "" Then
Exit Sub
'Otherwise, if either has a selection, insert selected text.
ElseIf ComboBox7 <> "" And TextBox9 = "" Then
ActiveWindow.Selection.SlideRange.Shapes("WarningData").TextFrame2.TextRange = _
ActiveWindow.Selection.SlideRange.Shapes("WarningData").TextFrame2.TextRange & vbCrLf & vbCrLf & dict7.Item(Ky)(0)
ElseIf ComboBox7 = "" And TextBox9 <> "" Then
ActiveWindow.Selection.SlideRange.Shapes("WarningData").TextFrame2.TextRange = _
ActiveWindow.Selection.SlideRange.Shapes("WarningData").TextFrame2.TextRange & vbCrLf & vbCrLf & TextBox9
End If
Next
Set dict7 = Nothing
End Sub
Any idea if this is possible?
Thanks!!
I simplified the code using a With statement and added 2 x font lines to show how to set the Font name. Other properties are also available in the Font2 object e.g. .Size, .Bold, .Fill etc.
Private Sub Three()
Call Dictionary.Call2Action
ComboBoxList = Array(CStr(ComboBox7))
For Each Ky In ComboBoxList
On Error Resume Next
With ActiveWindow.Selection.SlideRange.Shapes("WarningData").TextFrame2
'If nothing is selected in ComboBox7 and TextBox9, do nothing and exit this sub.
If ComboBox7 = "" And TextBox9 = "" Then
Exit Sub
'Otherwise, if either has a selection, insert selected text.
ElseIf ComboBox7 <> "" And TextBox9 = "" Then
.TextRange = .TextRange & vbCrLf & vbCrLf & dict7.Item(Ky)(0)
.TextRange.Font.Name = "Calibri"
ElseIf ComboBox7 = "" And TextBox9 <> "" Then
.TextRange = .TextRange & vbCrLf & vbCrLf & TextBox9
.TextRange.Font.Name = "Calibri"
End If
End With
Next
Set dict7 = Nothing
End Sub
Using the TextRange.Paragraphs method I was able to accomplish this task:
Private Sub 3()
Call Dictionary.Call2Action
ComboBoxList = Array(CStr(ComboBox7))
For Each Ky In ComboBoxList
On Error Resume Next
With ActiveWindow.Selection.SlideRange.Shapes("WarningData").TextFrame2
'If nothing is selected in ComboBox7 and TextBox9, do nothing and exit this sub.
If ComboBox7 = "" And TextBox9 = "" Then
Exit Sub
'Otherwise, if either has a selection, insert selected text.
ElseIf ComboBox7 <> "" And TextBox9 = "" Then
.TextRange = .TextRange & vbCrLf & vbCrLf & dict7.Item(Ky)(0)
.TextRange.Paragraphs(3).Font.Size = 18
.TextRange.Paragraphs(3).Font.Name = "Calibri"
.TextRange.Paragraphs(3).Font.Fill.ForeColor.RGB = vbWhite
.TextRange.Paragraphs(3).Font.Bold = msoTrue
.TextRange.Paragraphs(3).Font.Glow.Transparency = 1
ElseIf ComboBox7 = "" And TextBox9 <> "" Then
.TextRange = .TextRange & vbCrLf & vbCrLf & TextBox9
.TextRange.Paragraphs(3).Font.Size = 18
.TextRange.Paragraphs(3).Font.Name = "Calibri"
.TextRange.Paragraphs(3).Font.Fill.ForeColor.RGB = vbWhite
.TextRange.Paragraphs(3).Font.Bold = msoTrue
End If
End With
Next
Set dict7 = Nothing
End Sub

VBA - How do I send new line command (\n) or tab command (\t) to a textbox.textrange.text of a PowerPointS Shape

SlideNumber = 1
Set oPPTSlide = oPPTFile.Slides(SlideNumber)
For y = 1 To oPPTSlide.Shapes.Count
MsgBox oPPTSlide.Shapes(y).Name
Next
With oPPTSlide.Shapes("Title 1")
.TextFrame.TextRange.Text = _
"Operations Monthly Report\n" & _
"April " & _
"2014"
End With
This is the code I have now. The "\n" does cause the text-box I am editing to start a new line. Is it possible? The code, in its context, is working perfectly. The exact text is sent to the text-box though, not two lines of text.
There is no "\n" in Vba instead you should use VbNewLine or VbCrLf or Vblf
Replace this
SlideNumber = 1
Set oPPTSlide = oPPTFile.Slides(SlideNumber)
For y = 1 To oPPTSlide.Shapes.Count
MsgBox oPPTSlide.Shapes(y).Name
Next
With oPPTSlide.Shapes("Title 1")
.TextFrame.TextRange.Text = _
"Operations Monthly Report" & VbCrLf & _
"April " & _
"2014"
End With
I had the problem where the vbNewLine didn't work in the UserForm, but I fixed it by checking the textBox properties and making sure multi-line is true. Give that a try.