Rename textbox

S

salavine1

Hello,

I have a word document with at table that has 5 columns and anywhere
from 0 to 999 rows. Each table cell will have a textbox in it. I need
to change all the textbox names to a standard format as well as
incremental. If a row is deleted in the middle, I need to rename all
the textboxes after the deletion. The creating of this table and
textbox is automated and I don’t have the ability to change that. I
have to format after the fact.

The automated naming is as follows. Where # represents the number.
1st row: SqdMetContNo, SqdLabEquipDesc, SqdDatesUsed, SqdLastCal,
SqdDueDate

2nd – 9th rows: SqdMetContNo__#, SqdLabEquipDesc__#, SqdDatesUsed__#,
SqdLastCal__#, SqdDueDate__#

10th-99th rows: SqdMetContNo_##, SqdLabEquipDesc_##, SqdDatesUsed_##,
SqdLastCal_##, SqdDueDate_##

100 – 999 rows: SqdMetContNo###, SqdLabEquipDesc###, SqdDatesUsed###,
SqdLastCal###, SqdDueDate###


Here is the code I was using and thought that it was working until I
did further testing and found that it didn’t. Any help would be
appreciated.


Private Sub CheckTextBoxNames()
On Error Resume Next

Dim f As Object
Dim tb As TextBox
Dim iRowCounter As Integer
Dim sFieldName As String

iRowCounter = 0
sFieldName = ""

‘ Loops through the all the textbox objects.
For Each f In ActiveDocument.Fields
If f.OLEFormat.ClassType = "Forms.TextBox.1" Then
Set tb = f.OLEFormat.Object

‘ Returns only the left part of the name.
sFieldName = Len(tb.Name) - 3)

' Checks the name to see if it is the first or
after that.
Select Case sFieldName
Case Is = "SqdMetCon"
tb.Name = RenameTextBoxNames
("SqdMetContNo", iRowCounter)
Case Is = "SqdLabEquipD"
tb.Name = RenameTextBoxNames
("SqdLabEquipDesc", iRowCounter)
Case Is = "SqdDatesU"
tb.Name = RenameTextBoxNames
("SqdDatesUsed", iRowCounter)
Case Is = "SqdLast"
tb.Name = RenameTextBoxNames("SqdLastCal",
iRowCounter)
Case Is = "SqdDueD"
tb.Name = RenameTextBoxNames("SqdDueDate",
iRowCounter)
iRowCounter = iRowCounter + 1
Case Is = "SqdMetContNo"
tb.Name = RenameTextBoxNames
("SqdMetContNo", iRowCounter)
Case Is = "SqdLabEquipDesc"
tb.Name = RenameTextBoxNames
("SqdLabEquipDesc", iRowCounter)
Case Is = "SqdDatesUsed"
tb.Name = RenameTextBoxNames
("SqdDatesUsed", iRowCounter)
Case Is = "SqdLastCal"
tb.Name = RenameTextBoxNames("SqdLastCal",
iRowCounter)
Case Is = "SqdDueDate"
tb.Name = RenameTextBoxNames("SqdDueDate",
iRowCounter)
iRowCounter = iRowCounter + 1
End Select
End If
Next f
End Sub


Private Function RenameTextBoxNames(sFieldName As String, iRowCounter
As Integer)
On Error Resume Next

Dim sNewName As String
sNewName = ""

If iRowCounter <= 9 Then
sNewName = sFieldName & "__" & iRowCounter
ElseIf (iRowCounter > 9) And (iRowCounter < 100) Then
sNewName = sFieldName & "_" & iRowCounter
ElseIf iRowCounter >= 100 Then
sNewName = sFieldName & iRowCounter
End If

RenameTextBoxNames = sNewName

End Function
 
D

Doug Robbins - Word MVP on news.microsoft.com

See the article "How to assign a Name to a FormField that doesn't already
have a Name, using VBA” at:

http://www.word.mvps.org/FAQs/MacrosVBA/AssignNameToFmFld.htm


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com

Hello,

I have a word document with at table that has 5 columns and anywhere
from 0 to 999 rows. Each table cell will have a textbox in it. I need
to change all the textbox names to a standard format as well as
incremental. If a row is deleted in the middle, I need to rename all
the textboxes after the deletion. The creating of this table and
textbox is automated and I don’t have the ability to change that. I
have to format after the fact.

The automated naming is as follows. Where # represents the number.
1st row: SqdMetContNo, SqdLabEquipDesc, SqdDatesUsed, SqdLastCal,
SqdDueDate

2nd – 9th rows: SqdMetContNo__#, SqdLabEquipDesc__#, SqdDatesUsed__#,
SqdLastCal__#, SqdDueDate__#

10th-99th rows: SqdMetContNo_##, SqdLabEquipDesc_##, SqdDatesUsed_##,
SqdLastCal_##, SqdDueDate_##

100 – 999 rows: SqdMetContNo###, SqdLabEquipDesc###, SqdDatesUsed###,
SqdLastCal###, SqdDueDate###


Here is the code I was using and thought that it was working until I
did further testing and found that it didn’t. Any help would be
appreciated.


Private Sub CheckTextBoxNames()
On Error Resume Next

Dim f As Object
Dim tb As TextBox
Dim iRowCounter As Integer
Dim sFieldName As String

iRowCounter = 0
sFieldName = ""

‘ Loops through the all the textbox objects.
For Each f In ActiveDocument.Fields
If f.OLEFormat.ClassType = "Forms.TextBox.1" Then
Set tb = f.OLEFormat.Object

‘ Returns only the left part of the name.
sFieldName = Len(tb.Name) - 3)

' Checks the name to see if it is the first or
after that.
Select Case sFieldName
Case Is = "SqdMetCon"
tb.Name = RenameTextBoxNames
("SqdMetContNo", iRowCounter)
Case Is = "SqdLabEquipD"
tb.Name = RenameTextBoxNames
("SqdLabEquipDesc", iRowCounter)
Case Is = "SqdDatesU"
tb.Name = RenameTextBoxNames
("SqdDatesUsed", iRowCounter)
Case Is = "SqdLast"
tb.Name = RenameTextBoxNames("SqdLastCal",
iRowCounter)
Case Is = "SqdDueD"
tb.Name = RenameTextBoxNames("SqdDueDate",
iRowCounter)
iRowCounter = iRowCounter + 1
Case Is = "SqdMetContNo"
tb.Name = RenameTextBoxNames
("SqdMetContNo", iRowCounter)
Case Is = "SqdLabEquipDesc"
tb.Name = RenameTextBoxNames
("SqdLabEquipDesc", iRowCounter)
Case Is = "SqdDatesUsed"
tb.Name = RenameTextBoxNames
("SqdDatesUsed", iRowCounter)
Case Is = "SqdLastCal"
tb.Name = RenameTextBoxNames("SqdLastCal",
iRowCounter)
Case Is = "SqdDueDate"
tb.Name = RenameTextBoxNames("SqdDueDate",
iRowCounter)
iRowCounter = iRowCounter + 1
End Select
End If
Next f
End Sub


Private Function RenameTextBoxNames(sFieldName As String, iRowCounter
As Integer)
On Error Resume Next

Dim sNewName As String
sNewName = ""

If iRowCounter <= 9 Then
sNewName = sFieldName & "__" & iRowCounter
ElseIf (iRowCounter > 9) And (iRowCounter < 100) Then
sNewName = sFieldName & "_" & iRowCounter
ElseIf iRowCounter >= 100 Then
sNewName = sFieldName & iRowCounter
End If

RenameTextBoxNames = sNewName

End Function
 
S

salavine1

See the article "How to assign a Name to a FormField that doesn't already
have a Name, using VBA” at:

http://www.word.mvps.org/FAQs/MacrosVBA/AssignNameToFmFld.htm

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com


Hello,

I have a word document with at table that has 5 columns and anywhere
from 0 to 999 rows. Each table cell will have a textbox in it. I need
to change all the textbox names to a standard format as well as
incremental. If a row is deleted in the middle, I need to rename all
the textboxes after the deletion. The creating of this table and
textbox is automated and I don’t have the ability to change that. I
have to format after the fact.

The automated naming is as follows. Where # represents the number.
1st row: SqdMetContNo, SqdLabEquipDesc, SqdDatesUsed, SqdLastCal,
SqdDueDate

2nd – 9th rows: SqdMetContNo__#, SqdLabEquipDesc__#, SqdDatesUsed__#,
SqdLastCal__#, SqdDueDate__#

10th-99th rows: SqdMetContNo_##, SqdLabEquipDesc_##, SqdDatesUsed_##,
SqdLastCal_##, SqdDueDate_##

100 – 999 rows: SqdMetContNo###, SqdLabEquipDesc###, SqdDatesUsed###,
SqdLastCal###, SqdDueDate###

Here is the code I was using and thought that it was working until I
did further testing and found that it didn’t. Any help would be
appreciated.

Private Sub CheckTextBoxNames()
On Error Resume Next

    Dim f As Object
    Dim tb As TextBox
    Dim iRowCounter As Integer
    Dim sFieldName As String

    iRowCounter = 0
    sFieldName = ""

        ‘ Loops through the all the textbox objects.
        For Each f In ActiveDocument.Fields
                If f.OLEFormat.ClassType = "Forms.TextBox.1" Then
                    Set tb = f.OLEFormat.Object

                    ‘ Returns only the left part ofthe name.
                    sFieldName = Len(tb.Name) - 3)

                    ' Checks the name to see if it isthe first or
after that.
                    Select Case sFieldName
                        Case Is = "SqdMetCon"
                            tb.Name = RenameTextBoxNames
("SqdMetContNo", iRowCounter)
                        Case Is = "SqdLabEquipD"
                            tb.Name = RenameTextBoxNames
("SqdLabEquipDesc", iRowCounter)
                        Case Is = "SqdDatesU"
                            tb.Name = RenameTextBoxNames
("SqdDatesUsed", iRowCounter)
                        Case Is = "SqdLast"
                            tb.Name = RenameTextBoxNames("SqdLastCal",
iRowCounter)
                        Case Is = "SqdDueD"
                            tb.Name = RenameTextBoxNames("SqdDueDate",
iRowCounter)
                            iRowCounter = iRowCounter + 1
                        Case Is = "SqdMetContNo"
                            tb.Name = RenameTextBoxNames
("SqdMetContNo", iRowCounter)
                        Case Is = "SqdLabEquipDesc"
                            tb.Name = RenameTextBoxNames
("SqdLabEquipDesc", iRowCounter)
                        Case Is = "SqdDatesUsed"
                            tb.Name = RenameTextBoxNames
("SqdDatesUsed", iRowCounter)
                        Case Is = "SqdLastCal"
                            tb.Name = RenameTextBoxNames("SqdLastCal",
iRowCounter)
                        Case Is = "SqdDueDate"
                            tb.Name = RenameTextBoxNames("SqdDueDate",
iRowCounter)
                            iRowCounter = iRowCounter + 1
                    End Select
                End If
        Next f
End Sub

Private Function RenameTextBoxNames(sFieldName As String, iRowCounter
As Integer)
On Error Resume Next

    Dim sNewName As String
    sNewName = ""

If iRowCounter <= 9 Then
    sNewName = sFieldName & "__" & iRowCounter
ElseIf (iRowCounter > 9) And (iRowCounter < 100) Then
    sNewName = sFieldName & "_" & iRowCounter
ElseIf iRowCounter >= 100 Then
    sNewName = sFieldName & iRowCounter
End If

    RenameTextBoxNames = sNewName

End Function

Thank you Doug,

But, I using a Textbox from the Control Toolbar, not from the Forms
Toolbar. Any suggestions for this TextBox?
 
D

Doug Robbins - Word MVP on news.microsoft.com

If you put the cursor in the table and run the following macro, it should
rename that type of control as you want:

Dim i As Long
Dim fcode As Range
With Selection.Tables(1)
For i = 1 To .Rows.Count
Set fcode = .Cell(i, 1).Range.Fields(1).code
fcode.Text = Left(fcode.Text, 15) & "SqdMetContNo." & i & " \s"
Set fcode = .Cell(i, 2).Range.Fields(1).code
fcode.Text = Left(fcode.Text, 15) & "SqdLabEquipDesc." & i & " \s"
Set fcode = .Cell(i, 3).Range.Fields(1).code
fcode.Text = Left(fcode.Text, 15) & "SqdDatesUsed." & i & " \s"
Set fcode = .Cell(i, 4).Range.Fields(1).code
fcode.Text = Left(fcode.Text, 15) & "SqdMetContNo." & i & " \s"
Set fcode = .Cell(i, 5).Range.Fields(1).code
fcode.Text = Left(fcode.Text, 15) & "SqdDueDate." & i & " \s"
Next i
End With


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com

See the article "How to assign a Name to a FormField that doesn't already
have a Name, using VBA” at:

http://www.word.mvps.org/FAQs/MacrosVBA/AssignNameToFmFld.htm

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com


Hello,

I have a word document with at table that has 5 columns and anywhere
from 0 to 999 rows. Each table cell will have a textbox in it. I need
to change all the textbox names to a standard format as well as
incremental. If a row is deleted in the middle, I need to rename all
the textboxes after the deletion. The creating of this table and
textbox is automated and I don’t have the ability to change that. I
have to format after the fact.

The automated naming is as follows. Where # represents the number.
1st row: SqdMetContNo, SqdLabEquipDesc, SqdDatesUsed, SqdLastCal,
SqdDueDate

2nd – 9th rows: SqdMetContNo__#, SqdLabEquipDesc__#, SqdDatesUsed__#,
SqdLastCal__#, SqdDueDate__#

10th-99th rows: SqdMetContNo_##, SqdLabEquipDesc_##, SqdDatesUsed_##,
SqdLastCal_##, SqdDueDate_##

100 – 999 rows: SqdMetContNo###, SqdLabEquipDesc###, SqdDatesUsed###,
SqdLastCal###, SqdDueDate###

Here is the code I was using and thought that it was working until I
did further testing and found that it didn’t. Any help would be
appreciated.

Private Sub CheckTextBoxNames()
On Error Resume Next

Dim f As Object
Dim tb As TextBox
Dim iRowCounter As Integer
Dim sFieldName As String

iRowCounter = 0
sFieldName = ""

‘ Loops through the all the textbox objects.
For Each f In ActiveDocument.Fields
If f.OLEFormat.ClassType = "Forms.TextBox.1" Then
Set tb = f.OLEFormat.Object

‘ Returns only the left part of the name.
sFieldName = Len(tb.Name) - 3)

' Checks the name to see if it is the first or
after that.
Select Case sFieldName
Case Is = "SqdMetCon"
tb.Name = RenameTextBoxNames
("SqdMetContNo", iRowCounter)
Case Is = "SqdLabEquipD"
tb.Name = RenameTextBoxNames
("SqdLabEquipDesc", iRowCounter)
Case Is = "SqdDatesU"
tb.Name = RenameTextBoxNames
("SqdDatesUsed", iRowCounter)
Case Is = "SqdLast"
tb.Name = RenameTextBoxNames("SqdLastCal",
iRowCounter)
Case Is = "SqdDueD"
tb.Name = RenameTextBoxNames("SqdDueDate",
iRowCounter)
iRowCounter = iRowCounter + 1
Case Is = "SqdMetContNo"
tb.Name = RenameTextBoxNames
("SqdMetContNo", iRowCounter)
Case Is = "SqdLabEquipDesc"
tb.Name = RenameTextBoxNames
("SqdLabEquipDesc", iRowCounter)
Case Is = "SqdDatesUsed"
tb.Name = RenameTextBoxNames
("SqdDatesUsed", iRowCounter)
Case Is = "SqdLastCal"
tb.Name = RenameTextBoxNames("SqdLastCal",
iRowCounter)
Case Is = "SqdDueDate"
tb.Name = RenameTextBoxNames("SqdDueDate",
iRowCounter)
iRowCounter = iRowCounter + 1
End Select
End If
Next f
End Sub

Private Function RenameTextBoxNames(sFieldName As String, iRowCounter
As Integer)
On Error Resume Next

Dim sNewName As String
sNewName = ""

If iRowCounter <= 9 Then
sNewName = sFieldName & "__" & iRowCounter
ElseIf (iRowCounter > 9) And (iRowCounter < 100) Then
sNewName = sFieldName & "_" & iRowCounter
ElseIf iRowCounter >= 100 Then
sNewName = sFieldName & iRowCounter
End If

RenameTextBoxNames = sNewName

End Function

Thank you Doug,

But, I using a Textbox from the Control Toolbar, not from the Forms
Toolbar. Any suggestions for this TextBox?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top