Get textbox names

D

David Hodgkins

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
 
J

Jean-Guy Marcil

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
'_______________________________________

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
D

David Hodgkins

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
 
J

Jean-Guy Marcil

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
'_______________________________________

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 

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