Page 1 of 3 123 LastLast
Results 1 to 15 of 31
  1. #1
    2 Star Lounger
    Join Date
    Sep 2005
    Location
    Poughkeepsie, New York, USA
    Posts
    123
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Interface to a Word VBA macro (Word 2003 SP2)

    I have written several VBA "toggle" macros that enable the user to place the cursor in a word and have the word replaced by a different term. For example, if the user's cursor is in "that" and the activating key is pressed, "that" is changed to "which". The attached is a portion of the code for one of the toggle macros. There are several toggle macros because each focuses on particular types of changes.

    This macro is used by several of us as we edit book manuscripts. (We are not on a central server; each of us works out of our home on standalone computers.) Unfortunately, I am the only person who has any clue about VBA (albeit what I know is very limited). Consequently, when someone comes across something they want to add to the macro, and this occurs with great frequency, they tell me what it is, I add it to the appropriate macro, and I then redistribute the template to everyone with a list of the newest additions..

    What I want to do is create a macro that opens a dialog box in which the enduser enters the word to be changed and what it is to be changed to (both case sensitive), and indicates to which macro it is to be added by checkbox (that is, the macros would be listed and the enduser would select one). When the enduser clicks OK, this macro would then add the appropriate code to the checkmarked toggle macro. I have tried to create such a macro but with my limited knowledge of VBA have been wholly unsuccessful. So my question: Is what I want to do possible? If it is, any help would be appreciated.

    Thanks.
    Attached Files Attached Files

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Interface to a Word VBA macro (Word 2003 SP2)

    The code does not seem to deal in straight replacement (for example, it applies superscript in some situations). I don't see how you could take that into account in automated macro creation.

  3. #3
    2 Star Lounger
    Join Date
    Sep 2005
    Location
    Poughkeepsie, New York, USA
    Posts
    123
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Interface to a Word VBA macro (Word 2003 SP2)

    I am not concerned about the "aberrations" such as the superscripting because that occurs infrequently. If an interface can be done that would just handle the straight text, that would be perfect. The usual entries have no superscripting or subscripting, which would be the only variations from straight text in any case. What I really want is for the enduser to have a box that says, e.g.,

    Change [word] of [number] characters
    To [word]

    Add to: [checkbox] General Words
    [checkbox] Medical Acronyms
    [checkbox] [other macro choices]

    Add Cancel

    and then have automatically added to the checked macro and in the correct place the following code with the information filled in:

    ElseIf target = "[change word]" Then
    ActiveDocument.Range(Start:=aaa, End:=aaa + [number of characters]).Delete
    Selection.TypeText Text:="[to word]"

    Of ocurse, it would be even better if the enduser didn't have to provide the character count, that the macro could provide it automatically.

    Is this interface possible?

  4. #4
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Interface to a Word VBA macro (Word 2003 SP2)

    Wouldn't it be much easier to use AutoText for this? No code needed at all, all users can add their own preferences.

  5. #5
    Lounger
    Join Date
    May 2003
    Location
    San Diego, California, USA
    Posts
    34
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Interface to a Word VBA macro (Word 2003 SP2)

    Not sure, but the macro described Here might do want you want.

    When you run the macro, it goes through the entire Word document and if it finds a word on the list, replaces it with the replacement word.
    For example, it finds centimeter, it will replace it with cm.

    I use a modified version that keeps the words for replacement in a separate text file.

  6. #6
    2 Star Lounger
    Join Date
    Sep 2005
    Location
    Poughkeepsie, New York, USA
    Posts
    123
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Interface to a Word VBA macro (Word 2003 SP2)

    Hans, autotext doesn't accomplish the same thing with the ease of the macro. In addition there are usability limitations. Consider the following. In the sample code I provided, if the editor comes to a sentence that reads, for example,

    "the distance between the patient's eyes was 2.5 centimeters"

    to change "centimeters" to "cm", the editor simply puts her cursor somewhere in the word "centimeters" and presses the activation key (which is a single key) . With AutoText, the editor has to (a) select the word to be replaced, ([img]/forums/images/smilies/cool.gif[/img] open the AutoText menu, find the correct main menu, (d) find the correct submenu, perhaps having to go through several, and (e) select the correct replacement and click it. As the number of entries increases, the time it takes to find the replacement text increases.

    Also, consider the variations. Some combinations require multiple variations. For example, the "which" and "that" combination has 4 possibilities: that > which; which > that; That > Which; and Which > That. The "principal" and "principle" combination has 8 possibilities principal > principle; Principal > Principle; principals > principles; Principals > Principles; principle > principal; Principle > Principal; principles > principals; and Principles > Principals. As you can see, the list can grow. Consequently, it is much quicker to press a single key than to use AutoText.

  7. #7
    2 Star Lounger
    Join Date
    Sep 2005
    Location
    Poughkeepsie, New York, USA
    Posts
    123
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Interface to a Word VBA macro (Word 2003 SP2)

    Thanks for the suggestion, NeuroDoc, but the macro you suggest is essentially a find and replace macro. The macro would work great if we were correcting misspellings or something that doesn't require making a decision based on context. The toggle macros are based on grammar decisions made in context. For example, if it were always incorrect to use "principal" because in every instance "principle" was meant, the macro you suggest would work fine. But consider this:

    "The principal's principles were immoral and the assistant principal did nothing to correct those principles."

    Any change has to be decided in context -- the context not only of the current sentence, but of the preceding and succeeding sentence.

  8. #8
    Lounger
    Join Date
    May 2003
    Location
    San Diego, California, USA
    Posts
    34
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Interface to a Word VBA macro (Word 2003 SP2)

    <P ID="edit" class=small>(Edited by NeuroDoc on 15-Jan-06 04:06. Example Geaphic added.)</P>
    <hr>the macro you suggest is essentially a find and replace macro. <hr>
    Correct, but the macro allows you to choose whether you make the change or not. It also can check for phrases, rather than single words.

    One thing that might work for your multiple users, is using a text file and fso to store the changes. Each user could simply add the word to the text file.

    I have a second macro that lets a user select a word or phrase, which is then copied to a dialog box. The user then can add the corrected phrase to the text box. Pressing the command button adds the combination to the text file, which is used the next time the correction macro is run.

    I run the correction macro on my dictations. Then word in question appears in a userform, which offers the possible correction. It wouldn't be hard to have a list open with several possible choices.

    Another idea would be to use an exclusion dictionary. That way word principal would show up as an error during spell check and you could select the correct choice from the spell check list. See Post 19407
    Attached Images Attached Images

  9. #9
    Lounger
    Join Date
    May 2003
    Location
    San Diego, California, USA
    Posts
    34
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Interface to a Word VBA macro (Word 2003 SP2)

    Basically the F&R macro is used to clean up dictations done by Speech Recognition. In this context it works very well and i see some utility for editing. Basically it is a supersized spell check for phrases. The option to always correct is nice, for example it will fix "lumber puncture' to "lumbar puncture" or "1 month" to "one month" without a prompt. It however won't change "lumber jack".

    No problem sharing the code. I don't have it on this laptop but will try to send it later today. If I get time, I want to package the whole thing into a Word Template.

    It basically uses the Word find and replace function, so it can find any word or phrase and can be case sensitive. The find list is simply a text file so there is no limitation on the number of entries. If you add a word/phrase to the find list, there is no need to restart Word.

    I want to add some more options, for example enable some wildcard characters in the find list. See Also Here. Thiscould really work for your purpose by including some decision making.

    For some reason, I couldn't get the wildcard searches to work when run from the VBA code. I think that there are some bugs in Word that cause a problem with this Maybe More Here?.

  10. #10
    2 Star Lounger
    Join Date
    Sep 2005
    Location
    Poughkeepsie, New York, USA
    Posts
    123
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Interface to a Word VBA macro (Word 2003 SP2)

    The more I think about your two macros, NeuroDoc, the more intrigued I am. I think your suggestion would be useful, but for a different task than the toggle macros. I know that it seems as if a F&R macro should work, but it doesn't -- we have tried that in the past, although not with option to change or not change.

    Would you be willing to share with me the code you use for both the macro and the add-to-list dialog box? When a word is added to the list via your dialog box, will the word/phrase be included in the search the next time the macro is run or do you have to exit and restart Word? Can your macro deal with punctuation? For example, could I enter J. Cardiovasc. Surg. as the find phrase or microbally-derived or are there limitations as to how the find phrase is constructed? Is it case sensitive? Is there a limit as to the number of words or phrases that can be in the Find list?

  11. #11
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Interface to a Word VBA macro (Word 2003 SP2)

    There have been posts in the past concerning self-modifying macros; they seemed quite messy because the tools available and security restrictions complicate the process.

    Have you considered an external data store, such as a .txt, .ini, .mdb or .xml file, to store your search strings? You could create a global index when the document/template is loaded and then work from that copy. Then you would only need to distribute a new macro when the nature of the search itself changed (somewhat like the division of labor in antivirus programs between scanning engines and detection signatures). Of course, to avoid overwriting the user's own idiosyncratic searches, you actually would want to split the data into two stores (like the Microsoft dictionary and user's Custom dictionary).

  12. #12
    2 Star Lounger
    Join Date
    Sep 2005
    Location
    Poughkeepsie, New York, USA
    Posts
    123
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Interface to a Word VBA macro (Word 2003 SP2)

    Thanks for the suggestion, but it does not apply to these particular macros -- at least I don't think it does. There seems to be a continuing misunderstanding about the macros for which I want the interface: they are not search (or find and replace) macros. The macros are used as the editor reads the manuscript and are designed to facilitate making editing changes based on context, by which I mean that the editor has to consider context and style in deciding whether a particular term is properly used.

    Consider as an example the words "since" and "because". You cannot do a find and replace to change every instance of "since" to "because" because the determination of which is correct depends on its use in context. They both have a causal sense, so in common English they are used interchangeably to convey a causal connection ("since the virus produces symptoms", "because the virus produces symptoms"), but grammatically there is a distinction and "since" is used to convey a sense of time ("since he turned 18 he has lived alone") and "because" to convey a causal connection ("because he turned 18 he could legally drink alcohol"). Consider the difference in meaning that occurs when replacing "since" with "because" in this example: "because he turned 18 he has lived alone". Which does the author mean: the cause of his living alone was that he turned 18 or he has lived alone from the time he turned?

    My point is that the toggle macros serve a different purpose than a search macro and cannot (should not) be used in the same manner. It has been suggested that a find and replace macro that asks each time whether to make a change would work, and it will for some things, but not for what the toggle macros are intended to do. Additionally, if you had to run through a list of 50 terms in a 125-page document answering yes or no regarding replacement, the edting time would increase dramatically and the number of errors introduced would increase.

    OTOH, if there is a way to adapt the suggestion of having the terms in a .txt file rather than as part of the VBA macro yet essentially work the same (i.e., as a toggle rather than as a search and replace), I am open to that; in fact, I think that would probably work better, especially because words and phrases could easily be added to a .txt file. But I admit that I don't know how to write the toggle macro so that the user could place her cursor in a word or could select a phrase and have the macro compare the word/selection against the contents of the .txt file and if found, replace the word/selection with an alternative. This warrants further investigation.

  13. #13
    Super Moderator
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    3,852
    Thanks
    4
    Thanked 259 Times in 239 Posts

    Re: Interface to a Word VBA macro (Word 2003 SP2)

    I think Jefferson's suggestion is workable for the purpose you require but I haven't yet seen anyone mention the thesaurus function of Word. Have you investigated the thesaurus already and discarded it for a reason? I would have expected it to provide the functionality you require especially if you were able to customise the entries within or create your own.
    Andrew Lockton, Chrysalis Design, Melbourne Australia

  14. #14
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Interface to a Word VBA macro (Word 2003 SP2)

    In the attached zip file, you'll find two documents:
    Code.doc contains a macro, and some sample text.
    Replacements.doc contains a table with two columns. the left hand column contains phrases to be replaced, the right hand column their replacements. As you can see, some formatting is possible.
    Place both documents in your default document folder. Open Code.doc (make sure you enable macros), click in a word such as "because" or select a phrase such as "cubic centimeters". Then run the macro "MyReplace". The macro will open Replacements.doc and check for a match. If found, the selection will be replaced. The document will be left open. The user can simply switch to it and edit it.
    If you want to put Replacements.doc in another folder, you must specify the path in the MyReplace macro.

    I haven't tested extensively, there are probably some wrinkles that need to be ironed out, but I hope this gives you an idea.
    Attached Files Attached Files

  15. #15
    2 Star Lounger
    Join Date
    Sep 2005
    Location
    Poughkeepsie, New York, USA
    Posts
    123
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Interface to a Word VBA macro (Word 2003 SP2)

    Thanks, Hans.

    I will use this code as a starting point. I tried it and it works as you said. I appreciate the help. This will give something to do over the next few weeks.

Page 1 of 3 123 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •