Inserting text into textbox

L

Lasse

Hi

I am creating a Word template with a AutoNew module that inserts city name
and date into the document.
I want to insert the information into a specific textbox but I don't know
how to select the textbox.
I can easily insert the information into the document but not in the
textbox. I have tried to create a bookmark in the textbox and it works fine
when I run it from Visual Basic but not if I open up the template normally.
 
J

Jonathan West

Lasse said:
Hi

I am creating a Word template with a AutoNew module that inserts city name
and date into the document.
I want to insert the information into a specific textbox but I don't know
how to select the textbox.
I can easily insert the information into the document but not in the
textbox. I have tried to create a bookmark in the textbox and it works
fine
when I run it from Visual Basic but not if I open up the template
normally.

The simplest approach is to put a bookmark into the textbox. Then you
include this in your macro

ActiveDocument.Bookmarks("MyBookmark").Range.InsertAfter "My Text"

changing the bookmark name and inserted text as appropriate.


--
Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup
Keep your VBA code safe, sign the ClassicVB petition www.classicvb.org
 
L

Lene Fredborg

You said:

<…but not if I open up the template normally>

Do you mean that you open the template itself, i.e. the .dot file? In that
case, AutoNew will not run. It will only run when you create a new document
based on the template. You need an AutoOpen macro in order to make it run
when the template itself is opened.

See the VBA help on "Auto Macros" for details.

--
Regards
Lene Fredborg
DocTools - Denmark
www.thedoctools.com
Document automation - add-ins, macros and templates for Microsoft Word
 
H

Helmut Weber

Hi Lasse,
I am creating a Word template with a AutoNew module that inserts city name
and date into the document.
I want to insert the information into a specific textbox but I don't know
how to select the textbox.
I can easily insert the information into the document but not in the
textbox. I have tried to create a bookmark in the textbox and it works fine
when I run it from Visual Basic but not if I open up the template normally.

Normally, you don't open a template at all,
unless you want to change the template.
Normally, you create a new document based on a template.

But if the template is very complex,
and therefore the newly created document,
then you might need an ontime macro to wait
until your document is fully loaded,
before some command can successfully be executed.

Public Sub AutoNew()
' might work for a simple doc
ActiveDocument.Shapes(1).TextFrame.TextRange = "XXXXXXXXXXXX"
End Sub

For complex docs:

Public Sub AutoNew()
' do this and that if it happens to work
Application.OnTime When:=Now + TimeValue("00:00:03"), _
Name:="Mycontinue"
End Sub

Sub MyContinue()
ActiveDocument.Shapes(1).TextFrame.TextRange = "xxxx"
End Sub


Instead of waiting exactly 3 seconds,
you may check, if the document is fully loaded.

http://tinyurl.com/35b7t5
http://tinyurl.com/3asjt3

HTH

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 
L

Lasse

Thanks for the replies.

I have tried to insert your suggestion into my macro but it doesn't help.

We have four departments in four different cities. We share the Word
templates and I want the templates to fill out the city name when we create a
new document based on the template.

I use the following code:
If ActiveDocument.Bookmarks.Exists("Text") = True Then
Selection.Font.Size = 8
Selection.InsertAfter (Department)
End If

It actually inserts the text but not in the textbox with the bookmark. If I
create a textbox with a bookmark I can't jump to the bookmark if the textbox
isn't highlighted.
 
J

Jonathan West

Lasse said:
Thanks for the replies.

I have tried to insert your suggestion into my macro but it doesn't help.

We have four departments in four different cities. We share the Word
templates and I want the templates to fill out the city name when we
create a
new document based on the template.

I use the following code:
If ActiveDocument.Bookmarks.Exists("Text") = True Then
Selection.Font.Size = 8
Selection.InsertAfter (Department)
End If

It actually inserts the text but not in the textbox with the bookmark.

Of course it doesn't, "Selection.InsertAfter" means you are inserting at the
Selection. Yake a look at the line of code I provided.

ActiveDocument.Bookmarks("MyBookmark").Range.InsertAfter "My Text"

That means

- You are inserting "My Text"
- after
- The range of
- the Bookmark "MyBookmark"
- in the active document

If I
create a textbox with a bookmark I can't jump to the bookmark if the
textbox
isn't highlighted.

That doesn't matter, if your macro inserts the text directly.


--
Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup
Keep your VBA code safe, sign the ClassicVB petition www.classicvb.org
 
D

Doug Robbins - Word MVP

You are inserting into the selection which is not necessarily in the
bookmark

With ActiveDocument
If .Bookmarks.Exists("Text") = True Then
.Bookmarks("Text").Range.InsertBefore (Department)
.Bookmarks("Text").Range.Font.Size = 8
End If
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
 
L

Lasse

Great, I got it to work :)

I used:
ActiveDocument.Bookmarks("Text").Range.InsertAfter (Department)

I actually got it to work before as well with the following code:
If ActiveDocument.Bookmarks.Exists("Text") = True Then
ActiveDocument.Bookmarks("Text").Select
Selection.Font.Size = 8
Selection.InsertAfter (Department)
End If
The only problem with my own code was that it inserted a "|" before the city
name.

Thanks for your help.
 

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