Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 01-30-2014, 08:56 AM
Hdata Hdata is offline Find textbox name Windows 7 32bit Find textbox name Office 2010 32bit
Novice
Find textbox name
 
Join Date: Jan 2014
Posts: 26
Hdata is on a distinguished road
Thumbs up Find textbox name

I'm looking for a way to find the name of a textbox with VBA by placing the cursor in that textbox. Possible? Thanks in advance.
Reply With Quote
  #2  
Old 01-30-2014, 12:44 PM
gmaxey gmaxey is online now Find textbox name Windows 7 32bit Find textbox name Office 2010 (Version 14.0)
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,427
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

What kind of textbox?

Code:
Sub ScratchMacro()
'A basic Word macro coded by Greg Maxey
Dim oCC As ContentControl
Dim oILS As InlineShape
  'A formfield
  If Selection.FormFields.Count = 1 Then
    MsgBox Selection.FormFields(1).Name
  ElseIf Selection.FormFields.Count = 0 And Selection.Bookmarks.Count > 0 Then
    MsgBox Selection.Bookmarks(Selection.Bookmarks.Count).Name
  End If
  'A content control
  For Each oCC In ActiveDocument.Range.ContentControls
    If Selection.Range.InRange(oCC.Range) Then MsgBox oCC.Title
  Next oCC
  'A activeX control
  For Each oILS In ActiveDocument.InlineShapes
    If Selection.Range.InRange(oILS.Range) Then
      MsgBox oILS.OLEFormat.Object.Name
    End If
  Next
End Sub
See: http://gregmaxey.com/word_tip_pages/...ng_macros.html for instructions to employ the VBA code provided above.
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #3  
Old 01-30-2014, 01:14 PM
Hdata Hdata is offline Find textbox name Windows 7 32bit Find textbox name Office 2010 32bit
Novice
Find textbox name
 
Join Date: Jan 2014
Posts: 26
Hdata is on a distinguished road
Default

The textboxes I am working with are called VBA textboxes and not a Windows Form textbox. I hope that answers your question, or feel free and ask again. Thanks, this will be a big help.
Reply With Quote
  #4  
Old 01-30-2014, 01:23 PM
Hdata Hdata is offline Find textbox name Windows 7 32bit Find textbox name Office 2010 32bit
Novice
Find textbox name
 
Join Date: Jan 2014
Posts: 26
Hdata is on a distinguished road
Default

I just tried the above posted procedure and no result.
Reply With Quote
  #5  
Old 01-30-2014, 02:46 PM
gmaxey gmaxey is online now Find textbox name Windows 7 32bit Find textbox name Office 2010 (Version 14.0)
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,427
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

Well, if it is a textbox in a Userform, you pretty much have to know the name before you can do anything with it. For example, if you have a textbox named TextBox1, then you could use the Enter event for the named control:

Private Sub TextBox1_Enter()
MsgBox Me.TextBox1.Name
End Sub
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #6  
Old 01-30-2014, 07:31 PM
fumei fumei is offline Find textbox name Windows 7 64bit Find textbox name Office XP
Expert
 
Join Date: Jan 2013
Posts: 440
fumei is on a distinguished road
Default

I think you had better tell us where these textboxes are, in the document or on a userform. Greg's original question has not been answered. Are they fomfields - these ARE called textboxes you know. ActiveX textboxes...also called textboxes. Or maybe even content controls which sometimes are called textboxes as well.

Oh and they can all be thought of as "VBA textboxes".
Reply With Quote
  #7  
Old 01-30-2014, 07:39 PM
gmaxey gmaxey is online now Find textbox name Windows 7 32bit Find textbox name Office 2010 (Version 14.0)
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,427
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

I can say with a certain large degree of certainty that they are probably not fomfields ;-)

With my legendary carelessness with typos and spelling, it is refreshing to know that the realm of imperfection is not a population of 1.
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #8  
Old 01-30-2014, 07:41 PM
fumei fumei is offline Find textbox name Windows 7 64bit Find textbox name Office XP
Expert
 
Join Date: Jan 2013
Posts: 440
fumei is on a distinguished road
Default

Oh I don't know. Things seem rather mushy, so they are all FOAMfields.
Reply With Quote
  #9  
Old 01-30-2014, 11:13 PM
macropod's Avatar
macropod macropod is offline Find textbox name Windows 7 32bit Find textbox name Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,962
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Quote:
Originally Posted by Hdata View Post
The textboxes I am working with are called VBA textboxes and not a Windows Form textbox.
There is no such thing as 'VBA textboxes'. To VBA, userforms and documents can both contain textboxes, so maybe they're document textboxes (i.e. the kind that often exist in the body of a document)? For a macro to return the name of such a textbox when you click in it, you'd have to created a Selection_Change event macro. For the principles, see:
http://word.mvps.org/FAQs/MacrosVBA/AppClassEvents.htm
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #10  
Old 01-31-2014, 05:25 AM
Hdata Hdata is offline Find textbox name Windows 7 32bit Find textbox name Office 2010 32bit
Novice
Find textbox name
 
Join Date: Jan 2014
Posts: 26
Hdata is on a distinguished road
Default

I will try to clarify the textboxes I am using. They are created in Office 2010 vba from the userform where I click the textbox control from the toolbox. Thank you for your input. I want to check into your last posting identified as Selection_Change event macro. Best regards, Hdata
Reply With Quote
  #11  
Old 01-31-2014, 05:38 AM
macropod's Avatar
macropod macropod is offline Find textbox name Windows 7 32bit Find textbox name Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,962
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

In your previous reply, you said:
Quote:
Originally Posted by Hdata View Post
The textboxes I am working with are called VBA textboxes and not a Windows Form textbox.
From that, I would have thought they were not userform textboxes. But this:
Quote:
Originally Posted by Hdata View Post
They are created in Office 2010 vba from the userform where I click the textbox control from the toolbox.
indicates otherwise. For a userform control, all you need is an 'Enter' event, coded like:
Code:
Private Sub TextBox1_Enter()
MsgBox Me.ActiveControl.Name
End Sub
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #12  
Old 01-31-2014, 07:15 AM
Hdata Hdata is offline Find textbox name Windows 7 32bit Find textbox name Office 2010 32bit
Novice
Find textbox name
 
Join Date: Jan 2014
Posts: 26
Hdata is on a distinguished road
Default

I created a command button on the userform and attached the code below:

Code:
 
Private Sub cmdTextBoxName_Enter()
MsgBox Me.ActiveControl.Name
End Sub
What I received back was
"cmdTextBoxName"

The name of the textbox is actually "Document_City"
What am I missing?
Thank you for your response

Hdata
Reply With Quote
  #13  
Old 01-31-2014, 07:22 AM
gmaxey gmaxey is online now Find textbox name Windows 7 32bit Find textbox name Office 2010 (Version 14.0)
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,427
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

IF you want the textbox to return its name when you click in it use:

Private Sub Document_City_Enter()
MsgBox Me.Document_City.Name
End Sub

as previously provided, or

Private Sub Document_City_Enter()
MsgBox Me.ActiveConrol.Name
End Sub

as Paul provides, or

If you click a command button Document_City obviously no longer has the focus so:

Private Sub cmdTextBoxName_Enter()
MsgBox Me.ActiveControl.Name
End Sub

Naturally returns cmdTextBoxName, because cmdTextBoxName is now the active control.
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #14  
Old 01-31-2014, 07:32 AM
Hdata Hdata is offline Find textbox name Windows 7 32bit Find textbox name Office 2010 32bit
Novice
Find textbox name
 
Join Date: Jan 2014
Posts: 26
Hdata is on a distinguished road
Default

OK, as previously indicated, I have to know the name before I can do anything with it, which defeats what I was trying to do. So thanks again, I will move on to other issues.
Reply With Quote
  #15  
Old 01-31-2014, 03:11 PM
macropod's Avatar
macropod macropod is offline Find textbox name Windows 7 32bit Find textbox name Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,962
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Quote:
Originally Posted by Hdata View Post
I created a command button on the userform and attached the code below:

Code:
Private Sub cmdTextBoxName_Enter()
MsgBox Me.ActiveControl.Name
End Sub
What I received back was
"cmdTextBoxName"

The name of the textbox is actually "Document_City"
What am I missing?
Well, your sub's name (cmdTextBoxName_Enter) shows that the control's name can only be 'cmdTextBoxName'. Evidently, you want something else; perhaps the name of a visually-related label. If so, unless the label and control are grouped (and nothing else is in the same group), there is no 100% reliable way for VBA to identify which one you want.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
clip art with embeded textbox Mel Spence Misc 0 05-19-2013 09:38 AM
Find textbox name Display result in textbox based on the input of another textbox scarymovie Word VBA 5 05-16-2012 07:05 PM
textbox color issue Dave Duncan Word 0 08-07-2011 10:38 AM
Textbox Template t0m46 Word 0 09-07-2010 03:38 AM
Creating Chart using VB textbox value shyler82 Word VBA 0 03-12-2010 06:56 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 12:33 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft