Inserting Text into a text box using VBA...

  • Thread starter David Dubroff of Mr. PC
  • Start date
D

David Dubroff of Mr. PC

Microsoft Word 2000 for Windows Question:


I have a document to print envelopes that consists of 2,000+ envelopes to be
printed.

The address for each recipient resides in a text box within each page (one
page equals one envelope).

I would like to use VBA to add some text between the first and second line
within the recipient address text box on each page.

I want to add "or Current Resident" as a second line of text within each
existing text box.

Is there VBA code that would accomplish this?

Please advise.....

Dave
 
J

Jean-Guy Marcil

David Dubroff of Mr. PC was telling us:
David Dubroff of Mr. PC nous racontait que :
Microsoft Word 2000 for Windows Question:


I have a document to print envelopes that consists of 2,000+
envelopes to be printed.

The address for each recipient resides in a text box within each page
(one page equals one envelope).

I would like to use VBA to add some text between the first and second
line within the recipient address text box on each page.

I want to add "or Current Resident" as a second line of text within
each existing text box.

Is there VBA code that would accomplish this?

How many textboxes on each page? If more than one, the address in which
"nth" textbox?
Are each line in the textbox terminated by a ¶ (ENTER) or a manual line
break (SHIFT-ENTER)?
How are the page separated by each other (section break, manual page break,
paragraph page break... )?
What Word version?

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

David Dubroff of Mr. PC

Actually... I think the Word version is from Office XP Small Business
Edition.

There is only one text box on each page, and each line within the text box
is separated by a ¶ (ENTER).

I don't remember how each page is separated and I am not at the location
with the file at the moment.

Each page begins with some text and a section break. The text above each
section break contains the return address information on the envelope.
Below each section break is a text box with the recipient address
information.

I hope this information helps you to help me..........

Dave
 
J

Jean-Guy Marcil

David Dubroff of Mr. PC was telling us:
David Dubroff of Mr. PC nous racontait que :
Actually... I think the Word version is from Office XP Small Business
Edition.

There is only one text box on each page, and each line within the
text box is separated by a ¶ (ENTER).

I don't remember how each page is separated and I am not at the
location with the file at the moment.

Each page begins with some text and a section break. The text above
each section break contains the return address information on the
envelope. Below each section break is a text box with the recipient
address information.

I hope this information helps you to help me..........
Try this:

'_______________________________________
Sub InsertBefore2ndLineInTextBox()

Dim SecRange As Range
Dim TextBoxRge As Range
Dim ParaRge As Range
Dim i As Long

With ActiveDocument
For i = 1 To .Sections.Count
Set SecRange = .Sections(i).Range
With SecRange
If .ShapeRange.Count > 0 Then
Set TextBoxRge = .ShapeRange(1).TextFrame.TextRange
Set ParaRge = TextBoxRge.Paragraphs(2).Range
With ParaRge
.InsertBefore "or Current Resident" & Chr(13)
End With
End If
End With
Next
End With

End Sub
'_______________________________________

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

David Dubroff of Mr. PC

When I run the macro, I receive the following error:

Run-time error 5852: The requested object is not available.

When I click the "debug" button, the following line of code is highlighted:

If .ShapeRange.Count > 0 Then

Any suggestions.....

Thanks.
 
J

Jean-Guy Marcil

David Dubroff of Mr. PC was telling us:
David Dubroff of Mr. PC nous racontait que :
When I run the macro, I receive the following error:

Run-time error 5852: The requested object is not available.

When I click the "debug" button, the following line of code is
highlighted:
If .ShapeRange.Count > 0 Then
Have you modified the code?
Can you post the actual code you are using?

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

David Dubroff of Mr. PC

This is the code that is not working:

Sub Insert_Resident()
'
' Insert_Resident Macro
' Macro recorded 11/16/2004 by Joseph Tornetta
'

Dim SecRange As Range
Dim TextBoxRge As Range
Dim ParaRge As Range
Dim i As Long

With ActiveDocument
For i = 1 To .Sections.Count
Set SecRange = .Sections(i).Range
With SecRange
If .ShapeRange.Count > 0 Then
Set TextBoxRge = .ShapeRange(1).TextFrame.TextRange
Set ParaRge = TextBoxRge.Paragraphs(2).Range
With ParaRge
.InsertBefore "or Current Resident" & Chr(13)
End With
End If
End With
Next
End With

End Sub


Please advise...

Dave
 
J

Jean-Guy Marcil

David Dubroff of Mr. PC was telling us:
David Dubroff of Mr. PC nous racontait que :
This is the code that is not working:

If your document is as you described it to me in your preceding post, I do
not understand why it would not work.
You can send me your document. Just unscramble my email address from my
signature.

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

Jean-Guy Marcil

Jean-Guy Marcil was telling us:
Jean-Guy Marcil nous racontait que :
David Dubroff of Mr. PC was telling us:
David Dubroff of Mr. PC nous racontait que :


If your document is as you described it to me in your preceding post,
I do not understand why it would not work.
You can send me your document. Just unscramble my email address from
my signature.

OK, got it!

What you called textboxes were in fact Frames... a very different critter
altogether!

Try this instead:

'_______________________________________
Dim SecRange As Range
Dim FrameRge As Range
Dim ParaRge As Range
Dim i As Long

With ActiveDocument
For i = 1 To .Sections.Count
Set SecRange = .Sections(i).Range
With SecRange
If .Frames.Count > 0 Then
Set FrameRge = .Frames(1).Range
Set ParaRge = FrameRge.Paragraphs(2).Range
With ParaRge
.InsertBefore "or Current Resident" & Chr(13)
End With
End If
End With
Next
End With
'_______________________________________

By the way, what are you doing with a 2,651-page document containing nothing
but a bunch of envelopes?
Must be a pain to maintain (and the rain falls mainly on the plain in
Spain!)?

I think it would be easier to maintain a simple database (A Word document
with a table would do) and to use mail merge template? No?
In case you want to look it up:
http://word.mvps.org/faqs/MailMerge/index.htm

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

David Dubroff of Mr. PC

Merci pour le renseignement.

Dave


Jean-Guy Marcil said:
Jean-Guy Marcil was telling us:
Jean-Guy Marcil nous racontait que :


OK, got it!

What you called textboxes were in fact Frames... a very different critter
altogether!

Try this instead:

'_______________________________________
Dim SecRange As Range
Dim FrameRge As Range
Dim ParaRge As Range
Dim i As Long

With ActiveDocument
For i = 1 To .Sections.Count
Set SecRange = .Sections(i).Range
With SecRange
If .Frames.Count > 0 Then
Set FrameRge = .Frames(1).Range
Set ParaRge = FrameRge.Paragraphs(2).Range
With ParaRge
.InsertBefore "or Current Resident" & Chr(13)
End With
End If
End With
Next
End With
'_______________________________________

By the way, what are you doing with a 2,651-page document containing
nothing but a bunch of envelopes?
Must be a pain to maintain (and the rain falls mainly on the plain in
Spain!)?

I think it would be easier to maintain a simple database (A Word document
with a table would do) and to use mail merge template? No?
In case you want to look it up:
http://word.mvps.org/faqs/MailMerge/index.htm

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