Please help me grasp program structure

D

dak

Hi, I'm a very excited newbie... used to program but it's been awhile.
I'm making a Word VBA program to parse a table and make a new sheet
of labels, and I totally get all the loops and subroutines and
functions and stuff... I'm just confused about how to start.

When I start my program, from a menu or a shortcut key or whatever I
end up choosing, it will call up a dialog box, and dismissing that box
will start the whole ball rolling. Do I make a whole bunch of
individual macros (subroutines) and call them from a main program? Do
I list them all in order? As I add functions/subroutines/whatever
they just get added to my overall program. But I don't know how to
run them. All of the coding samples I've seen only show individual
functions or subroutines. I'd love to see a complete VBA program that
calls all the different functions in order.

I feel lame... I feel like I grasp all the individual pieces, I just
can't see the whole picture. I know how I want it all to run, I just
don't get how I'm supposed to organize everything.

Thanks so much in advance for any help!!

--dak
 
H

Helmut Weber

Hi Dak,
the problem is where to start explaining.
Have you ever heard of pseudocode?
That is writing down in a way similar to a programming language
what you want to do and in which order.
http://www.csc.calpoly.edu/~jdalbey/SWE/pdl_std.html
Whether you create a whole bunch of single macros,
or one big one, depends on what you actually want to do.
Reusable code is better stored in a single macro or function.
On the other hand, to create hundreds of macros constisting of
only one line of code is of no use. And matters of taste and style
play a role, too. Macros can call other macros, functions can
call other functions, macros can call functions, functions can call
macros. Like:
sub MyProg
macro1
macro2
macro1 again
if number of words > 1000
msgbox "too long"
endif
end sub
 
J

Jonathan West

dak said:
Hi, I'm a very excited newbie... used to program but it's been awhile.
I'm making a Word VBA program to parse a table and make a new sheet
of labels, and I totally get all the loops and subroutines and
functions and stuff... I'm just confused about how to start.

When I start my program, from a menu or a shortcut key or whatever I
end up choosing, it will call up a dialog box, and dismissing that box
will start the whole ball rolling. Do I make a whole bunch of
individual macros (subroutines) and call them from a main program? Do
I list them all in order? As I add functions/subroutines/whatever
they just get added to my overall program. But I don't know how to
run them. All of the coding samples I've seen only show individual
functions or subroutines. I'd love to see a complete VBA program that
calls all the different functions in order.

I feel lame... I feel like I grasp all the individual pieces, I just
can't see the whole picture. I know how I want it all to run, I just
don't get how I'm supposed to organize everything.

Thanks so much in advance for any help!!


Hi dak,

These articles may help you

The art of defensive programming
http://word.mvps.org/FAQs/MacrosVBA/MaintainableCode.htm

How to cut out repetition and write much less code, by using
subroutines and functions that take arguments
http://word.mvps.org/FAQs/MacrosVBA/ProcArguments.htm


Also, the follwojg article has a link to a template which has a complete
(though fairly small) application, with multiple routines

Getting access to the Document Properties of a Word file
http://word.mvps.org/FAQs/MacrosVBA/DSOFile.htm
 
D

dak

Jonathan and Helmut, thank you both.

I actually have written the program in pseudo code, and there are
definitely a couple of repetitive tasks that I'll want to call from
within the program. My problem was simply figuring out the structure
of the document that I create in the Word VBA editor.

From your help and poking around I found this:
You can then call it like this:

Sub MainMacro()
    'Remove double spaces
    Call DoFindReplace("  ", " ")
    'Remove all double tabs
    Call DoFindReplace("^t^t", "^t")
    'Remove empty paras (unless they folow a table or start or finish a doc)
    Call DoFindReplace("^p^p", "^p")
    'etc etc
End Sub

Which helps me understand the structure of the main macro. Then I've
discovered the "Insert" menu for adding other subroutines and forms
and stuff. So as I understand it, my document should look like:

-----------------------------
Declare Constants

Sub MainMacro
Dim statements
Main Macro code and calls to other subs and functions
End Sub

Sub OtherSubroutine
code
End Sub

Function SomeFunction
code
End Function
-----------------------------

Then I simply call the MainMacro from the document and everything just
works. Is that correct?

Thanks,
--dak
 
H

Helmut Weber

Hi dak,
right! Plus,
in many books on programming I am finding the advice,
to keep the scope of variables as limited as possible.
Which means, to declare variables in a sub or a function,
unless a global declaration at modul level, e.g., is required.
Plus, have a look at
call "by value" (ByVal) and call "by reference" (ByRef).
By the way, as this seems to be a "beginners' again question",
DoFindReplace("  ", " ") would probably not remove
all doubles spaces, in case there are more than 2 in sequence.
But that's another story.
---
Greetings from Bavaria, Germany
Helmut Weber, MVP
"red.sys" & chr(64) & "t-online.de"
Word XP, Win 98
http://word.mvps.org/
 
W

Word Heretic

G'day (e-mail address removed) (dak),

My advice is being clearly explained in a lengthy article for the MVP
site called Your Documentation Department. You need to create a
virginal normal, make a few copies of it, moving them to your startup
folder and renaming them DocMgr, Author and User. This will be a good
start for you to start storing generic code in.

Then you should get the VBA Programmers Handbook (Ken Getz et al) and
check out how classes work, so you can start creating single public
instancing of private class modules which will help unclutter the IDE.

Its pretty simple when you get the hang of it. Create a new class
module called oMyClass, and in a standard code module use Public
MyClass as oMyClass and then have your Document_Open event do the If
MyClass is Nothing then Set MyClass = New oMyClass



Steve Hudson - Word Heretic

steve from wordheretic.com (Email replies require payment)
Without prejudice


dak reckoned:
 
D

dak

Thanks all for your help.

I've proudly created a very powerful, flexible, and fast (relatively)
program that takes a word table of rows of recipients and columns of
distributions and creates a new label sheet of the appropriate
distribution, with optional address labels for those out of the
office. I'm very proud of it, and I owe it all to posts in this
group, as well as the help file and the O'Reilly "Writing Word Macros"
book. (By the way, I'm on Word X for Mac)

I look forward to future, more complex challenges using Word Heretic's
suggestions to start...

--dak
 

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