Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to write, paste or append text on a word document?

Status
Not open for further replies.

CTOROCK

Programmer
May 14, 2002
289
US

I wanted to create a combo box with numbers. Depending on the number chosen, it would paste or write a paragraph where the cursur is on the document. Does anyone have an idea how to paste or write or append text to an active document?
Thanks is advance
Eric
"The greatest risk, is not taking one."
 
Hi, CTOROCK ,

I am not a Word VBA guru, but here's a way to do it, where Para(4) is declared Public in a module...
Code:
Private Sub ComboBox1_Change()
    With Selection
        .InsertAfter Para(ComboBox1.Text)
    End With
End Sub

Private Sub UserForm_Activate()
    With ComboBox1
        .AddItem 1
        .AddItem 2
        .AddItem 3
        .AddItem 4
    End With
    Para(1) = "fred"
    Para(2) = "jack"
    Para(3) = "skip"
    Para(4) = "tom"
End Sub
This inserts at the CURRENT cursor location, the array element corresponding to the selection made.

Hope this helps :) Skip,
SkipAndMary1017@mindspring.com
 
Hey Thanks Skip. But when I do that It adds the text at the beginning of the document. Do you know how to make it add to the document? Also, for some reason I can not add the items to the combo box. I get an invalid argument. Thanks for you time!

Eric "The greatest risk, is not taking one."
 
CTOROCK.

I had neither of those problems.

The Selection.InsertAfter method does so after the cursor selection point NOT at the beginning of the document.

Are you certain that you have NOT used the EQUAL SIGN in your code?

Let me know :) Skip,
SkipAndMary1017@mindspring.com
 
Skip, for some reason It enters it at the very beginning of the document. I'm not sure why, but I did this:

Selection.EndKey Unit:=wdStory
Selection.InsertAfter Para(ComboBox1.Text)
to take me to the end. There may better way. But I am having a hard time adding items to the list. I try to populate the combobox during Ducument_Open and it doesn't work.

With ComboBox1
.AddItem "Hi", 1
or
.AddItem 2
.AddItem 3
.AddItem 4

End With
or
ComboBox1.AddItem "Hey", 1


Thanks Skip "The greatest risk, is not taking one."
 
1. I don't know what...
Code:
Selection.EndKey Unit:=wdStory
does, BUT it does NOT work as you are requesting. Why did you insert it?

I DO know that WITHOUT that statement, IT DOES WORK at the cusror insertion point!

2. So what do you want to do, select a number or select some text?

Text...
Code:
Private Sub ComboBox1_Change()
    With Selection
        .InsertAfter ComboBox1.Text
    End With
End Sub

Private Sub UserForm_Activate()
    With ComboBox1
        .AddItem "Now is the time"
        .AddItem " for all good men"
        .AddItem " to come to the aid"
        .AddItem " of their country."
    End With
End Sub
:)

Skip,
SkipAndMary1017@mindspring.com
 
I got it Skip, your original way was correct. I started to fool around with text and it didn't work, but i need numbers anyway. I really apreciate your time, your're a real life saver.
Thanks again!

Eric "The greatest risk, is not taking one."
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top