Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 10-03-2013, 10:16 AM
Dart82 Dart82 is offline Conditional tied to trying to find a " " space character Windows 7 64bit Conditional tied to trying to find a " " space character Office 2010 64bit
Novice
Conditional tied to trying to find a " " space character
 
Join Date: Oct 2013
Posts: 7
Dart82 is on a distinguished road
Default 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
Attached Images
File Type: png 2013-10-03_130946.png (2.7 KB, 28 views)
Reply With Quote
  #2  
Old 10-03-2013, 02:47 PM
macropod's Avatar
macropod macropod is offline Conditional tied to trying to find a " " space character Windows 7 32bit Conditional tied to trying to find a " " space character Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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]
Reply With Quote
  #3  
Old 10-04-2013, 08:13 AM
slaycock slaycock is offline Conditional tied to trying to find a &quot; &quot; space character Windows 7 64bit Conditional tied to trying to find a &quot; &quot; space character Office 2013
Expert
 
Join Date: Sep 2013
Posts: 256
slaycock is on a distinguished road
Default

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.
Reply With Quote
  #4  
Old 10-04-2013, 01:47 PM
macropod's Avatar
macropod macropod is offline Conditional tied to trying to find a &quot; &quot; space character Windows 7 32bit Conditional tied to trying to find a &quot; &quot; space character Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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]
Reply With Quote
Reply



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
Conditional tied to trying to find a &quot; &quot; space character The heading doesn't "obey" the "before" space! why? Jamal NUMAN Word 1 07-06-2011 04:25 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 11:45 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft