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

    Selection in VBA (Word 2003)

    <img src=/S/question.gif border=0 alt=question width=15 height=15> I want a macro I am working on to select a word. The VBA statement

    Selection.Expand wdWord

    will select the word, but it also includes the following space, which I don't want. I have been unable to figure out how to write the statement to select a word without the following space but also select the word if it is followed by punctuation. For example, I would like the macro to select only the letters "hand" in the following two examples:

    hand-carved
    hand carved

    Ultimately, I want to create a VBA macro that will work similar to Word's Add to Dictionary feature except that it will bring up dialog box that allows me to choose which dictionary to add the word to. Until I can figure out how to write the whole macro in VBA, I am doing a patch job with some things in VBA and some in Macro Express. Currently, thanks to the help you have given, I have added to the context-sensitive menus Add to Custom Dictionary which activates a VBA macro that calls the Macro Express macro to copy the selected word, bring up a menu listing my custom dictionaries from which I can choose the dictionary to add the word to, opens the dictionary and pastes the word, saves the file, and reloads the dictionaries.

    What I haven't been able to do is place my cursor in the word, right-click to select Add to Custom Dictionary and have the word where my cursor is selected, copied, and if Word indicates it is a misspelled word, choose Ignore All, before proceeding to call the Macro Express macro. Thus my question about selecting just the characters of the word. Right now, I have to first click Ignore All and then I have to manually select the word/characters to be copied before right-clicking and selecting Add to Custom Dictionary.

    Thanks for your help.

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

    Re: Selection in VBA (Word 2003)

    Does this do what you want?
    <code>
    Selection.Expand Unit:=wdWord
    If Selection.Characters(Selection.Characters.Count) = " " Then
    Selection.MoveEnd Unit:=wdCharacter, Count:=-1
    End If
    </code>
    It explicitly checks if the last character in the selection is a space, and if so moves the end of the selection one character to the left.

  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: Selection in VBA (Word 2003)

    <img src=/S/thankyou.gif border=0 alt=thankyou width=40 height=15> Again, thank you, Hans. It works perfectly. Now that I see the solution, I wonder why I had such trouble coming up with it. Believe it or not, I spent several hours trying to figure it out. <img src=/S/stupidme.gif border=0 alt=stupidme width=30 height=30> Eventually I'll get the hang of this. I appreciate everyone's patience with my struggles.

  4. #4
    3 Star Lounger
    Join Date
    Apr 2004
    Location
    Boston, Massachusetts, USA
    Posts
    389
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Selection in VBA (Word 2003)

    Hi Rich,

    As usual, Hans is spot on with his solution. I also thought I'd post the following utility function, which collapses both end of the current selection, de-selecting both leading and trailing spaces,as well as paragraph marks (helpful when you select a word at the end of a paragraph). I use it quite a bit throughout some of my templates.

    HTH,

    <pre>Function ChompSelection(ByRef sel As Selection)
    If sel.Range.Characters.Count = 1 Then Exit Function
    sel.MoveStartWhile cset:=(Chr$(32) & Chr$(13)), Count:=sel.Characters.Count
    sel.MoveEndWhile cset:=(Chr$(32) & Chr$(13)), Count:=-sel.Characters.Count
    End Function
    </pre>


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

    Re: Selection in VBA (Word 2003)

    <img src=/S/thankyou.gif border=0 alt=thankyou width=40 height=15> Thank you, Andrew.

    I bought a copy of Word Hacks last April. There are several things in it that I want to try, especially #65, if I ever get some time. As you can tell, writing macros is not my forte but I have been forced to do it for my small business in my "spare" time because we haven't been able to find someone interested in writing them for us.

    I'm still struggling with this dictionary macro -- I don't want to tell you what I have spent the last 7 hours trying to do, but the impetus for my latest struggle is that the code Hans provided to reload the dictionaries, while it works, it doesn't cause Word to do away with the squiggly line under the added word unless you right-click on each word. I am trying to create a workaround. My idea, which hasn't worked so far, is to have Word do a forward-only F&R of the word. The sequence of events would be (1) select the word (2) add the word to the custom dictionary (3) do a F&R. Of course, the most logical thing would be to have the macro (1) select the word (2) activate Ignore All (3) add the word to the custom dictionary.

    The more logical course was what I worked on for hours yesterday and part of today before I finally gave up on the idea and turned to the F&R solution (which still isn't working correctly but on which I have made some progress). I gave up on the Ignore All solution because I can't figure out how to activate Ignore All via the macro. I can't find a command for it anywhere, although there is a ResetIgnoreAll method and there are Ignore methods for such things as uppercase.

    I'll conquer this macro yet!! Thanks again for your help.

  6. #6
    Silver Lounger
    Join Date
    Jan 2001
    Location
    West Long Branch, New Jersey, USA
    Posts
    1,921
    Thanks
    6
    Thanked 9 Times in 7 Posts

    Re: Selection in VBA (Word 2003)

    Rich,

    I read thru this thread but didn't see enough to let me know what the problems are or to know if the following is of any value. For example, you say that your sequence would start with selecting a word. What word? The first one you come to might not be misspelled. So let me assume you've gone past some words until you come to the first misspelled one. Then you're going to add the "selected" (and presumably misspelled) word to one of several custom dictionaries. Once you've added the word to the custom dictionary, you could do "Ignore All." Although I see this in the thread, I'm not sure if you're doing that in your "idea." Once you ignore the first occurrence of a misspelled word, the rest are ignored and all red squiggles under that same word, no matter where in the document (except maybe headers/footers and text boxes) are turned off. So as you keep stepping forward thru the document, you should not find a word that is misspelled if such a misspelling already occurred.

    Now I understand you want to turn off the red squiggles. As noted above, it should be happening. However, if it isn't, it's not clear you have to search for the misspelled words to turn off the squiggles. For example, if I select a paragraph and then choose Tools | Language | Set Language (in 2003) and check the box that says "Do not check spelling or grammar", then red squiggles are removed. I'm not even on the word and it takes care of the entire paragraph. In fact, you can select the entire document and then check the "Do not..." box (in code of course, if possible). Of course, the down side is that if you later add words that are misspelled, they won't be flagged.

    So don't know if this helps at all, but thought I'd mention it in case it does.

    Fred

  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: Selection in VBA (Word 2003)

    Hello Fred. I apologize for the confusion. Let me try to clarify.

    By select a word, I do mean select the word that is flagged by Word as misspelled that I want to add to a custom dictionary of my choosing. What currently happens is this. If I first right-click and choose Ignore All, all of the red squiggles disappear as they should. I can then run my macro, select the word, and add it to a custom dictionary. But this involves 2 right-clicks: one to Ignore All and then a second to select and add the word to a dictionary.

    If, instead, I first run the macro to add the word to a dictionary, I can no longer choose Ignore All because Word recognizes the word as correctly spelled. But the red squiggles do not disappear under any instance of the word unless I right-click on each instance or save and close the document and reopen it. I don't have to do anything more than right-click on the word, but I have to do it each time I come across the word. I do not want to turn off all the red squiggles, I simply want to find a way to run the macro and as part of the macro have the red squiggles that appear under the word I have added to a dictionary disappear, just as it would if I chose Ignore All or chose Add to Dictionary from the standard Word right-click menu.

  8. #8
    Silver Lounger
    Join Date
    Jan 2001
    Location
    West Long Branch, New Jersey, USA
    Posts
    1,921
    Thanks
    6
    Thanked 9 Times in 7 Posts

    Re: Selection in VBA (Word 2003)

    Rich,

    I'm still somewhat confused. I do understand that if one were doing this manually, one would right-click on a word that was misspelled, click add to dictionary, and then have to right-click again to have the word ignored so the red squiggles dissappear. I guess what you're looking for is a way to do everything at one shot.

    With the macro, does it matter if the red squiggles are turned off at the time you add the word to the dictionary? Suppose the macro steps thru the document and adds all the misspelled words to the dictionary of choice. Then, as the last thing your macro does, select the entire document and check the "Do not check spelling or grammar" checkbox as described in my last post. That gets rid of all the red squiggles (regardless of whether anything was added to the dictionary) with the downside that any additional text is not checked.

    Fred

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

    Re: Selection in VBA (Word 2003)

    Fred,

    It matters because of what I do. As a book editor, I work on manuscript with individual chapters that range in size from (approximately) 2500 words (10 pages) to 75,000 words (300 pages) or more. Most of the books I work on run 10,000+ manuscript pages (1 manuscript page = 250 words).

    The idea of adding all misspelled words to the dictionaries and then turning off spellchcecking is appealing but impractical. The manuscripts are technical and replete with words that are not readily found in standard dictionaries, and often require investigation to determine if they are correct. It would be impossible to go through a document and simply add what spellcheck has identified as a misspelled word to a custom dictionary before doing the editing. And because of the size and complexity of the manuscripts, I cannot work with spellchecker off for fear of missing something. I just need to find a way to either activate Ignore All before adding a word to a dictionary or to effectively refresh the dictionaries to eliminate the red squiggle under the newly added word.

  10. #10
    Silver Lounger
    Join Date
    Jan 2001
    Location
    West Long Branch, New Jersey, USA
    Posts
    1,921
    Thanks
    6
    Thanked 9 Times in 7 Posts

    Re: Selection in VBA (Word 2003)

    Rich,

    A few other thoughts come to mind if they're of any use:
    - firstly, on my Word 2003 system, when I add a word to the main dictionary, the red squiggle under that occurrence AND all other occurrences of the same word are removed. Sounds like your system may work differently. Or perhaps its the fact that you're adding the word to a custom dictionary makes a difference. I never work with custom dictionaries.
    - if you turned off "check spelling as you type", you don't get any red squiggles at all. You can then check the spelling by using the Tools | Spelling and Grammar menu. If you make a selection, Word will go thru all the words in the selection and show you in a dialog box which words are misspelled. You can then add to the dictionary or ignore. If you Ignore All, Word will NOT remove the red squiggle under a word NOT in the selection. However, when Word is finished checking your selection, it will ask you if you want to check the remainder of the document. If you answer yes, it will continue checking the rest of the document. HOWEVER, it will skip a misspelled word previously flagged as ignore all (per the original selection) AND remove the red squiggle as it cycles thru the remainder of the document - that is, it skips words previously thought to be misspelled but no longer are by virtue of having been told to Ignore All.

    Rereading your original post and your last response to me, it sounds like what you're doing should work. If you can select the word, copy it to the clipboard for use in a dialog for adding the word to a selectable custom dictionary and then reload the dictionary, doesn't that then remove all further occurrences of the red squiggle under that word. In my limited experiment, it didn't remove the red squiggle unless I started a new document (equivalent to reloading the dictionary?).

    Fred

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

    Re: Selection in VBA (Word 2003)

    Fred,

    I, too, am running Word 2003 and it works just as yours does IF I add the word to Word's default custom dictionary (Custom.dic). The problem occurs when adding the word to one of my custom dictionaries.

    You would think that reloading the dictionary would solve the problem, but it doesn't. The red squiggles remain unless I open a new document or save, close, and reopen the current document.

    I appreciate the suggestion about turning off check spelling as you type and the running spellchecker separately, but that is not feasible for my work. I simply need to find a way to get Word to work as I want it to <img src=/S/smile.gif border=0 alt=smile width=15 height=15> .

  12. #12
    Silver Lounger
    Join Date
    Jan 2001
    Location
    West Long Branch, New Jersey, USA
    Posts
    1,921
    Thanks
    6
    Thanked 9 Times in 7 Posts

    Re: Selection in VBA (Word 2003)

    Ok Rich, one more suggestion. I was toying with this when I wrote my last post but wasn't sure about whether it would work. I think it does but the ultimate test is coding it and seeing what happens. I assume you're running some macro to have Word step thru the words and it stops at a misspelled word that is underlined with red squiggles. There may be other occurrences of the word in the document.

    1. You've just stopped at a misspelled word but you don't know if it's going to be added to a custom dictionary.
    2. by right-clicking on the word, the checker can select your Add to Custom Dictionary selection if that's the desire. If not, Word goes on to the next misspelled word.
    3. in the macro I'm assuming is associated with you Add to Custom Dictionary,
    - copy the current word to the clipboard (which I think you're doing to use that as part of a message to ask to what custom dictionary the word should be added to)
    - do whatever is needed to add the word to the selected dictionary
    - assuming the checker does not Cancel out of adding the word to some custom dictionary (so the word is now in a selected dictionary), do the following :
    --- paste the word into your document as the next word after the current one (creating a double word)
    --- have the macro find the next misspelled word, which should be the 2nd of the double words
    --- click Ignore All or however you'd do that in a macro
    --- delete the previous word, which should be the 1st of the double words

    It sounds like a hair-brained scheme but it might work.

    I also created one of my own dictionaries to try the above thru the user interface. It seems that if I change the default to another dictionary (in my case, the "fred" dictionary that I created just for this purpose), then adding the word to the selected (and now current default) dictionary also has the result of turning off the red squiggles under all occurrences of the word. That not only applies to the CUSTOM.DIC custom dictionary that Word uses, but whatever dictionary is currently the default. So rather than the above, can't you just reset the default custom dictionary to the one selected to add the word to (I certainly was going back and forth using the Word menu interface to do this) and then just use the Add to Dictionary command (which will add it to the current default custom dictionary, which is the one selected) resulting in turning off all red squiggles for that word?

    Anyway, I'm at the end of my ideas - at least for now. I recall a friend of mine playing around in VBA with multiple dictionaries. I think he was doing something similar to what you're trying to do but it was a few years ago. I could see if I can dig up some contact info and maybe give him a hollar if useful.

    Fred

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

    Re: Selection in VBA (Word 2003)

    Fred,

    I tried your suggestion of making one of my custom dictionaries the default dictionary. (I have 6 custom dictionaries plus the standard Word created Custom.dic.) I tried two things: First, I made one of the 6 the default dictionary and then I added the misspelled word to a different custom dictionary using my macro. The red squiggles didn't disappear. Second, leaving the same dictionary as the default dictionary, I added the misspelled word to the default custom dictionary using Word's Add to Dictionary command rather than my macro; now the squiggles disappear. There must be something about Word's built-in Add to Dictionary command that activates the Ignore All as it adds a word to the default dictionary that I can't duplicate in my macro.

    As for your pasting suggestion, I run into the same problem that I currently have: I can't get the macro to activate the Ignore All -- I still need to do it "manually." Consequently, it is just as easy to first "manually" choose Ignore All and then run my macro. What I would like to do is figure out code to automate the entire procedure so that when I run the macro, the macro would (1) choose Ignore All, then (2) select the word, and after selecting the word, (3) run the rest of the macro to add the word to the dictionary of my choosing.

    I do appreciate your help and your suggestions and will gladly consider and try any others.

  14. #14
    Silver Lounger
    Join Date
    Jan 2001
    Location
    West Long Branch, New Jersey, USA
    Posts
    1,921
    Thanks
    6
    Thanked 9 Times in 7 Posts

    Re: Selection in VBA (Word 2003)

    Rich,

    Based on what you found, which seems to help a little, I do have another suggestion. In your macro, see if you can invoke Word's Add to Dictionary command or maybe even Word's Ignore All command. These commands are buried deep down in the menu structure so it may not be possible. On the other hand, it might. For menu commands like File | Save As, people sometimes write their own macro called "SaveAs" (something mimicing the Word menu command name) which, because of the duplication of names between the user's version and Word's version, results in the user's version being run (I guess MS planned it that way). I once wrote a macro called UpdateField (or FieldUpdate) which I didn't realize at the time was a duplicate of Word's Update Field command and mine was used. Couldn't figure out why F9 was not updating the selected field for a few days and then the bulb lit up. Anyway, if there is a way for you to write your own macro (kind of like what you did with your "Add to Custom Dictionary" and invoke the Word commands that you need, that might help; someone else on the board might be able to help with a list of "overrideable" commands. I looked in Word's command list (accessible thru the Tools | Customize in the Commands tab and the "All" category in the left list). Didn't see anything that seemed to deal with dictionaries but did see an IgnoreAllConsistenceError - whatever that is.

    Fred

  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: Selection in VBA (Word 2003)

    Fred,

    Again, thanks. I have been trying to do what you suggest but I can't seem to find a way to access Ignore All with a macro and I can't find any VBA language that will activate it. If I should stumble on a solution, I'll pass it on, but I think it will take someone with significantly more knowledge of VBA than I have to figure it out.

Posting Permissions

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