Reply
Thread Tools Display Modes
#1
Old 09-14-2009, 09:36 PM
Guest
Join Date: Apr 2007
Posts: 6,562
VBA coding help - How do I say "If there is nothing selected"

I was able to build the code that I asked for in this thread. Turns out, I can just paste the webpage into Word and run the macro there. Now I'm hitting a snag. I have a document that's a mix of tables and text. Part of the document goes something like
Quote:
Paint Price: $32
Color: Red
Amount: 6 gallons
I want to highlight "Red" and copy it. Part of the macro I've built goes

Quote:
Selection.find.text "Color:"
Selection.find.execute
Selection.moveright count:=2
Selection.endkey extend:=wdExtend
Selection. Moveleft extend:=wdExtend
Selection.copy
This gets my cursor into the right position, then highlights what I need, then backs up a character so I don't copy the return at the end of the line. This leaves me with the color. Usually, it's highlighting one word. But sometimes the line is blank so it ends up copying nothing because Endkey takes the highlight one character to the right, then then MoveLeft undoes that. This gives me an error that terminates the macro. So I tried inserting an If....Then where it types out a message for me to copy, a la:
Quote:
Selection.find.text "Color:"
Selection.find.execute
Selection.moveright count:=2
Selection.endkey extend:=wdExtend
Selection. Moveleft extend:=wdExtend
If IsNull(Selection.Text) = True Then
Selection.typetext "No Data Given"
Selection.HomeKey Extend:=wdExtend
End If
Selection.copy
But that doesn't work. If I hover over Selection.Text, it says it's equal to two squares (which I don't know how to render on the Dope). So maybe I have to use = ""? I've tried changing it to all of the following, none of which work:

If Selection.Text <> 0 Then
If Selection <> 0 Then
If Selection is Null Then
If Selection = "" Then
and a bunch of other combinations like setting the selection to a variable, then trying that in the above statements.

So what's the right way to say "If there is nothing selected, then write a sentence and highlight that"? I don't want the return on the end of the line unless I absolutely have to, because I need to paste the result elsewhere sans the extra return, so please try to avoid getting rid of the "MoveLeft" line.
#2
Old 09-15-2009, 09:43 AM
Guest
Join Date: Mar 2003
Location: Sydney, Australia
Posts: 453
I don't know VBA but I know VBScript, I'd suggest testing if Selection = Nothing and if that doesn't work then does selection.text have a length? If so you can try testing length = 0
#3
Old 09-15-2009, 10:59 AM
Guest
Join Date: Apr 2005
Location: Over Macho Grande
Posts: 1,994
Quote:
Originally Posted by Chessic Sense View Post
I was able to build the code that I asked for in this thread. Turns out, I can just paste the webpage into Word and run the macro there. Now I'm hitting a snag. I have a document that's a mix of tables and text. Part of the document goes something like


I want to highlight "Red" and copy it. Part of the macro I've built goes



This gets my cursor into the right position, then highlights what I need, then backs up a character so I don't copy the return at the end of the line. This leaves me with the color. Usually, it's highlighting one word. But sometimes the line is blank so it ends up copying nothing because Endkey takes the highlight one character to the right, then then MoveLeft undoes that. This gives me an error that terminates the macro. So I tried inserting an If....Then where it types out a message for me to copy, a la:


But that doesn't work. If I hover over Selection.Text, it says it's equal to two squares (which I don't know how to render on the Dope). So maybe I have to use = ""? I've tried changing it to all of the following, none of which work:

If Selection.Text <> 0 Then
If Selection <> 0 Then
If Selection is Null Then
If Selection = "" Then
and a bunch of other combinations like setting the selection to a variable, then trying that in the above statements.

So what's the right way to say "If there is nothing selected, then write a sentence and highlight that"? I don't want the return on the end of the line unless I absolutely have to, because I need to paste the result elsewhere sans the extra return, so please try to avoid getting rid of the "MoveLeft" line.
The first thing I would try is the text length suggested by mecaenas. Then I would try putting the "If' statement before the copy. If there is nothing there, don't even select or copy it. The third thing would be to have a default color value, like "none" in my table, so the color field would never be blank.

ETA: Have you tried Selection.value? Can't remember if that's valid or not.

Last edited by Don't Call Me Shirley; 09-15-2009 at 11:00 AM.
#4
Old 09-15-2009, 11:18 AM
Guest
Join Date: Apr 2007
Posts: 6,562
So what's the syntax for the length = 0 and selection = nothing ideas? I'll give them a shot.

I can't make a default in the table because these tables were written a long time ago in a country far far away, and there are thousands upon thousands of them. The idea of the code was to write in something as a default when I came across a blank one.
#5
Old 09-15-2009, 11:33 AM
Guest
Join Date: Apr 2005
Location: Over Macho Grande
Posts: 1,994
This is what I would do. Disclaimer- I am totally self-taught and usually end up doing things the really, really hard way.

I would create a userform with a textbox and paste the result into that textbox. Then you can use "If textbox1.value= "" then..." command. Make the userform invisible.
#6
Old 09-15-2009, 11:48 AM
Guest
Join Date: Apr 2007
Posts: 6,562
I've tried the len(Selection) idea, but it says that the length is 2, because Selection = [] [] (my best rendering of those two squares. If someone could tell me how to make those squares, I could just say "If Selection = [][]" and be done with it.
#7
Old 09-15-2009, 12:00 PM
Just Lovely and Delicious
Charter Member
Join Date: Aug 2001
Location: Northeast Ohio
Posts: 23,752
The problem is that those squares are something. As you've found, the value is not "" nor is it null and nor is its length 0.

You're going to have to dig to see what the squares represent - they could be the tab character, the VbCrLf (line break) character, the space character, or anything else ASCII 0-32.

Check out this tutorial: http://dreamincode.net/forums/showtopic12952.htm It shows you how to figure out what sort of characters are in a string. It might give you some insight to make some code that checks to see if the characters are NOT letters or numbers, then selection = blank. Or, google "vb characters" to find some more info that can help you.
#8
Old 07-22-2015, 12:29 PM
Guest
Join Date: Jul 2015
Posts: 2
Sorry for the mega-necropost, but after three years of writing stupid VBA macros, I finally stumbled upon the answer to OP's question. I figure if I can spare anyone else the frustration of dealing with Word's unintuitive Selection object, it's worth it.

Code:
 If Word.Selection.Type = wdSelectionIP Then
 MsgBox "Nothing is selected"
 Exit Sub
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump


All times are GMT -5. The time now is 06:17 PM.

Copyright © 2017
Best Topics: blue eyed asians catnip highs riders edge class wild west soundtracks jane eyre pronunciation tosh.0 racist sweet caroline movie 3 stooges themes emotional dreams mechanic horror stories pirate languages baby seal club freres jacques translation tramp means comrades in russian doctor plasma ezekial 23 20 quienes mas macho hydrostatic mowers dog anal swelling ebco water fountain fig newton commercials drow sexuality garment dyed books in portuguese simpsons font buffalo sucks autria godfrey hot t balling weechat windows sexy german names coke can girth iron vs bronze dominoes game strategy locking liquor storage can i use priority mail box for media mail schedule 40 vs schedule 80 pipe did ancient greeks have last names what does money smell like what do fraternity names mean best way to find a hole in an air mattress cost of honeybaked ham swallow piece of plastic fork cost of a bag of rice american frozen foods complaints kitchenaid dishwasher not cleaning top shelf the load out song tangle free telephone cords empty light bulb socket cover bee sting symptoms a week later kittens breath smells like poop what does how's tricks mean how much do shooting ranges cost april fools jokes for school teachers how long does dulcolax stay in your system 2 person kayak weight limit how long do cigarettes stay fresh does amazon sell used items chest hurts from coughing so much my cat wants to go outside is it ok to sweat out a fever whoa oh oh oh song female singer what do you put in a flask dying ain't much of a living boy pizza left out overnight still good what does the 06 13 mean on a guinness glass 1999 colorized silver eagle