The scenario is trying to adjust font size to get a nice graphic arrangement, or trying to decide where to break a caption/subtitle.
a) In XL VBA is there a way to find out whether a text on a textbox, or caption on a label, still fits the control?
b) Is there a way to know where was the text/caption broken on multiline control?
I gave this a rest, gave it enough back-of-head time (which produces far better results than "burp a non-answer ASAP, for credits"), and...
Function TextWidth(aText As String, Optional aFont As NewFont) As Single
Dim theFont As New NewFont
Dim notSeenTBox As Control
On Error Resume Next 'trap for aFont=Nothing
theFont = aFont 'try assign
If Err.Number Then 'can't use aFont because it's not instantiated/set
theFont.Name = "Tahoma"
theFont.Size = 8
theFont.Bold = False
theFont.Italic = False
End If
On Error GoTo ErrHandler
'make a TextBox, fiddle with autosize et al, retrive control width
Set notSeenTBox = UserForms(0).Controls.Add("Forms.TextBox.1", "notSeen1", False)
notSeenTBox.MultiLine = False
notSeenTBox.AutoSize = True 'the trick
notSeenTBox.Font.Name = theFont.Name
notSeenTBox.SpecialEffect = 0
notSeenTBox.Width = 0 ' otherwise we get an offset (a ""feature"" from MS)
notSeenTBox.Text = aText
TextWidth = notSeenTBox.Width
'done with it, to scrap I say
UserForms(0).Controls.Remove ("notSeen1")
Exit Function
ErrHandler:
TextWidth = -1
MsgBox "TextWidth failed: " + Err.Description
End Function
I feel I'm getting/got close to answer b), but I'll give it a second mind rest... because it works better than stating "impossible" in a flash.
I'm sure there is no way to do this with the ordinary Excel controls on the Forms toolbar, not least because (as I understand it) they are simply drawings and not full Windows controls.
The simplest approach may be to make a slightly conservative estimate of the maximum text length for each control, through a few tests, and use these to manage your line breaks.
This can be achieved by taking advantage of the label or textbox's .AutoSize feature, and looping through font sizes until you reach the one that fits best.
Public Sub ResizeTextToFit(Ctrl As MSForms.Label) 'or TextBox
Const FONT_SHRINKAGE_FACTOR As Single = 0.9 'For more accuracy, use .95 or .99
Dim OrigWidth As Single
Dim OrigHeight As Single
Dim OrigLeft As Single
Dim OrigTop As Single
With Ctrl
If .Caption = "" Then Exit Sub
.AutoSize = False
OrigWidth = .Width
OrigHeight = .Height
OrigLeft = .Left
OrigTop = .Top
Do
.AutoSize = True
If .Width <= OrigWidth And .Height <= OrigHeight Then
Exit Do 'The font is small enough now
.Font.Size = .Font.Size * FONT_SHRINKAGE_FACTOR
.AutoSize = False
Loop
.AutoSize = False
.Width = OrigWidth
.Height = OrigHeight
.Left = OrigLeft
.Top = OrigTop
End With
End Sub
Related
I want to insert a shape in Word above the picture where ever user clicks.
I have written the program below, but sometimes it is placing the rectangle incorrectly and inserting it twice: once where I need it and again somewhere else.
Why is the Shape being inserted twice?
Private WithEvents app As Word.Application
Private Sub app_WindowSelectionChange(ByVal Sel As Selection)
Cancel = True
Call CurosrXY_Pixels
End SuB
Sub CurosrXY_Pixels()
ActiveDocument.Shapes.AddShape(msoShapeRectangle, fcnXCoord, fcnYCoord, 20#, 16#).Select
With Selection
.ShapeRange.TextFrame.TextRange.Select
.Collapse
.Font.Name = "Arial"
.Font.Size = 7
.Font.Bold = False
.Paragraphs.FirstLineIndent = 0
.Paragraphs.RightIndent = -10
.Paragraphs.LeftIndent = -10
.Paragraphs.Alignment = wdAlignParagraphCenter
.TypeText Text:=11
.ShapeRange.LockAspectRatio = msoCTrue
End With
End Sub
Function fcnXCoord() As Double
fcnXCoord = Selection.Information(wdHorizontalPositionRelativeToPage)
End Function
Function fcnYCoord() As Double
fcnYCoord = Selection.Information(wdVerticalPositionRelativeToPage)
End Function
The reason the code is firing more than once is because of the use of the Select method. Code changing the selection is the same as the user doing so. The way to avoid this is to work directly with the Word objects.
The code below illustrates this in the procedure CurosrXY_Pixels. A Shape object is declared, then the newly inserted drawing object assigned to it. This then used for setting the formatting and text, in the With block.
Notice I've also passed the Selection object from the event to this procedure, as well as to the two that calculate the co-ordinates. Since, conceivably, the user could click again before the macro finishes, it's important to pass along the original Selection. (That the original code was not doing so probably contributed to the "randomness" of where things were being created since the code, itself, was changing the selection.)
The code line in the app_WindowSelectionChange event to call the other procedures: CurosrXY_Pixels Sel
Sub CurosrXY_Pixels(Sel As Word.Selection)
Dim shp As Word.Shape
Set shp = ActiveDocument.Shapes.AddShape(msoShapeRectangle, fcnXCoord(Sel), fcnYCoord(Sel), 20#, 16#, Sel.Range)
With shp.TextFrame.TextRange
.Font.Name = "Arial"
.Font.Size = 7
.Font.Bold = False
.Paragraphs.FirstLineIndent = 0
.Paragraphs.RightIndent = -10
.Paragraphs.LeftIndent = -10
.Paragraphs.Alignment = wdAlignParagraphCenter
.Text = 11
End With
shp.LockAspectRatio = msoCTrue
End Sub
Function fcnXCoord(Sel As Word.Selection) As Double
fcnXCoord = Sel.Information(wdHorizontalPositionRelativeToPage)
End Function
Function fcnYCoord(Sel As Word.Selection) As Double
fcnYCoord = Sel.Information(wdVerticalPositionRelativeToPage)
End Function
I'm in the process of writing a code to convert a PDF into DOCX. In the first step, i save the PDF as DOCX using acrobat object. The example shown in attachment is from one of the pages. Wrench Image is wrapped as "Behind the text" and it is not part of the table. My questions are,
how do i move it in to or make it as part of the table cell above using VBA. I tried wrap tight etc. It works for some of the images and not for majority of them. As the code goes through 100's of images, user does not have a visual of result from change in wrap format.
When i try to delete first blank paragraph using code, the wrench image shown in attachment gets deleted as it is not part of the table. How do i delete the first empty paragraph without deleting the image if the image is not part of table and is in "behind the text format".
Thanks
Edit1: Conversion of shape to inlineshape (with inline text wrap format) throws the image out of the table as shown in 2nd attachment.
Edit2:
Sub Resizeimage(iDoc As Word.Document)
Dim i As Long
On Error GoTo eh
With iDoc
' For i = .Shapes.Count To 1 Step -1
' With .Shapes(i)
' If .Type = msoPicture Then
' .ConvertToInlineShape
' End If
' End With
' Next
For i = .Shapes.Count To 1 Step -1
'Application.StatusBar = "Resizing & formatting Images - " &
Round((iDoc.Shapes.Count - (i + 1)) / iDoc.Shapes.Count * 100, 0) & "%
completed..."
With .Shapes(i)
If .width > Application.InchesToPoints(6) Then
.LockAspectRatio = msoTrue
.width = Application.InchesToPoints(6.9)
.Left = wdShapeCenter
'.WrapFormat.Type = wdWrapTight
End If
If .width > Application.InchesToPoints(3) Then
.RelativeHorizontalPosition = wdRelativeHorizontalPositionMargin
'.ZOrder msoBringToFront
.Left = wdShapeCenter
'.WrapFormat.Type = wdWrapTight
ElseIf .width > Application.InchesToPoints(1.75) And .width <
Application.InchesToPoints(2.75) And .WrapFormat.Type = wdWrapTight Then
.RelativeHorizontalPosition =
wdRelativeHorizontalPositionLeftMarginArea
.Left = Application.InchesToPoints(0.1)
.ZOrder msoBringToFront
ElseIf .width < Application.InchesToPoints(1.75) Then
.RelativeHorizontalPosition =
wdRelativeHorizontalPositionRightMarginArea
.Left = Application.InchesToPoints(-2)
End If
End With
Next
End With
Exit Sub
eh:
Call Errorhandler("Resizeimage", Err)
End Sub
When a floating shape is converted to inline, it moves to the position where its anchor formerly was. So we can predict the image position by finding the anchor location. After conversion, expand the range to include the picture, then cut it and paste into the table:
Sub Float2Inline()
Dim oRange As Range
Set oRange = ActiveDocument.Shapes(1).Anchor
ActiveDocument.Shapes(1).ConvertToInlineShape
With oRange
.Expand Unit:=wdParagraph
.Cut
End With
ActiveDocument.Tables(1).Rows(2).Cells(3).Range.Paste
End Sub
I'm having an issue in Excel 2007 VBA whereby I'm trying to set the visible property to false on a frame within a UserForm.
Userform1.Frame1.Visible = False
When trying to set the property, excel throws the error:
Run-time error '-2147418113 (8000ffff)':
Could not set the Visible property. Unexpected call to method or property access.
I've researched this and the only thing that I've uncovered is that it might be something to do with not having a control to take the focus. In my case this is not true though as there is a button available to take the focus on another frame. The other frame is set to be visible prior to Frame1 being hidden.
Has anyone else experienced this issue or can help me understand what is causing this error?
Edit - Code Addition
Public Sub fOpenFrame(uf As UserForm, strName As String)
Dim con As Control
Dim i As Long
i = 5
Application.ScreenUpdating = False
With uf.Controls(strName)
.Top = 38.15
.Left = 120
.Height = 400
.Width = 565
.Visible = True
End With
For Each con In uf.Controls
If TypeName(con) = "Frame" And con.Name <> strName And InStr(con.Name, "Menu") < 1 _
And con.Name <> "frmNewAbsenceButton" And con.Name <> "frmExistingAbsenceButton" Then
With con
.Visible = False 'Error occurs here'
.Top = 5
.Left = i
.Height = 20
.Width = 20
End With
i = i + 25
End If
Next con
Application.ScreenUpdating = True
End Sub
Edit 2 - Pictures Added
This is the first frame Frame1. A msgbox pops up and when the user clicks yes, it opens Frame2.
This is Frame2. This frame opens with all the textboxes / comboboxes disabled. The button control 'Edit' is enabled.
I'd prefer to make all frames invisible first (and not to care about their position nor size); after that the only relevant frame can be made visible.
If the sub is in the userform's macromodule you can use Me("Frame4") and refrain from the argument: 'uf as userform'.
Public Sub fOpenFrame(uf As UserForm, strName As String)
for each it in uf.controls
if typename(it)="Frame" then it.visible=false
next
With uf.Controls(strName)
.Top = 38.15
.Left = 120
.Height = 400
.Width = 565
.Visible = True
End With
End Sub
I have been having the same issue intermittently.
After reading the other answers, I added .setfocus call to a valid textbox before I the visible = false call, and it seemed to fix the issue.
With con
textbox1.setfocus 'Adding this seemed to fix the issue
.Visible = False 'Error occurs here'
.Top = 5
.Left = i
.Height = 20
.Width = 20
End With
I have tested in excel 2010 the the code is working fine(I don't have excel 2007)
Please try the below code.
Private Sub Frame1_Click()
End Sub
Private Sub TextBox1_Change()
End Sub
Private Sub UserForm_Initialize()
Me.Frame1.Visible = False
End Sub
I have a userform that has a text box and whatever value is put into the textbox will determine the number of dynamic controls that are added to the user form and then there is a button and once that is clicked I want the dynamic controls to be removed from the userform altogether.
Below shows the code that is used to create the dynamic controls and this code works perfectly
For i = 1 To TextBox1.Value
newPosition = 360
Set cLabel = Me.Controls.Add("Forms.Label.1")
With cLabel
.Caption = "Label " & (i)
.Font.Size = 8
.Font.Bold = True
.Font.Name = "Tahoma"
'.Left = 70
.Left = 36
.Top = switchBoardLevel
.Width = 130
End With
switchBoardLevel = switchBoardLevel + newPosition
Set cButton = Me.Controls.Add("Forms.CommandButton.1")
With cButton
.Name = "CommandButton" & i
.Caption = "Calculate"
.Left = 300
.Top = buttonStartPosition
.Width = 45
.Height = 18
End With
ReDim Preserve TextListBox(1 To i)
Set TextListBox(i).ButtonGroup = cButton
buttonStartPosition = buttonStartPosition + newPosition
Next i
However there is a problem when it comes to removing the dynamically created controls. I have tried numerous ways to remove the controls. The code below is executed when the button is clicked to remove the controls but it just won't work for me and I am going round in circles so It would be greatly appreciated if someone could give me some guidance on the issue.
For Each TextListBox(i).ButtonGroup In Me.Controls
If (TypeOf TextListBox(i).ButtonGroup Is CommandButton) Then
TextListBox(i).ButtonGroup.Visible = False
End If
Next
You haven't given a lot of information but you can't loop that way - you need to loop through the array:
For i = Lbound(TextListBox) to UBound(TextListBox)
If TypeOf TextListBox(i).ButtonGroup Is MSForms.CommandButton Then
TextListBox(i).ButtonGroup.Visible = False
End If
Next
I'm working on a Userform in Excel that has to be dynamically generated each time. It can list many (100+) lines which are all exactly the same in format. These are generated by the following code snippet.
' ctextbox
Set ctl = .Controls.Add("Forms.Textbox.1")
With ctl
.Top = 12 + linetop
.Left = 464.9
.Width = 140
.Height = 18
.Name = FieldName & "_ctextbox"
End With
' cshow
Set ctl = .Controls.Add("Forms.CommandButton.1")
With ctl
.Top = 13.1 + linetop
.Left = 611.35
.Width = 41.95
.Height = 18
.Name = FieldName & "_cshow"
.Caption = "Show All"
End With
' confirm
Set ctl = .Controls.Add("Forms.Checkbox.1")
With ctl
.Top = 13.5 + linetop
.Left = 659
.Width = 44.95
.Height = 17.25
.Name = FieldName & "_confirm"
.Caption = "Confirm"
End With
It would fine except for a random occurrence where the Confirm checkbox appears smaller than the rest. The screenshot below shows what I mean.
Has anyone experienced this issue before?
I would recommend using repainting the Userform after you have added the controls dynamically.
The Repaint method completes any pending screen updates for a specified form. When performed on a form, the Repaint method also completes any pending recalculations of the form's controls.
This method is useful if the contents or appearance of an object changes and you don't want to wait until the system automatically repaints the area. Me.Repaint simply updates the display by redrawing the form
I had the same issue in that my repaint did not work. I solved this by setting the CheckBox AutoSize property to True and I have no problems anymore.