PDA

View Full Version : put text in Word via VB



apangratiu
10-20-2008, 06:36 AM
Good afternoon

I just wanna know if I can do with VB an small application taht can do the following
make an new document in word and put inside some text in the correct position. The text has to be controlled by check box. If one buttone it is check the assign text will be putted inside. and one box can be with variable text and to insert inside what ever name I want? If this is Yes ..can You give me an start for the code or something like an START?
I ask abou this because I have a lot of combination af some standard text ...and I think this kind of small aplication will help me and gain some time like I am doing now with an templet and delet what I don't need.

all answer will be more then wellcom
Antoniu

fumei
10-20-2008, 07:10 AM
Yes, I am sure this can be done. What have you tried? You need to give some more details.

For example, what does "in the correct position." mean. What defines "correct"?

Where is the text coming from? Assigned by what?

You do not state anything about where this checkbox is. On a userform? If so, then, it would be something like:

If Checkbox1.Value = True Then
' put the text in the "correct" position
End if

Other than than, can't really give you much suggestions, as there is not enough information.

However, again....what have you tried?

lucas
10-20-2008, 11:04 AM
Another alternative would be to have all of the text in the document and give the user a choice of what to keep, deleting the rest. Gerry gives a good example in this thread.......starts at the bottom of post#11

http://vbaexpress.com/forum/showthread.php?t=13543

fumei
10-20-2008, 11:26 AM
That is certainly one way to do a multiple choices structure for a document, but we do not really know what they requirements are. The text may be coming fro9m another source...who knows.

Antoniu, can you give us more information on what you are trying to do? What does: "one box can be with variable text and to insert inside what ever name I want?" actually mean?

Insert inside....what? Insert inside what ever name I want? Name? A bookmark name? I do not understand.

apangratiu
10-21-2008, 01:05 AM
look at that, somebody answer .. thank you so much.
correct position will be with paragraph of certain Tab or with bullets or the distance from the left edge to be put it on exact measurement.
and the text it is some condition that some time I have to put in the doc file or I don't have to put it ...depends on the person I have to writh condition. The condition it is the security ..were helmet or not ..so if I check the check button to put inside an text

the visitor need to have helmet? if the button it is check ..insert an text with You have to put the Helmet..if not don't put anything.. I hope I was clear ..

anyway thanks in advance
Antoniu

fumei
10-21-2008, 02:01 PM
No, I am sorry, but I do not understand.

Helmet?

apangratiu
10-30-2008, 04:36 AM
I am really sorry ..I have to start slow because in mine mind it is clear but ...mine mind my be too confuse for somebody else

so

it is possible to make like this (see the picture)
then after the button are check an push the generate button to make an new word document with the text inside (put it with tab or colors etc ...)

this is clear or I am confusing more?

regards
Antoniu

fumei
10-30-2008, 10:11 AM
"I am confusing more?"

You are confusing more.

It would help if you did as it has been asked. Give us some actual code you have tried. In the meantime, here is a file that does:

"after the button are check an push the generate button to make an new word document with the text inside (put it with tab or colors etc ...)"

I am going to ignore the "with tab or colors", as you give no indication of what that actually means. The attached file will open with a userform like the one in your image. Selecting a checkbox and clicking the "Generate" button will:

1. make a new document
2. "with the text inside"

That is, it will make a new document (or documents) with "Hello World", "Have a nice Day", or "Red text".

Until you actually say EXACTLY what you want to do, I do not think I can help more that that.

Note that the demo allows ALL three checkboxes to be checked, and if that is done, THREE separate documents will be generated.

This is because you did NOT state that you wanted only one choice to be made of the three. If you had stated you only wanted only one choice to be made, then I would have done it that way. It is easily done. But...you did not state that. This is why I ask you to state EXACTLY what you want to happen.

Note: the userform displays on document open, but you can also get it by clicking "Show My Userform" on the top toolbar.

Good luck.

apangratiu
10-31-2008, 01:23 AM
cool ..like that but all the text inside to ONE word document for all "words" this is possible? also the RED text can be made red?

many many thx
Antoniu

fumei
10-31-2008, 10:23 AM
Sorry, I can not see how I can help you any further.

"cool ..like that but all the text inside to ONE word document for all "words" this is possible? also the RED text can be made red?"

I have repeated asked that you state exactly what you want to happen. You have not really responded at all.

"is this possible"?

Yes, it is possible. You can make Word almost anything you want it to do. BUT...you have to tell it EXACTLY what to do. And you are not doing that. Hopefully you can take what I have put in the demo and figure out what you are doing, and do it.

This is a simple matter of logic. Figure out what your logic is, and make the code DO that. You want text associated with each checkbox going into ONE document...then...do that.

apangratiu
11-01-2008, 01:53 PM
ok ...for me, mine first explanation was the way you figured out .. mine skills in VB for Word are almost zero ..and I just assume that I was clear.. but seems I was not ..mea culpa.

ok the text what I want to insert in the new document are just some security text that I have to put't all the time from an temple ..Like wen you are going to vist an site you need to have protection equipment. So I was thinking that I can made an tiny mini 'program' can do this it just some clicks without opening all the time the temple and delete what I don't need... But I was stuck in the first line ...like, made an new document and insert inside 'some text' that are premade and to be put I just need to check what I need or what I don't need (exactly like you made 't). for me It is not that easy ..

I will try to figured myself to do it. because for the moment I can not tell exactly an code of the VB

Anyway many thanks for the help ..and if I don't ask too much ..with what I shod begin ?

apangratiu
06-18-2009, 02:48 AM
hello lovely people
it is crisis here in Belgium so like that I have a bit of time. I try to figure out my old problem an I do this VBA code (ok ..beginner stile) so:

this will start word aplication and will write an text in word:

Private Sub CommandButton1_Click()
Dim wdAppl As Word.Application
Set wdAppl = CreateObject("Word.Application")
aDoc = wdAppl.Documents.Add()
wdAppl.Visible = True
With wdAppl.Selection
.Font.Name = "Arial"
.TypeText "Formules dans la feuille: "
.TypeParagraph
.TypeText "Cells: " + Selection.Cells(1, 1)
.TypeParagraph
.TypeParagraph
End With
End Sub

then to the other side and I have the check box that I need to be check and like that to insert an text inside that word file that can be created via check box:

Private Sub CheckBox1_Click()
If CheckBox1.Value = True Then
put the following text - here I do not know what instruction I can put
End If
End Sub
than I have to put together the first code with the second one (this one it is a mister for me)

To create create an file in word with the text from check box (if this one is checked)..can you VBA fans help me, to find my way?

thank you :)

fumei
06-18-2009, 01:56 PM
"can you VBA fans help me, to find my way?"

Yes we could help if you did what we have asked a number of times. Tell us EXACTLY what it is you want to do.

Exactly means just that...exactly. Precisely.

You do not tell us, so while I will repeat that Word can be made to do just about anything, to do so requires knowing exactly what it is you want it to do.

Word does not read minds, and neither do we.

Even as a start, your code above indicates that you are creating an instance of Word.

Dim wdAppl As Word.Application
Set wdAppl = CreateObject("Word.Application")
THAT implies you are executing the VBA from some other application. Do you not think it is worth mentioning that? WHAT application?

Inserting text is easy. You can either type it in using TypeText Text:=text; or you could use an AutoText. AutoText is text that stored by Word. You can insert it by name.

For example, you could have the text:

"This is some text that I want to insert somewhere. I am not sure where because i am not paying attention."

You can make that chunk of text an AutoText, and name it, say, SomeText. Now you can insert that text - as an AutoText entry - anywhere you want.
NormalTemplate.AutoTextEntries("SomeText") _
.Insert Where:= Selection.Range , RichText:=True
which would insert the text chunk at the Selection.

Note that the location can be ANY location. The default is at the Selection.
.Insert Where:= Selection.Range
but it could be ANY location. Say you want to put it in the header.
Dim r As Range
Set r = ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Range
NormalTemplate.AutoTextEntries("SomeText") _
.Insert Where:= r , RichText:=True
Voila. The text (as an Autotext) is inserted into the primary header of Section 1.

What I am saying, and have said repeatedly, is that you must figure out EXACTLY what it is you want to do.

"create an file in word with the text from check box "

There is NO such thing as "text from a checkbox". A checkbox is a checkbox. It has a Caption, but it does NOT have text. You can make it so the Value of a checkbox (True = checked, False = unchecked) results in text being inserted some place, but that is logic. YOU, and only you, must determine the logic.

IF checkbox is checked THEN do X

It will not do anything unless YOU tell it to do something, and the first step is to figure out EXACTLY what that is.

Which you are not doing.

apangratiu
06-19-2009, 12:52 AM
good morning :) thx for replay ..I am sorry but my VBA language is almost null .. I want to be clear in what I try to explain but seems something is not going the way I see things ..

for an VBA user this problem, I think is a pice of cake ..but I try to help myself without help :P..so the aplication was started from excel because I just figure out how to start word via on button from excel (then I was glad about that)..i like to be an separat aplication (not started from an diffrent aplication like excel) but this is my level so I have to handel myself (this I know this I do)..

I know also that an check box just return TRUE and FALSE ..IF check box = 1 >> TRUE then start an comand that will write an TEXT , if is 0 >> False Then stay and do nothing.

so:

Private Sub CommandButton1_Click()
Dim wdAppl As Word.Application
Set wdAppl = CreateObject("Word.Application")
aDoc = wdAppl.Documents.Add()
wdAppl.Visible = True

Dim text1 As Range
Set text1 = ActiveDocument.Range.NormalTemplate.AutoTextEntries _
With text1
.TypeText "This is some text that I want to insert somewhere. I am not sure where because i am not paying attention."
.Font.Name = "Arial"
.Font.Size = 24
End With
End Sub
'start word via button and insert condition from cheack box

Private Sub CheckBox1_Click()
If CheckBox1.Value = True Then
text1=TRUE
End If
If CheckBox1.Value = False
text1=FALSE
End If
End Sub
' check box condition for some text

but ofcorse is not going ..this is better? I meen do you undertand what I tray to tell ? or I am just to far away

thank you

A.

fumei
06-19-2009, 09:27 AM
"I am sorry but my VBA language is almost null"

I keep on repeating myself over and over. This is NOT, repeat NOT, about your skill at VBA. It is about being clear in your thinking.

We can help with the VBA coding part. We would be happy to help. But it is NOT about VBA coding. It is about thinking...logic.

"i like to be an separat aplication (not started from an diffrent aplication like excel) "

Then that is not VBA. VBA must use an application. It is Visual Basic for Application.

Further...again...you do not state clearly what you mean by that. If it is not "started from a differnt application"...where EXACTLY does it start?

Sorry, but someone else is going to have to help you (although I doubt it). This is just too frustrating for me. I would like to help, but until you pay attention to what I have been trying to say, I fail to see much point in responding.

BTW: the code:
Dim text1 As Range
Set text1 = ActiveDocument.Range.NormalTemplate.AutoTextEntries
will fail.

You declare text1 as a Range...and try and set it as an AutoText. This should result in a Mismatch error.

Good luck.

apangratiu
07-01-2009, 12:23 AM
thx Fumei

so ..I made the script like this ..I figure out how to link check box and instruction to be done ..


Private Sub CheckBox1_Click()

End Sub

Private Sub CommandButton1_Click()
Dim wdAppl As Word.Application
Set wdAppl = CreateObject("Word.Application")
aDoc = wdAppl.Documents.Add()
wdAppl.Visible = True
If CheckBox1.Value = True Then
With wdAppl.Selection
.Font.Name = "Arial"
.TypeText "text that has to be type: "
.TypeParagraph
.TypeParagraph
End With
End If

End Sub

with the small steps I think I will help myself ..ok, for you it is an piece of cake but for me, it is an alien language.. anyway thank you guys

fumei
07-02-2009, 11:24 AM
Shrug.

You may have other questions at some time. May I offer some suggestions?

Please read up on asking questions. It really helps if you ask as clearly as possible.

Answer questions, as best as you can, when people ask YOU questions.

You did not answer a single question I asked. Not one.