再次,我似乎坚持使用Mac上的vba word 2011。 我想要做的是将连续的TextBox添加到我的UserForm。它是一个文件写发票。以下是我想出的代码:
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
现在我的问题是,它只在我第一次单击按钮时添加了两个TextBox,之后只有部分
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
似乎有效。我究竟做错了什么?有关简单代码的任何想法吗?
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)
这部分也很完美。
提前致谢。 CU Kath
*********************************************已添加信息**************
顺便说一下:
If UFRechnung.Controls.Count = (20 + (i * 2)) Then
i = i + 1
是我检查UserForm中有多少个控件处于活动状态的方法,因为我无法检查具有特定名称的TextBox是否已存在。因为它总是在下次检查时添加2个文本框,它增加了2个复选框,因此对于i = 1我有22(20 + 2)个控件,对于i = 2,我的UserForm中有24个(20 + 4)控件。希望我让你更清楚明白。
(我甚至发现了2-3个错误,但仍然没有运气......)
UFSomething = UserForm
cbSomething = ControlButton
txtSomething = TextBox Name
答案 0 :(得分:1)
我可能会对非英语代码感到困惑,但看起来变量i是按钮单击处理程序中的局部变量。每次单击按钮时,它将重置为2。相反,将其声明为UserForm(声明)范围并在UserForm_Initialize()处理程序中初始化它。
答案 1 :(得分:0)
我自己再次找到答案...... :)
以下是代码:
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
使用
行k = (((UFRechnung.Controls.Count - 20) / 2) + 1)
我做了诀窍......我计算默认情况下Userform出现时的控件数量。比计数更容易
debug.print Userform.controls.count
我通常是22,但我在用户窗体中默认已经有txtpos1和txtbet1所以我必须减去它们的控件数量,因为我想添加文本框,如txtpos2和txtbet2(连续编号)。因此,当我有22个控件时,我有一个txtpos和一个txtbet,当我有24个控件时txtpos2和txtbet2已经在userform中......一旦你看到整个事情,它实际上并不那么复杂。也许这可能有一天会帮助别人:)(当我在寻找解决方案时,我没有找到任何东西......)
这里的工作已经完成,但是请求帮助。
CU Kath