#1
|
|||
|
|||
Conditional tied to trying to find a " " space character
Hey, I'm trying to write MS word VBA code to find a space to the left of the cursor. If the space is found, go to right of the space, otherwise, type a space. I've also attached an image to try to be more specific.
My code assumes I start to the left of the required location. I then select the text one character to the left. I search this text for a " ". However, I can't get it to work correctly. Could anybody please help me get my code, shown below, sorted? Code:
Sub IfSpaceFound() Selection.MoveLeft Unit:=wdCharacter, Count:=1, Extend:=wdExtend With Selection.Find .ClearFormatting .Text = " " .Forward = True .Wrap = False .Format = True .MatchCase = False .MatchWholeWord = False .MatchWildcards = True .MatchSoundsLike = False .MatchAllWordForms = False .Execute If .Found Selection.MoveRight Unit:=wdCharacter, Count:=1 Else Selection.TypeText Text:=" " End If End With End Sub |
#2
|
||||
|
||||
Try:
Code:
Sub IfSpaceFound() With Selection.Characters.First If .Start = ActiveDocument.Range.Start Then .InsertBefore " " .MoveStart wdCharacter, 1 ElseIf .Previous <> " " Then .InsertBefore " " .MoveStart wdCharacter, 1 End If End With End Sub
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#3
|
|||
|
|||
Why are you trying to do this? Surely it would be easier to do a search and replace on the document e.g. .^p for . ^p.
If you want to do it programatically then your code will work if you change to count:= -1. So if you are in the no space case in your thumbnail, the count:= -1 will select to the left of the cursor. if you use count:=1 then you select to the right of the cursor. |
#4
|
||||
|
||||
Slaycock,
The limitation of your approach is that it will only work at paragraph ends and even then only of the paragraph break is preceded by a period. It won't work in any other situation. The code I posted will work anywhere in a document - including at the very start, where your '-1' suggestion would cause an error.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
the character "v" when typed acts like ctrl-v. | jim redfield | Word | 1 | 09-22-2012 05:19 AM |
Wierd symbols inplace of "space", "indentation" etc | aka.bhagvanji | Word | 5 | 02-16-2012 11:50 AM |
Conditional formatting - "From" - Why does it recognise only names and not emails | ghumdinger | Outlook | 0 | 09-18-2011 01:28 AM |
The heading doesn't "obey" the "before" space! why? | Jamal NUMAN | Word | 1 | 07-06-2011 04:25 AM |