une


I started learning VBA in MS Word yesterday. I have some programming experience in C++ and Java. I am trying to understand how the environment is structured and what Projects, Objects and Modules represent.

For example I recorded a few macros using the macro recorder and then opened the VB Editor to see what was created. I notice that under "Project" are 3 folders. One is named "Microsoft Word Objects". Within this folder is what looks like a file named "This Document". What does "Project" represent What does "This Document" represent What is the difference between "This Document" and the "Project"

Also under "Project" is a folder named "Modules". Within this folder is what looks like a file named "New Macros". This "file" seems to hold the code that represents the macros I recorded. Right clicking on the "Modules" folder allows me to insert another module into which I can add code for a Sub/s (short for subroutine/s I assume) via the VB Editor to do whatever.

Why would you put code for different Subs into different modules The simple tutorials I have been studying seem to put all Subs into the one module. Why is the code for the recorded macros put into a separate module

I am trying to work out exactly what Projects, Objects, and Modules are and how they interact. It seems like some sort of hierarchy. This environment is very new and quite confusing to me at the moment. Any help or references appreciated.




Re: Projects, objects & modules

Andy Pope


Hi,

Project contains all the classes related to the document.

ThisDocument is a special class referencing the document within the project. The events of the document are exposed in the Thisdocument object.

Modules contain code which is available to the whole project, taking into account any scope issues.

Multiple modules tend to be used on more complex projects.

Recording macros in a freshly opened copy will cause the code to be placed in a new module. Keeps new code away from any previously created and structured coding.

How are you learning VBA

This site might be of interest.
http://word.mvps.org/FAQs/MacrosVBA/index.htm






Re: Projects, objects & modules

une

Thanks a lot for the explanations. I assume the term "class" in this context is analagous to the term when used in the contaext of C++ or Java. The link you provided looks perfect at first glance. It also led me to the whole MVP world, which I am sure to make a lot of use of. I still believe one of the best ways to learn a language is to get hold of lots of basic practical examples that are presented in their ENTIRETYand play around with them. Technical explanations are important but only go so far.

Could you possibly expand on what you meant by the following (particularly underlined sections);
"ThisDocument is a special class referencing the document within the project. The events of the document are exposed in the Thisdocument object."
If you have time perhaps you could provide brief examples. I may not understand them, but who knows where it will take me.

To start learning I Googled for basic tutorials and found;
http://users.bigpond.net.au/gradley/vbatutor/
This was not a bad start.
It was the best intro I could find.

I found many others, but people who can write code AND explain what they are doing to newbies are rare in my experience. Simple yet integral steps and concepts in the process of creating and running code are often missed or quickly glossed over in order to get to what the writer finds interesting. One of the biggest problems I find is that writing of the code in isolation is explained well, but how you implement it within the environment and then use it is often not explained clearly enough.

Thanks for your help so far.







Re: Projects, objects & modules

Andy Pope

Place this code in Thisdocument. Save/close the file then re open it.
The Open event will be trigger and the message displayed.

Private Sub Document_Open()

' will display a message box when document is opened.

MsgBox "Hello World"

End Sub


ThisDocument refers to the document that contains the code currently being executed.

The application also has an ActiveDocument reference which as the name suggests refers to the active document. The updating of this reference is handled automatically by the application.

Copy the following code into a standard module within your document. Within the VBE (Visual Basic Editor) environment press CTRL+G to display the immediate window. From here you can run the macro by simply typing DocInfo and pressing enter.
The message boxes should display the same name for This and Active document.

Now open another document file and re run the macro. This time the activedocument name should be that of the newly opened file whereas the Thisdocument will be the same.

Sub DocInfo()

MsgBox "Thisdocument is " & ThisDocument.Name
MsgBox "Active Document is " & ActiveDocument.Name

End Sub






Re: Projects, objects & modules

une

Thanks for the examples. I got the 1st one to work but not the 2nd.

I have a document named Test01.doc. The first code example was placed in ThisDocument. Upon saving, closing and opening the document, the Message Box appeared as it should. I then entered the followng code into Module 1 within the VB Editor;

Sub DocInfo()
MsgBox " Thisdocument is " &
ThisDocument.Name
MsgBox "Active document is " &
ActiveDocument.Name
End Sub

I exited the VB Editor, saved the document, closed it and then opened it.

I opened the VB Editor and pressed CTRL + G to show the immediate window.

I entered DocInfo into this window and pressed enter.

An error window appeared saying "Compile error:  Syntax error". The code window for module 1 also appeared with the first line of code "Sub DocInfo()" highlighted in yellow. I closed the Immediate window, closed the module 1 code window and then tried to close the VB Editor window by clicking the x at top right. This caused a warning message box to appear saying "This command will stop the debugger". I select OK in this warning box and then click the x at top right in the VB Editor window again. The VB Editor window then closed.

What went wrong

 

 





Re: Projects, objects & modules

Andy Pope

Does the line of code appear on 1 line or 2

This will error
MsgBox " Thisdocument is " &
ThisDocument.Name


This should work
MsgBox " Thisdocument is " & ThisDocument.Name




Re: Projects, objects & modules

magicalclick

MsgBox " Thisdocument is " & _
ThisDocument.Name

You need the underline to continoue a statement otherwise they think you have two statements. It is simular to forget ; at the end of statement in C like programming, but effect is in reverse.





Re: Projects, objects & modules

une

Yes, putting the statement's code in 1 line in the text editor worked.

Separating the statement into 2 lines with the underscore at the end of the first portion of the statement did not work , I got a syntax error.

I will get to the questions I have about "ThisDocument" vs "ActiveDocument" after I sort out some basic syntax problems I seem to have.

How do you signify the end of a statement in VBA

Does simply entering a non visible "return" tell the compiler that that is the end of the statement

If so, the underscore to tell the complier that the statement continues on the next line makes sense, but I could not get this to work in practice.





Re: Projects, objects & modules

Andy Pope

Yes new lines denote end of statement, with the following excepts.

To continue a statement over multiple lines end each line, except the final one, with SPACE and UNDERLINE.
e.g.

Msgbox "Hello" & _
"World"

To have multiple statements on one 1 line use COLON.
e.g.

Msgbox "Hello":Msgbox "World"







Re: Projects, objects & modules

une

Thanks, I've got the syntax worked out now.

Now I will look at "ThisDocument" vs "ActiveDocument".

Why is the following code in Microsoft Word Objects > ThisDocument rather than in a module

Private Sub Document_Open()
' Display mesage box when document opened
MsgBox " Hello World"
End Sub

And visa versa, why is the following code in a module rather than in Microsoft Word Objects > ThisDocument

Sub DocInfo()
MsgBox "This document is " & _
ThisDocument.Name
MsgBox "Active document is " & _
ActiveDocument.Name
End Sub

Also what is the difference between "ThisDocument" and "ActiveDocument". What would be an example of "ThisDocument" and "ActiveDocument" referring to two different .doc files

Maybe these questions don't make sense, but I will work my way to understanding the concepts with some much appreciated help. Thanks again.





Re: Projects, objects & modules

une

Thanks for the help with the syntax.




Re: Projects, objects & modules

Andy Pope

Document_Open in an Event, related to the document.

DocInfo is a standard routine placed in a standard code module so it can be run from any other routine in the project. If this routine was placed in the ThisDocument object then only routines within the object could have called it. Routines outside of the object would needed to use the object interface, so the call would look like ThisDocument.DocInfo.

Thisdocument - where the code resides
ActiveDocument - the document that current has focus in the active window,

With only 1 document open both instances will report the same. Open multiple documents and you should get a different report.






Re: Projects, objects & modules

une

Thanks, I think I am getting closer.
Are the following statements valid

*A Project may incorporate one or more documents.

* If a Project was comprised of say two documents, there be two items shown within the "Microsoft Word Objects" folder. One would be "ThisDocument" (the one being currently worked on) and the other one would be named something else here.

* Code placed in a document under the "Microsoft Word Objects" folder can only be accessed by that document, unless the object interface is used.

* Code placed in a module can be called easily from any document in the Project.

If the last two points are valid, why not just place all code in the "Microsoft Word Objects" folder and employ object interfaces to use it from any document Are modules used for transportability of code

Again, I am feeling my way in the dark but the lights are starting to glow slightly. Thanks.






Re: Projects, objects & modules

Andy Pope

*A Project may incorporate one or more documents.
No. A project can only contain 1 document. It may though contain one of more Code modules or Class Modules.

* If a Project was comprised of say two documents, there be two items shown within the "Microsoft Word Objects" folder. One would be "ThisDocument" (the one being currently worked on) and the other one would be named something else here.
See above.

* Code placed in a document under the "Microsoft Word Objects" folder can only be accessed by that document, unless the object interface is used.
Correct. As the ThisDocument is just a built-in class object.

* Code placed in a module can be called easily from any document in the Project.
Yes can be called from other code and class modules. Taking into account the usual rules with code scope. That is a variable or routine declared as private would not be accessible.





Re: Projects, objects & modules

une

Thanks again. I am getting there but it is all still a little unclear. First of all I think I need a good beginner¡¯s book on VBA (MS Word) that explains the environment in an easily understandable way (especially the how, what and why of projects, code modules, class modules, user forms & references etc). I have found plenty on VB but few on VBA. Can you recommend a book/s that provides many examples/tutorials

The difference between ThisDocument and ActiveDocument still has me confused. If a project can only contain one document I would expect that ThisDocument is always the ActiveDocument. I did a few experiments with two documents open and the code you wrote that displays message boxes detailing the names of ThisDocument and ActiveDocument always displayed the name of the same document. What would be an example where running this code would produce two message boxes showing ThisDocument and ActiveDocument as different files

You defined Document_Open() as an Event that is related to the document. I am still unsure why this code cannot be contained within a module. Do modules also not contain code that define events related to the document

It seems to me that a Project and a Document are the same thing, but I have a feeling I am wrong on this point.

Thanks for your patience. Feel free to cut me loose at any time as I am possibly becoming a pest with these questions.