View Full Version : Logic versus Code
fumei
02-11-2009, 11:22 AM
I know I have ranted quite a bit about the importance of understanding logic. In those rants I have tried to make the point that a lot of people concentrate overly on the coding aspect. I have stated that in many cases the coding itself is simple, or minor, but that the thinking, the grasp of logic was of far greater significance.
I would like to demonstrate. I am going to post images, and as I can only do one at a time, please be patient as I wait the 30 seconds before posting the next one.
Here is image #1. This is a userform I have in a global template. This userform can control ALL aspects of the relationships between ALL the formfields in any document. This first image is the opening display of the userform.
The "Include" checkboxes are not checked.
fumei
02-11-2009, 11:28 AM
Darn, I thought attaching a jpg displayed it IN the post. Darn, I did not realize I would have to have them at at URL and do a http://...... to display them.
As I have no way to do that, and having people download the images showing what I wanted to talk about...ummm, Mr. Moderator can you delete this thread. Too much of a pain in the butt for people I think.
My apologies. I thought I was trying to be helpful, but methinks it is not worth it.
TrippyTom
02-11-2009, 11:56 AM
Wait!
Fumei, I'm sure people (like myself) would still be interested in seeing what you had to say.
Can you put all the images in a doc or something, and zip it up and just post that?
lucas
02-11-2009, 12:58 PM
Attaching using manage attachments does display your jpg in the post Gerry.....it is visible...
Paul_Hossler
02-14-2009, 11:51 AM
Yes, please continue -- I think that waiting until you understand what you want to do is oft times more beneficial than jumping in and start slinging code, only to find out later you're too far down the path to change, or to even realize you need to change.
Almost always (in my experiences), improving the correct 5% of the code provided the biggest improvement
Especially in anything related to the UI
Paul
TrippyTom
02-14-2009, 01:20 PM
I think a lot of it (at least in my case) is I don't know the better way to achieve what I want to do.
For instance, using range instead of selection. I'm still trying to grasp all the different ways of using Range to get at the text I want to work with.
And not being familiar with the Word object model doesn't help either.
fumei
02-16-2009, 10:29 AM
Steve I tried to do that in my post. It attached the jpg as a file. Let me try again.
fumei
02-16-2009, 10:35 AM
Oh crap, hit the wrong button. Maybe I should wait, I am having a bad day.
Nope. I tried to put the jpg in again, and it refused saying I have already attached this file in this thread.
Tommy
02-16-2009, 10:49 AM
Gerry, :hi:
The image is in the original post. :cloud9:
I am not willing to delete this thread as requested because I believe that the information you are about to give will be WAY to vaulable to stop. I will be more than willing to "reduce the clutter", if requested, after you have gotten going. :bow:
I hope your bad day gets better.:beerchug:
fumei
02-16-2009, 11:55 AM
"The image is in the original post." Yes, but as an attachment, not displayed in the post. I wanted to have images displayed, and add text about what people were seeing.
OK, I wrote a document covering what I want to say, or at least to start. My purpose here is to try and get a discussion, not to supply code. I am open for comment, objections, questions. I am willing to share some code, but the whole point I am trying to make is that - for the most part - actual coding comes a far far distant second to logic, when it comes to using VBA to make professional looking/acting applications.
fumei
02-16-2009, 12:02 PM
BTW: for anyone interested, please note that:
1. there is not a single instance of Normal style in the document (including the paragraphs that hold the images - they use..."ImageHolder" style);
2. there is not a single instance of those "extra" paragraphs to make space between paragraphs;
3. there is not a single instance of those StyleName + attribute abominations. All instances of formatting different within a paragraph is handled by character styles.
I am not trying to toot my own horn here, I am trying to be consistent in my message to those who wish to use Word well. That message is simple:
1. THINK
2. Use Word as it is designed to be used.
TrippyTom
02-16-2009, 02:44 PM
Fumei,
Thanks for taking the time to write that up.
I should add, that logic is very much tied to conceptual understanding. For example, once you really grasp the concept of Range with Word, the coding of it becomes, well trivial. It is NOT difficult.
This is the last paragraph and I was hoping you would go into using Range a little more. The macro recorder uses Select in almost everything it does, which makes it difficult for new Word vba users to know they should be using Range instead.
But even after they realize they should be using Range, their limited knowledge of the Word object model becomes a roadblock. This is the point I'm at now and it's very frustrating because the help system only suggests 1 or 2 examples of how to use it.
I think i have a very clear understanding of what I want to do, but because of my limited understanding of the object model, I just thrust myself into coding (albeit somewhat blindly) using the Select method.
Is there a website or something that shows real-life examples of how people are using Range in Word vba?
CreganTur
02-17-2009, 10:39 AM
gerry,
I was going to PM this to you...
you may already know this, but you can display images within a post if the image you want to display is on a website somewhere. Then you can use the [ img ] [ / img ] tags- wrap them around the hyperlink to the image and it will display in the text. Like this:
http://img502.imageshack.us/img502/706/spoonap8.gif
And this:
http://img502.imageshack.us/img502/8936/dunecatoj5.jpg
If you don't have a place to host your images, let me know and I can host them for you.
fumei
02-17-2009, 11:32 AM
RE: web site regarding Range, no not really. However, there are a number of threads both here and at Tek-Tips that cover aspects of using Range. I have done a number of them, as well as other knowledgeable Word users like Tony Jollans.
"I think i have a very clear understanding of what I want to do, but because of my limited understanding of the object model, I just thrust myself into coding (albeit somewhat blindly) using the Select method."
I guess what I am hinting at with THIS thread is that if you (or anyone else) does indeed have the logic - what you precisely want to do - figured out, I (and others) will be happy to assist you with coding using Range rather than Selection.
The most important things (IMO) to grasp about Range are:
1. it represents the range within the document from Point_A to Point_B - thus it really can be considered as [/b]numbers[/b] (its .Start and .End values);
2. it does NOT use the GUI, GUI resources - thus the reason it is faster;
3. any .Find operation of a Range literally changes the range with each successful .Found. Say you have (and let's pretend it is the whole document):
blah blah blah yadda blah
blah with some other textblah blah yadda blah
Take a look at the following code.
Dim r As Range
Set r = ActiveDocument.Range
' display the whole shebang
MsgBox "Start = " & r.Start & _
vbCrLf & "End = " & r.End
With r.Find
Do While .Execute(findtext:="yadda", Forward:=True) = True
' display Start and End after EACH successful .Found
MsgBox "Start = " & r.Start & _
vbCrLf & "End = " & r.End
Loop
End With
First there is a message that displays the range Start and End - right after the range object is set for the whole document. Result?
Start = 0
End = 72
Now, there are two "yadda". There is a Do loop that finds them all.
After the first "yadda" the message displays:
Start = 15
End = 20
The range object itself has been changed to those numbers.
After the next .Found, it will display:
Start = 61
End = 66
The range object itself has been changed to those numbers.
To show it graphically (I will bold what is the range), when first declared as the whole document (Set r = ActiveDocument.Range), r.Text =
blah blah blah yadda blah
blah with some other textblah blah yadda blah
When .Find finds the first "yadda", the range object (r) IS - literally - (in bold):
blah blah blah yadda blah
blah with some other textblah blah yadda blah
When .Find finds the next "yadda" the range object (r) IS - literally - (in bold):
blah blah blah yadda blah
blah with some other textblah blah yadda blah
As the messagebox displaying the Start and End clearly show. The range literally changes during a .Find operation.
IF you perform operations on the range - and ONLY the range - during a .Find operation, you do not need to do anything. VBA automatically collapses the range to the current end, and continues on.
Dim r As Range
Set r = ActiveDocument.Range
With r.Find
Do While .Execute(findtext:="yadda", Forward:=True) = True
r.Font.Bold = True
Loop
End With
finds all the "yadda" strings, and bolds them.
IF you change the range during a .Find, then you must tell VBA to adjust the range Start and End parameters. Here is a demo using the same text as above.
blah blah blah yadda blah
blah with some other textblah blah yadda blah
Do NOT run this except using F8 in the VBE - i.e. Step through - as THIS IS AN INFINTE LOOP!!!!!!!
Dim r As Range
Set r = ActiveDocument.Range
With r.Find
Do While .Execute(findtext:="yadda", _
Forward:=True) = True
Debug.Print r.Start
r.Expand Unit:=wdWord
r.Font.Bold = True
Loop
End With
I added that Debug.Print to let you see that it does indeed loop, but it will ALWAYS end up with .Start = 15, and will - again - be an infinite loop. Do not execute except as stepping through, so you can stop it. Otherwise you will have to blow Word away to get out.
Why? Why does it infinite loop?
A range .Find operation implicitly collapses each .Found Start and End numbers to End.
If you code explicit values, those take precedence.
So, NOT explicitly changing the range Start and End values:
1st Found "yadda" - range Start = 15, End = 20
range collapsed to end - range Start = 20, End = 20
therefore the previous "yadda" is BEFORE Start and will not be found...the next one will.
Explicitly changing the range Start and End (r.Expand Unit:=wdWord), makes the range (at the end of the current loop) Start = 15...therefore the SAME "yadda" will be found. Again and again and again, an infinite loop.
The irony here is that r.Expand Unit:=wdWord does not do anything really. "yadda" is already found, so the explicit instruction makes it what it already is!
Yet, BECAUSE the range parameters have been explicitly changed, they take priority.
So, the rule is (with .Find), if you change the range, explicitly collapse it afterwards.
Dim r As Range
Set r = ActiveDocument.Range
With r.Find
Do While .Execute(findtext:="yadda", _
Forward:=True) = True
Debug.Print r.Start
r.Expand Unit:=wdWord
r.Font.Bold = True
r.Collapse 0 ' collapse to End
Loop
End With
Now it will process through all the "yadda" - no infinite loop.
Of course, clean coding would have it as:
Dim r As Range
Set r = ActiveDocument.Range
With r.Find
Do While .Execute(findtext:="yadda", _
Forward:=True) = True
Debug.Print r.Start
With r
.Expand Unit:=wdWord
.Font.Bold = True
.Collapse 0
End With
Loop
End With
The key to using ranges is to keep in mind my point #1. Ranges are the numbers from Point_A to Point_B.
For this reason, essentially everything in a document has a range, because everything covers SOME Point_A to Point_B.
Yes?
Ranges are the numeric values of the Start and End of objects. Those objects may have parts - child objects - and those ALSO have ranges.
Say you have a table in a document. It is bookmarked as "Table1". In Cell 2 - NOTE! using range in a table means you can use a Cell Index number, you do NOT have to use its Row/Column - is the text "Blah blah blah yadda blah"
"yadda" is bookmarked with the name "TestText".
Take a look at this.
Sub RangeUponRangeUponRange()
MsgBox ActiveDocument _
.Range _
.Bookmarks("Table1") _
.Range _
.Tables(1) _
.Range _
.Cells(2) _
.Range _
.Bookmarks("TestText") _
.Range.Text
End Sub
Result? A messagebox displaying "yadda".
What the code is "saying":
Within the document RANGE, and in THAT
within bookmark "Table1" RANGE, and in THAT
within Table(1) RANGE, and in THAT
within its Cell(2) RANGE, and in THAT
within its bookmark "TestText" RANGE,
what is the .Text value?
Admitedly, there would be much shorter ways to get at that value, like this:
Msgbox Active.Document.Bookmarks("TestText").Range.Text
Thus dealing directly with the object. My point being is that virtually ALL objects in Word have ranges. They are crucial to using Word VBA efficiently.
Perhaps more importantly though is understanding objects themselves!
I am ranting again. Bottom line is that if you have a clear understanding of what it is you want to do, try giving your efforts a shot, using ranges. If you have trouble, post questions.
fumei
02-17-2009, 11:37 AM
cregan: "you may already know this, but you can display images within a post if the image you want to display is on a website somewhere."
Yes, I know that. Please see post#2.
"Darn, I thought attaching a jpg displayed it IN the post. Darn, I did not realize I would have to have them at at URL and do a http://...... to display them.
As I have no way to do that,"
I have no way to post images to a URL. I no longer have internet access (or computers!) at home - I dumped my access and got rid of all my computers.
Here at work, we have a VERY restrictive firewall, both in and out. I have no way to post images anywhere.
TrippyTom
02-17-2009, 07:46 PM
Fumei:
Wow. :) That's a lot to digest! Thanks for explaining it for me. I needed that. And I didn't realize I didn't have to use columns and rows in tables either. Thanks for that as well.
I will try to rewrite my code now based on your and Macropod's help. I'll get back to you with specific questions in a separate thread if I need help.
But in keeping with this topic, my other problem seems to be explaining what I think is clear in my head into a post on this forum. :) I don't think anyone can help with that besides myself.
Cosmo
02-18-2009, 06:50 AM
Thank you very much, fumei!
fumei
02-18-2009, 10:14 AM
"But in keeping with this topic, my other problem seems to be explaining what I think is clear in my head into a post on this forum. I don't think anyone can help with that besides myself."
Tom, indeed. I mean indeed that it is indeed up to you. I know some have found me pushy and rude when I try to get them to be precise and explicit, and I admit that sometimes I am.
However, the whole point of me posting this thread is to hammer home that it is the thinking that is important. Myself, and others here, can help people with coding. We can not help with someone's thinking clearly.
Further, we (or at least I) will not help someone if they obviously have not tried to work it out themselves. I never give full code solutions to someone who is simply looking for a solution. I will give full code solutions to someone who has made serious effort, shows that they have, but are stuck on a technicality.
The more clearly you think it out - and for complicated processes I would recommend actually writing it our, on paper - the easier it becomes. Draw a flowchart. You will be amazed when you do. Logically, things almost always are more complicated than our initial desires (intentions).
I want to do "this".
Yes....well, first of all, what EXACTLY is "this"? The more you have the ability to do the determination of what exactly "this" is - what you are trying to do - the more you realize the actual coding parameters are secondary.
Another example that I have ranted about.
Someone writing 60 IF...THEN statements/instructions, when 1 (one) Select Case will do the job. This is not a coding issue, it is an understanding and using logic issue.
fumei
02-18-2009, 10:26 AM
Oh....BTW, in my discussion on the Range.Find method, when I mentioned that VBA automatically collapses the the End for each Found, this is only true if you have Forward:=True (which in my code I do). You can of course make the .Find operation action in other directions, or stop.
However, the majority of Find requirements are for "start at the beginning and work forward".
Once again...be explicit about what you are trying to do. there may be, in fact, a reason for doing a search (.Find) differently. If there is, then you code it differently.
You code for what you NEED. IMO, that is all one should do. I see lots of code that does all sorts of things, but only a small fraction of which actions what the coder really needs.
With the point (again) being...figure out what you actually need.
THINK first.
CODE second.
What happens over time (and of course practice...), is that your understanding deepens and you THINK faster and more efficiently.
Powered by vBulletin® Version 4.2.5 Copyright © 2025 vBulletin Solutions Inc. All rights reserved.