I hope someone can help.
I have a word document with several textbox fields. How can I loop through
them getting their names? This is not on a form, just on the document itself.
I can not find a Controls collection there to use.
David
Bonjour,
Dans son message, < David Hodgkins > ?crivait :
In this message, < David Hodgkins > wrote:
|| I hope someone can help.
||
|| I have a word document with several textbox fields. How can I loop
through
|| them getting their names? This is not on a form, just on the document
itself.
|| I can not find a Controls collection there to use.
This code should get you going:
'_______________________________________
Sub GetTextBoxName()
Dim myFormField As FormField
Dim FFNames As String
Dim i As Long
Dim ResultDoc As Document
Dim SourceDoc As Document
Set SourceDoc = ActiveDocument
i = 0
For Each myFormField In SourceDoc.FormFields
With myFormField
If .Type = wdFieldFormTextInput Then
i = i + 1
FFNames = FFNames & .Name & Chr(13)
End If
End With
Next myFormField
Set ResultDoc = Documents.Add
With ResultDoc
.Range.Text = "There are " & i & " textbox fields " _
& " in " & SourceDoc.Name & "." & Chr(13) & Chr(13) _
& "Here are their names:" & Chr(13) & Chr(13) _
& FFNames
End With
End Sub
'_______________________________________

Signature
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
jmarcilREMOVE@CAPSsympatico.caTHISTOO
Word MVP site: http://www.word.mvps.org
David Hodgkins - 30 Sep 2004 20:51 GMT
Thanks you, but alas, no. When I run it nothing is picked up. There is no
form on the document. The textbox is pulled directly off the toolbox and
placed on the document.
David
> Bonjour,
>
[quoted text clipped - 42 lines]
> End Sub
> '_______________________________________
Jean-Guy Marcil - 30 Sep 2004 22:11 GMT
Bonjour,
Dans son message, < David Hodgkins > ?crivait :
In this message, < David Hodgkins > wrote:
|| Thanks you, but alas, no. When I run it nothing is picked up. There is no
|| form on the document. The textbox is pulled directly off the toolbox and
|| placed on the document.
In your first post you wrote:
<quote>
I have a word document with several textbox fields
<end quote>
So I assumed they were form fields...
Now you are writing:
<quote>
The textbox is pulled directly off the toolbox
<end quote>
You mean the ActiveX Control toolbar?
If they were from the Control Toolbar, try this:
'_______________________________________
Sub GetTextBoxName()
Dim myObj As Field
Dim ObjNames As String
Dim i As Long
Dim ResultDoc As Document
Dim SourceDoc As Document
Set SourceDoc = ActiveDocument
i = 0
For Each myObj In SourceDoc.Fields
With myObj
If Not InStr(1, .Code, "Forms.TextBox.1") = 0 Then
i = i + 1
ObjNames = ObjNames & .OLEFormat.Object.Name & Chr(13)
End If
End With
Next myObj
Set ResultDoc = Documents.Add
With ResultDoc
.Range.Text = "There are " & i & " textbox controls " _
& " in """ & SourceDoc.Name & ".""" & Chr(13) & Chr(13) _
& "Here are their names:" & Chr(13) & Chr(13) _
& ObjNames
End With
End Sub
'_______________________________________

Signature
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
jmarcilREMOVE@CAPSsympatico.caTHISTOO
Word MVP site: http://www.word.mvps.org