PDA

View Full Version : VBA word basics?



Ago
03-24-2008, 11:58 AM
i have been useing VBA for excel for a while now and can say that im quite good at it.

i have a few macros that i need to convert to VBA word.
and its alot more diffrent than i thought.

in excel there is a command called range(column, row), how can i do this in word?
lets say i want to know if the fifth word in line 70 is "hello", how would i do that?

thanks for helping me with this noob question

Tinbendr
03-25-2008, 06:33 AM
Word is much more fluid than Excel, and so, does not relate to coordinates very well. (Except for tables, which is similar to Excel.)

If you had a more defined request, we might be able to steer you in the right direction.

In the mean time, here (http://msdn2.microsoft.com/en-ca/library/aa679615(office.10).aspx) is some reading material for the Word object.

Ago
03-25-2008, 08:02 AM
sorry about that..

what im trying to build is a script that can see if the information is worth saving or if the whole line should get deleted.
thats the start.

to know if its something worth saving there are several rules.
1. first "thing" on each line should be a number. by "thing" i mean it can be both word or number. not a number delete the line.
2. the 6:th "thing" should contain three dots and one colon. if not delete the line

if a line follows all rules it should stay.

this is just two of the rules, there are several others.
if i just know how to point the code at a word it should be quite easy.

was this more of a help?

fumei
03-25-2008, 09:42 AM
OK, numero uno concept in Word is paragraph.

Yes, Word indeed uses Range...a lot. In fact, a great deal of how Word operates is by Range. The basic unit for a Range, the centerpiece of the Word Object Model (or one of them) is the Range of a paragraph.

You must grasp what Word means by paragraph if you want to use VBA in Word.

You use the word "line". This is a very tricky thing in Word.

Now if your document has a paragraph mark at the end of each "line" - i.e it IS a paragraph - then getting information from the 70th paragraph is easy.

Dim r As Range
Set r = ActiveDocument.Paragraphs(70).Range

r.Text = the whole paragraph
r.Words(1) = the first word

etc. etc.

Or:
Dim oPara As Paragraph
For Each oPara In ActiveDocument.Paragraphs
' do what testing you are doing for each paragraph


While I disagree with Tinbendr regarding the statement Word "does not relate to coordinates very well" - in fact, everything Word knows about where things are in a document is...sort of...coordinates. Ranges in fact. Ranges start at 0 - the start of the document - and count characters.

For example:
MsgBox "The 70th paragraph text is: " & _
ActiveDocument.Paragraphs(70).Range.Text & _
vbCrLf & "and Starts at " & _
ActiveDocument.Paragraphs(70).Range.Start & _
", and Ends at " & _
ActiveDocument.Paragraphs(70).Range.End


gives (in the document I am current viewing):

"The 70th paragraph text is: Types of Styles
and Starts at 4235 and Ends at 4318."

This is a - sort of - coordinate. No, it is a coordinate.

However, I certainly agree with Tinbendr's comment:

"If you had a more defined request, we might be able to steer you in the right direction."

Ago
03-25-2008, 10:09 AM
Perfect!!
thats exactly what i wanted!

but i found a problem.

in this text

13 0 999 -=LkL=-Santa 3900 77.226.169.207:12203 52009 5000

when i use this code

Dim r As Range
Set r = ActiveDocument.Paragraphs(1).Range

MsgBox r.Text & Chr(10) & r.Words(4)


i dont the whole name, wich should be -=LkL=-Santa.
is equalsigns some kind of delimiter?

thanks alot for this help!

fumei
03-25-2008, 10:53 AM
OK, you say the paragraph is:

13 0 999 -=LkL=-Santa 3900 77.226.169.207:12203 52009 5000

But is it? Do you have Show/Hide on? You should. Is there are paragraph mark only at the end? You state:

"the whole name, wich should be -=LkL=-Santa"

How can that be, if you also state the paragraph is:

13 0 999 -=LkL=-Santa 3900 77.226.169.207:12203 52009 5000?

Which is it? What about the 3900 77.226.169.207:12203 52009 5000? What about the 13 0 999? Is that not part of the "whole name"?

Note that range.text will include the terminating paragraph mark.

fumei
03-25-2008, 11:01 AM
If I put:

13 0 999 -=LkL=-Santa 3900 77.226.169.207:12203 52009 5000

as the first paragraph (Paragraph(1))

and execute:

Sub whatever()
Dim r As Range
Set r = ActiveDocument.Paragraphs(1).Range

MsgBox r.Text & Chr(10) & r.Words(4)
End Sub


I get:

13 0 999 -=LkL=-Santa 3900 77.226.169.207:12203 52009 5000

-=

13 Word(1)
0 Word(2)
999 Word(3)
-= Word(4)

Yes, -= is the fourth "word" to VBA. All punctuation is a separate "word" to VBA. Including the period at the end.

This is a sentence.

The above (to VBA) has six words.

1 This
2 is
3 a
4 sentence
5 .
6 the paragraph mark

Again, in Tinbendr's words: "If you had a more defined request, we might be able to steer you in the right direction."

Ago
03-25-2008, 11:10 AM
EDIT: i didnt see your last message as i wrote this

the fourth part of the string/line is the playername, so in the example i gave you -=LkL=-Santa is the playername.

i need to check each word of each paragraph to make sure its correct.

so if we look at the rules that i talked about earlier, this line passes since the first word is a number and the 6:th word has three dots and one colon.

but because the name is now devided in to 4 parts (word(4) -word(7)) it makes it very hard for me.
so now word(9) should be 77.226.169.207:12203 (havnt checked if it is), and thats fine if its always 9.
so if the name is not santa on next paragraph will it still be 9?

do you understand what i mean?

Ago
03-25-2008, 11:20 AM
If I put:

13 0 999 -=LkL=-Santa 3900 77.226.169.207:12203 52009 5000

as the first paragraph (Paragraph(1))

and execute:

Sub whatever()
Dim r As Range
Set r = ActiveDocument.Paragraphs(1).Range

MsgBox r.Text & Chr(10) & r.Words(4)
End Sub


I get:

13 0 999 -=LkL=-Santa 3900 77.226.169.207:12203 52009 5000

-=

13 Word(1)
0 Word(2)
999 Word(3)
-= Word(4)

Yes, -= is the fourth "word" to VBA. All punctuation is a separate "word" to VBA. Including the period at the end.

This is a sentence.

The above (to VBA) has six words.

1 This
2 is
3 a
4 sentence
5 .
6 the paragraph mark

Again, in Tinbendr's words: "If you had a more defined request, we might be able to steer you in the right direction."


ok i see! thanks!
so all symbols creates a new word?
that is probably not such a big problem since i can do something like.
if word(j) has a = then j=j+1
and put that in a loop untill something else happens. lol

wow i must say its alot harder than i thought to write VBA i word

fumei
03-27-2008, 09:02 AM
I did not follow or understand your post #8 whatsoever. But if something here has helped I am glad.

Ago
03-27-2008, 01:44 PM
ok sorry about the fuzzy post.
i willtry to explain it.

13 0 999 -=LkL=-Santa 3900 77.226.169.207:12203 52009 5000
from left to right.
playernumber, score, ping, playername, lastmsg, IP: port, qport, rate.
this is a gameserver feeds me with.
but sometimes the message has got currupted and it might look like

13 0 999 -=LkL=-Santa 3900 77.

meaning an important part of the message is missing.
so this message is of no use for me, thats why i want the script to delete this line.

offcourse its easy to spot these lines manually, but when you get hundereds of lines it get quite boring.

so what i wanted to build is a script that checks each line to make sure its complete and not currupted in any way.
i have been busy at work so i havent had time to write anything yet but i have been thinking of it alot and i think this is going to be very hard to create something like that in word.
so yes you have helped me understand that i probably shouldnt even try :-)

fumei
03-31-2008, 10:51 AM
"so yes you have helped me understand that i probably shouldnt even try :-)"

No! No! NO!

Try, by all means try. It should not, actually, be very hard. What I have been trying to get you to see, is you have to be very very explicit in your logic. The coding to do anything like this is not very hard. It is the thinking of the absolutely explicit rules (the logic) that is hard.

"so what i wanted to build is a script that checks each line to make sure its complete and not currupted in any way."

This is NOT, repeat NOT, a coding issue or problem. This is a logic problem.

Say your....

13 0 999 -=LkL=-Santa 3900 77.226.169.207:12203 52009 5000
from left to right.
playernumber, score, ping, playername, lastmsg, IP: port, qport, rate.
this is a gameserver feeds me with.
but sometimes the message has got currupted and it might look like

13 0 999 -=LkL=-Santa 3900 77.

THAT one could solved extremely easily. Let's say you have already made the range the sentence...whatever it is.

r.text = 13 0 999 -=LkL=-Santa 3900 77

Now, according to a VERY simple logic rule, the above is not correct. How?

If Len(r.text) < 30 Then
Msgbox "YUCK! This is just wrong!"


Len(13 0 999 -=LkL=-Santa 3900 77) = 29

so...it is wrong.

That is using a simple - this sucker is not long enough to be right - piece of logic.

Yet logic it is. YOU, as the developer, have to explicitly state what are those pieces of logic.

Expanding the above example, is there a minimum length that could be valid? If so, then anything less than that has to be incorrect, no matter what it is.

THAT is logic.

If Len(r.Text) < minimum length Then
wrong wrong wrong

So say it is 30 - I am just tossing that out, YOU have to figure the logic - if the string is:

13 0 999 -=LkL=-Santa 3900 77
12 0 431 -=ght=-Blah 1200 77
00 0 999 -=LkL=-Santa 3900 63
13 0 999 -=LkL=-Santa 3900 51

are ALL, logically, incorrect. You can, logically, eliminate them ALL (regardless of the actual text content), by that one piece of logic. A minimum length.

That may or may not apply for you. Again, YOU have to determine the logic.