My Little Pony, sorry, my first little macro

User avatar
LineLaline
2StarLounger
Posts: 194
Joined: 19 Sep 2022, 16:51

My Little Pony, sorry, my first little macro

Post by LineLaline »

Well, I did it!
I recorded a macro and it works. I did it for the current doc only out of precaution.
I hit 'Record' and typed, then hit my 'add image now please' shortcut and placed an image, continued to type, made the text bold, stopped recording.
Then I ran the macro And It Worked.

As I checked things out I noticed I could summon up the code, and found other macros; one of which is causing the problems with the continuous page numbers. I had no idea but this might be a start to debugging this.

The fact that I can see the code makes me curious; I could tweak that. I am intrigued. But I also have a lot of other things to do so I will first do those because if I dive into this now, I'm lost :laugh:
Ceci n'est pas une signature.

User avatar
HansV
Administrator
Posts: 78545
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: My Little Pony, sorry, my first little macro

Post by HansV »

Congratulations! You've already learned a lot!
Best wishes,
Hans

User avatar
ChrisGreaves
PlutoniumLounger
Posts: 15641
Joined: 24 Jan 2010, 23:23
Location: brings.slot.perky

Re: My Little Pony, sorry, my first little macro

Post by ChrisGreaves »

LineLaline wrote:
27 Dec 2022, 12:18
Well, I did it!
Well congratulations! :clapping: I hope that you turn out better at writing VBA code than you are at bear-baiting :evilgrin:
... found other macros; one of which is causing the problems with the continuous page numbers. I had no idea but this might be a start to debugging this.
Might I humbly suggest that, given your limitations of time (I speak as one who is waiting for the microwave oven to heat up someone else's Christmas dinner leftovers) you might consider pasting a single existing macro, as-is, into a MSWord document and then adding comments to it?
Use your own language to describe briefly what each line of code does.(1)
... I could tweak that. I am intrigued. But I also have a lot of other things to do so I will first do those because if I dive into this now, I'm lost :
I agree that you could tweak macros, and that is a good exercise, but rather than upset the apple-cart on Day Two, looking at, reading, and documenting never did any one any harm.

:yep: :trophy: :thinks: :sing:
Chris
(1)I would be happy to read your comments, comment on your comments, and then you could comment on my comments on your commented code, which might lead us into recursion; you never know!
He who plants a seed, plants life.

User avatar
Jay Freedman
Microsoft MVP
Posts: 1320
Joined: 24 May 2013, 15:33
Location: Warminster, PA

Re: My Little Pony, sorry, my first little macro

Post by Jay Freedman »

LineLaline wrote:
27 Dec 2022, 12:18
But I also have a lot of other things to do so I will first do those because if I dive into this now, I'm lost :laugh:
That is also a valuable insight. I dove into Word macros sometime back in the 1990s, and have yet to surface. :rofl:

User avatar
LineLaline
2StarLounger
Posts: 194
Joined: 19 Sep 2022, 16:51

Re: My Little Pony, sorry, my first little macro

Post by LineLaline »

HansV wrote:
27 Dec 2022, 12:33
Congratulations! You've already learned a lot!
Thank you Hans 😊
Ceci n'est pas une signature.

User avatar
LineLaline
2StarLounger
Posts: 194
Joined: 19 Sep 2022, 16:51

Re: My Little Pony, sorry, my first little macro

Post by LineLaline »

ChrisGreaves wrote:
27 Dec 2022, 14:36
Well congratulations! :clapping: I hope that you turn out better at writing VBA code than you are at bear-baiting :evilgrin:
Oh, the bear! Well, it is sunny and Winter starts when the light and warmth are coming back, I am sure he will come out soon :laugh:

More tomorrow because typing with one ginger on the phone slows me down even though I was just told I could not post so soon after the last one. Two fingers outdid the server. Quite the feat.
Ceci n'est pas une signature.


User avatar
macropod
4StarLounger
Posts: 508
Joined: 17 Dec 2010, 03:14

Re: My Little Pony, sorry, my first little macro

Post by macropod »

LineLaline wrote:
27 Dec 2022, 12:18
I recorded a macro and it works. I did it for the current doc only out of precaution.
I hit 'Record' and typed, then hit my 'add image now please' shortcut and placed an image, continued to type, made the text bold, stopped recording.
Then I ran the macro And It Worked.
Do be aware that the macro recorder is about as smart as a box of rocks. Yes, the code may work, but it'll likely be terribly verbose and inefficient - and it's hopeless at anything that might benefit from loops and processing anything other than Selections and keystrokes.
Paul Edstein
[Fmr MS MVP - Word]

User avatar
ChrisGreaves
PlutoniumLounger
Posts: 15641
Joined: 24 Jan 2010, 23:23
Location: brings.slot.perky

Re: My Little Pony, sorry, my first little macro

Post by ChrisGreaves »

LineLaline wrote:
27 Dec 2022, 12:18
I recorded a macro and it works. ...

As macropod says, be aware. Here is an example of a problem with a recorded macro: Recording macro changes font colour but does not record the code.
My primary use of recorded macros it to do a look-see on what sort of instructions might be involved in a task.

Cheers, Chris
He who plants a seed, plants life.

User avatar
LineLaline
2StarLounger
Posts: 194
Joined: 19 Sep 2022, 16:51

Re: My Little Pony, sorry, my first little macro

Post by LineLaline »

Ceci n'est pas une signature.

User avatar
LineLaline
2StarLounger
Posts: 194
Joined: 19 Sep 2022, 16:51

Re: My Little Pony, sorry, my first little macro

Post by LineLaline »

ChrisGreaves wrote:
28 Dec 2022, 13:55
As macropod says, be aware. Here is an example of a problem with a recorded macro: Recording macro changes font colour but does not record the code.
My primary use of recorded macros it to do a look-see on what sort of instructions might be involved in a task.
I created 'Actions' and 'Scripts' in different types of software. I remember my father and brother's frowns and scary words: "DO NOT TOUCH anything Macro related!" and I was too afraid at the time to even look at it, then never thought I needed it because I was too busy with other things.
But this macro recording was fun and was a surprise since it even recorded keystrokes and menu items. So, I wonder if maybe you wanted to change the colour with 'something that could not be recorded'. In some software for instance, going to a menu item was a 'mouse movement' which could not be part of the action. Maybe that was the reason? I will read the post in more detail soon; when it was written I was not yet erm, into macros.
Ceci n'est pas une signature.

User avatar
ChrisGreaves
PlutoniumLounger
Posts: 15641
Joined: 24 Jan 2010, 23:23
Location: brings.slot.perky

Re: My Little Pony, sorry, my first little macro

Post by ChrisGreaves »

LineLaline wrote:
28 Dec 2022, 19:57
... So, I wonder if maybe you wanted to change the colour with 'something that could not be recorded'.
Quite so. :clapping:
But if you select a chunk of text in MSWord and record a macro to change the colour of the font, that will work.
I was recording a macro to "Edit, Replace All" to change the colour of text, a subtly different action. That is what failed.

In both cases my aim was "to change font colour", but in the exercise described a month ago, the explicit aim was "to change font colour USING AN EDIT-REPLACEALL COMMAND"

Tricky, huh?
Anyway, welcome to the swamp ... :frog:
Cheers, Chris
He who plants a seed, plants life.

User avatar
LineLaline
2StarLounger
Posts: 194
Joined: 19 Sep 2022, 16:51

Re: My Little Pony, sorry, my first little macro

Post by LineLaline »

Oh dear, a swamp...
What have I stumbled into now!
Ceci n'est pas une signature.

User avatar
HansV
Administrator
Posts: 78545
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: My Little Pony, sorry, my first little macro

Post by HansV »

Don't worry. While Paul (macropod) is correct that the macro recorder doesn't generate sophisticated code, and sometimes doesn't record at all, it is still useful to get a feel for what macros do and look like. You can worry about advanced stuff such as that mentioned by Chris (much) later.
Best wishes,
Hans

User avatar
ChrisGreaves
PlutoniumLounger
Posts: 15641
Joined: 24 Jan 2010, 23:23
Location: brings.slot.perky

Re: My Little Pony, sorry, my first little macro

Post by ChrisGreaves »

HansV wrote:
28 Dec 2022, 20:37
... it is still useful to get a feel for what macros do and look like.
Agreed wholeheartedly! :yep:
Cheers, Chris
He who plants a seed, plants life.

User avatar
DocAElstein
5StarLounger
Posts: 602
Joined: 18 Jan 2022, 15:59
Location: Re-routing rivers, in Hof, Beautiful Bavaria

Re: My Little Pony, sorry, my first little macro

Post by DocAElstein »

Hello

I think perhaps the way to think about the macro recorder is that it can be one of a few useful tools when writing and learning VBA.
Important perhaps to remember that it’s not really writing a program, although its workings have been tweaked a bit to give you a final, often working, program. That what I just said is perhaps is debatable, open to opinion. But what I think everyone would agree is that the main thing its doing is recording what you are doing manually and giving you as best it can the VBA to do that. Mostly the only code writing it does is add the start and stop bit. The rest is just recording what you do, or rather what was done after you typed stuff in order to make it happen.
I am less familiar with Word, but in Excel, when you do stuff manually you have to see and select cells which usually results when you run the macro recorder in a lot of activating and selecting stuff coming out in the final given thing. Those activating and selecting stuff code bits VBA does not usually need to do in order to do stuff, and generally those bits slow coding down unnecessarily.
Also, if I am not mistaken, the recorder will just chuck out what you do in the application you run it from. This is very restrictive considering the vast amount of cross application/ platform stuff you can do with VBA. Knowing about the cross platform possibilities with VBA might be something that would encourage some people to learn it sooner. You would not notice that from what the recorder gives you.

When you write code you are usually best to think of the best way to get it to do the final thing you want to do, and not necessarily to think of the coding to do exactly what you would do manually in order to do it. Only occasionally with very simple things those two things would be the same.

Personally I think VBA itself is very well worth learning even if you are just a novice in Excel. I have less experience with Word. I only started a few years back with Office to help speed up a personal project. I followed Excel and Office courses and so did not get into VBA until a while later. I was very frustrated then later when I discovered it, and really wish I had learnt a bit of it from day one. That is based on my experience with Excel. It might be a bit different with Word. I don't know. I note you are looking at word here.

Just my opinion: I don’t agree with what people often say that the macro recorder can be a good start point in learning VBA, at least not from my experience with Excel VBA: I think if I was teaching Excel for beginners then in the very first lesson I would teach some VBA. I would probably as very first thing talk about writing stuff manually in a cell using the keyboard, things like words and formulas. Second part of the same very first lesson would be to write a simple macro to do the same – put stuff in a cell: A few lessons later I would introduce the macro recorder as just one of the many tools available in VBA. But that’s just my opinion, please don’t hate me for it.

Some very experienced and knowledgeable professional VBA programmer’s often still use the macro recorder. Often they do that just so as to quickly and conveniently get the correct syntax for doing something they don’t do often. I don’t think any sane person would want to learn and remember all the syntax there is in VBA, and the macro recorder can be one quick way to get the syntax you need to do something when you are in the middle of writing some coding .

Alan
I seriously don’t ever try to annoy. Maybe I am just the kid that missed being told about the King’s new magic suit, :(

User avatar
Charles Kenyon
5StarLounger
Posts: 625
Joined: 10 Jan 2016, 15:56
Location: Madison, Wisconsin

Re: My Little Pony, sorry, my first little macro

Post by Charles Kenyon »

Alan,

This is an excellent contribution. I think the macro recorder is a good way to make a quick and dirty macro to do something in Word. Often it will work. For learning vba, it can be useful just to give a framework in which to write macros.

I got my start on Word macros and vba with Word 97 Annoyances and Writing Word Macros. As with your experience in Excel, recorded macros in Word use the selection method which is very inefficient and problematic. There are many things that can be done using vba that cannot be recorded. Jay Freedman wrote an excellent article on modifying recorded macros. It is one of the ones on the MVP FAQ page I linked earlier. https://wordmvp.com/FAQs/MacrosVBA/Modi ... dMacro.htm. It was written back in the day of usenet but is still very valid.

User avatar
DocAElstein
5StarLounger
Posts: 602
Joined: 18 Jan 2022, 15:59
Location: Re-routing rivers, in Hof, Beautiful Bavaria

Re: Word macro recorder

Post by DocAElstein »

Hi,
Charles Kenyon wrote:
29 Dec 2022, 20:12
As with your experience in Excel, recorded macros in Word use the selection method which is very inefficient and problematic. ...
That’s interesting to know, thanks.
One thing that has always confused me with the word macro recorder is that it won’t let me select individual things, such as parts of text, or words or pages, etc. when its running, ( I did notice I can use the keys Ctrl+a to select everything).
I had heard that a different team was responsible for developing word and Excel so thought maybe things might be different in some aspects. Not being able to select individual things as I can in Excel when running the Excel recorder feels rather strange and quite different to someone like me having done a lot with the excel recorder, but not much at all in Word.
( I rarely use Word and when I have needed something that I found difficult to figure out, then you or Hans or Paul could show me how to do it, so I have not needed to spend too much time on Word yet. (I do have a wildcard book you recommended and that will happily keep me occupied if I have time to think about word. I am generally keen to get better at wild card things to cure some strange phobia I have with them – my brain seems to crash when I try to think of those things, I just need time to get all the syntax clear I think.) )
Alan
I seriously don’t ever try to annoy. Maybe I am just the kid that missed being told about the King’s new magic suit, :(

User avatar
Jay Freedman
Microsoft MVP
Posts: 1320
Joined: 24 May 2013, 15:33
Location: Warminster, PA

Re: My Little Pony, sorry, my first little macro

Post by Jay Freedman »

Charles Kenyon wrote:
29 Dec 2022, 20:12
As with your experience in Excel, recorded macros in Word use the selection method which is very inefficient and problematic. There are many things that can be done using vba that cannot be recorded. Jay Freedman wrote an excellent article on modifying recorded macros. It is one of the ones on the MVP FAQ page I linked earlier. https://wordmvp.com/FAQs/MacrosVBA/Modi ... dMacro.htm. It was written back in the day of usenet but is still very valid.
Yeah, I've been doing VBA since antedeluvian times, and not much has changed -- especially with the recorder.

There are several tools in the VBA editor that are underused and often helpful.
  • The object browser (F2) lets you type something in the search box (next to the binoculars icon) and hit Enter, and it lists everything in the object model (classes, objects, properties, methods, enumerations, etc.) whose names contain what you typed -- including those where it isn't at the beginning of the name. Scrolling down that list can be incredibly enlightening. Sometimes you know there's a thing you need to deal with, but you can't remember its name, or you got the parts of the name backwards, or whatever. Oh, there it is!
  • In the editor's menus, Debug > Compile will point out any syntax errors in your code. Unfortunately, it can't predict runtime errors such as the dreaded "The requested member of the collection does not exist". Related to this, go to Tools > Options and check the box for "Require variable declaration". See https://wordmvp.com/FAQs/MacrosVBA/DeclareVariables.htm for an explanation.
  • The Immediate window lets you execute a single statement (or several if you separate them with colons) to see what happens. Use the Print keyword (or just a question mark) at the start of the line to have the result printed in the window (if the statement produces a value). Inside a macro, use the Debug.Print command to do the same, with the result also in the Immediate window. And if you stop a macro in the middle of executing, you can get values of the macro's variables in the window.
  • Running a macro one statement at a time, with the Debug > Step Into (F8) command, lets you watch what's happening both in the code and in the document as each statement runs. You can set breakpoints in the code (F9) and then Run (F5) and the code will stop just before the breakpointed statement executes; that way you can let a lot of code execute invisibly and then stop at the "interesting" place.
  • Most of the time you can click on the name of an object or one of its properties in your code and press F1 to open the documentation page for that thing in your browser. Sometimes that page is actually helpful. Sometimes it isn't. :scratch:

User avatar
ChrisGreaves
PlutoniumLounger
Posts: 15641
Joined: 24 Jan 2010, 23:23
Location: brings.slot.perky

Re: My Little Pony, sorry, my first little macro

Post by ChrisGreaves »

LineLaline wrote:
28 Dec 2022, 20:27
Oh dear, a swamp... What have I stumbled into now!
You will have stumbled into a swamp if you just make lots of Procedures ("Subroutines" and "Functions") and have them stored in your templates, or documents, or workbooks all over the place.
Early on in both Word and Excel I started a utility library (UW.dot for Word and UW.xls for Excel) and into those put, well, Utility functions.

My UW.dot holds functions that can be used in a variety of applications; the template sits in my Word startup folder "T:\Greaves\Startup\Word\".

How to know what goes into a utility library? Easy. Here's an example. Suppose in writing Application_A I write a function "strGetFileData" whose purpose is to Get Data from a File. I write the function within Application_A, test it, and it works. Months later I write Application_B and realize that it too needs such a function. I move the function strGetFileData from Application_A to my UW.dot. Now both Application_A and Application_B can make use of the function strGetFileData from the library template UW.dot.

The library cuts down development time drastically because you don't have to write and test two versions of strGetFileData and if strGetFileData needs to be modified, you don't have to re-test it in two places.
I have attached a list of 894 utility procedures in my UW.dot

The other trick I use is to write self-testing functions, and I learned that from a book fifty years ago.

Cheers, Chris
You do not have the required permissions to view the files attached to this post.
He who plants a seed, plants life.