Reputation: 1290
Once again I seem stuck with vba word 2011 on Mac. What I want to do is to add continuous TextBoxes to my UserForm. It is for a document to write invoices. The following is the code that I figured out:
Private Sub cbRechnungPosHinzu_Click()
Dim Cntrl As control
Dim PosName As String
Dim BetName As String
Dim i As Integer
i = 2
If UFRechnung.Controls.Count = (20 + (i * 2)) Then
i = i + 1
Else
With UFRechnung
.Height = UFRechnung.Height + 45
.cbRechnungPosHinzu.Top = .cbRechnungPosHinzu.Top + 45
.cbRechnungPosWeg.Top = .cbRechnungPosWeg.Top + 45
.cbRechnungCancel.Top = .cbRechnungCancel.Top + 45
.cbRechnungOk.Top = .cbRechnungOk.Top + 45
PosName = "txtPos" & i
BetName = "txtBet" & i
Set Cntrl = UFRechnung.Controls.Add("Forms.Textbox.1", PosName, True)
With Cntrl
' .Name = PosName #already defined by Set Cntrl
' .Visible = True
' .Enabled = True
.Top = UFRechnung.Controls("txtPos" & (i - 1)).Top + 45
.Left = 20
.Width = 470
.Height = 25
.AutoSize = False
' .Font = "Calibri Light, 14" #These specials do not work on Mac
' .SpecialEffect = fmSpecialEffectSunken
' .TextAlign = fmTextAlignLeft
' .WordWrap = True
End With
Set Cntrl = UFRechnung.Controls.Add("Forms.Textbox.1", BetName, True)
With Cntrl
' .Name = BetName
' .Visible = True
' .Enabled = True
.Top = UFRechnung.Controls("txtBet" & (i - 1)).Top + 45
.Left = 510
.Width = 470
.Height = 25
.AutoSize = False
' .Font = "Calibri Light, 14"
' .SpecialEffect = fmSpecialEffectSunken
' .TextAlign = fmTextAlignLeft
' .WordWrap = True
End With
Selection.GoTo What:=wdGoToBookmark, Name:=("Betrag" & (i - 1))
Selection.MoveRight
Selection.TypeText Text:=vbNewLine
Selection.FormFields.Add(Range:=Selection.Range, Type:=wdFieldFormTextInput) _
.Name = ("Position" & i)
Selection.TypeText Text:=vbTab
Selection.TypeText ("CHF")
Selection.TypeText Text:=vbTab
Selection.FormFields.Add(Range:=Selection.Range, Type:=wdFieldFormTextInput) _
.Name = ("Betrag" & i)
End With
End If
End Sub
Now well my problem is, that it only adds the two TextBoxes the first time I click the button, after that only the part
With UFRechnung
.Height = UFRechnung.Height + 45
.cbRechnungPosHinzu.Top = .cbRechnungPosHinzu.Top + 45
.cbRechnungPosWeg.Top = .cbRechnungPosWeg.Top + 45
.cbRechnungCancel.Top = .cbRechnungCancel.Top + 45
.cbRechnungOk.Top = .cbRechnungOk.Top + 45
seems to work. What am I doing wrong? Any ideas for an easier code?
Selection.GoTo What:=wdGoToBookmark, Name:=("Betrag" & (i - 1))
Selection.MoveRight
Selection.TypeText Text:=vbNewLine
Selection.FormFields.Add(Range:=Selection.Range, Type:=wdFieldFormTextInput) _
.Name = ("Position" & i)
Selection.TypeText Text:=vbTab
Selection.TypeText ("CHF")
Selection.TypeText Text:=vbTab
Selection.FormFields.Add(Range:=Selection.Range, Type:=wdFieldFormTextInput) _
.Name = ("Betrag" & i)
This part also works perfectly.
Thanks in advance. CU Kath
********************************************* Information added *********************************************
By the way:
If UFRechnung.Controls.Count = (20 + (i * 2)) Then
i = i + 1
was my way to check how many Controls are active in my UserForm as I couldn't check if the TextBoxes with a specific name are already existing. As it always adds 2 Textboxes the next time it checks it has increased by 2 checkboxes so for i = 1 I have 22 (20 + 2) controls, for i=2 I have 24 (20 + 4) controls in my UserForm. Hope I made this clearer for you to understand.
(I even found 2-3 mistakes, but still no luck...)
UFSomething = UserForm
cbSomething = ControlButton
txtSomething = TextBox Name
Upvotes: 2
Views: 172
Reputation: 1290
Once again found the answer myself... :)
Here's the code:
Private Sub cbRechnungPosHinzu_Click()
Dim Cntrl As control
Dim PosName As String
Dim BetName As String
Dim k As Integer
k = (((UFRechnung.Controls.Count - 20) / 2) + 1)
With UFRechnung
.Height = UFRechnung.Height + 45
.cbRechnungPosHinzu.Top = .cbRechnungPosHinzu.Top + 45
.cbRechnungPosWeg.Top = .cbRechnungPosWeg.Top + 45
.cbRechnungCancel.Top = .cbRechnungCancel.Top + 45
.cbRechnungOk.Top = .cbRechnungOk.Top + 45
PosName = "txtPos" & k
BetName = "txtBet" & k
Set Cntrl = UFRechnung.Controls.Add("Forms.Textbox.1", PosName, True)
With Cntrl
.Top = UFRechnung.Controls("txtPos" & (k - 1)).Top + 45
.Left = 20
.Width = 470
.Height = 25
.AutoSize = False
End With
Set Cntrl = UFRechnung.Controls.Add("Forms.Textbox.1", BetName, True)
With Cntrl
.Top = UFRechnung.Controls("txtBet" & (k - 1)).Top + 45
.Left = 510
.Width = 470
.Height = 25
.AutoSize = False
End With
Selection.GoTo What:=wdGoToBookmark, Name:=("Betrag" & (k - 1))
Selection.MoveRight
Selection.TypeText Text:=vbNewLine
Selection.FormFields.Add(Range:=Selection.Range, Type:=wdFieldFormTextInput) _
.Name = ("Position" & k)
Selection.TypeText Text:=vbTab
Selection.TypeText ("CHF")
Selection.TypeText Text:=vbTab
Selection.FormFields.Add(Range:=Selection.Range, Type:=wdFieldFormTextInput) _
.Name = ("Betrag" & k)
Exit Sub
End With
End Sub
With the line
k = (((UFRechnung.Controls.Count - 20) / 2) + 1)
I did the trick... I count the amount of controls as the Userform appears by default. Easier than count is
debug.print Userform.controls.count
Mine are 22 normally but I already have txtpos1 and txtbet1 by default in the userform so I have to subtract them for the amount of the Controls as I want to add Textboxes such as txtpos2 and txtbet2 (continuously numbered). So when I have 22 Controls I have one txtpos and one txtbet, when I have 24 controls txtpos2 and txtbet2 are already in the userform... It's actually not so complicated once you see the whole thing. Maybe this might help someone else someday :) (when I was searching for solutions I didn't find anything....)
The work here is done, but thx for the help.
CU Kath
Upvotes: 0
Reputation: 339
I may be confused by the non-english code, but it looks like the variable i is a local variable in the button click handler. It will be reset to 2 each time the button is clicked. Instead, declare it the UserForm (Declarations) scope and initialize it in the UserForm_Initialize() handler.
Upvotes: 1