VBA coding help - How do I say "If there is nothing selected"

I was able to build the code that I asked for in thisthread. 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.

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

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.

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.

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.

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.

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: Situs Slot Gacor Gampang Menang Hari Ini Maxwin Slot Online Terpercaya 2023 > Slot88 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.

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.


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

:smack: