PDA

View Full Version : Word VBA Textboxes



tejwani
06-28-2006, 10:25 AM
Hello,


I have inserted textboxes using the "Insert" --> textboxes function in the Ms Word Tool Strip. This textbox is not inserted through VB Toolbox. However, I want to define the textboxes that I have inserted using the MS Word Toolbaf as objects, so I can code for it.

Ultimately, I want to link those textboxes to fields wtih an excel spreasheet. I can do this if I inserted textboxes throught VB Toolbox, however, I am contrained to only use textboxes, provided my MS WORD tool bar (Go to "Insert" ---> textbox) How do I define this textbox as an object/macro so I can code for it?

Thanks,

Tejwani

mdmackillop
06-28-2006, 10:57 AM
Have a look at the following code

Option Explicit
Sub NameBoxes()
Dim sh As Shape, i As Long
For Each sh In ActiveDocument.Shapes
i = i + 1
sh.Select
Selection.TypeText sh.Name
Next
End Sub
Sub ExcelValues()
Dim ws As Object
Set ws = GetObject("C:\AAA\Book1.XLS")
ActiveDocument.Shapes("Text Box 2").Select
Selection.TypeText ws.Sheets("Sheet1").Cells(1, 1).Text
End Sub

fumei
06-28-2006, 10:57 AM
I have inserted textboxes using the "Insert" --> textboxes function in the Ms Word Tool Strip. This textbox is not inserted through VB Toolbox. You can not insert a textbox into a document with the VBA Toolbox. The VBA Toolbox only puts textboxes onto Userforms.

By "VB Toolbox" do you mean the Controls toolbar? View > Toolbars > Controls? These are ActiveX textboxes, and are objects that can be fairly easily accessed through code.

The textboxes inserted by Insert > Textbox are a real pain in the ass. I avoid them as much as possible. Especially if they need to be accessed by code. Word does very very weird things with them. Here is a demo of that.

Have Word completely closed.
Start up Word.
In the blank document, go Insert > Textbox
Do not put anything it, just close the drawing toolbar. Do NOTHING except close the drawing toolbar.

You now have ONE textbox in a blank document.

Put the following into the ThisDocument module, and run it.Sub TextBoxName()
Dim oShape As Word.Shape
For Each oShape In ActiveDocument.Shapes()
MsgBox oShape.Name
Next
End SubNow, on my computer, the message box will display "Text Box 4". Why...I have no idea. But it sure ain't "1" - even though there is only one thing in the document...ONE textbox.

OK......

Put in another textbox. Again, do not put anything in it. Just make another textbox. Run the code again.

On my computer, the message box displays twice - makes sense, there are two Shapes. The messages are:

"Text Box 4"....and

"Text Box 7"

There are weird reasons for this. My point is that while - yes - you can use Insert > Textbox for taking your data from Excel, it may be easier to use something else.

A formfield perhaps?
A bookmark, perhaps?

1. why are you "constrained" to only using textboxes?
2. what, precisely, are you doing? Formfields / bookmarks can very capable of taking information. Why textboxes????