Word adding spaces where they are not wanted!!

D

DannyE

Hi Guys,

I thought I was doing quite well in learning VBA but this one has m
beaten for now... I select a group of paragraphs that have manuall
entered numbering which I want to get rid of and replace with prope
styled num formatting. There are empty paragraphs in the selection which i wan
to remove as it fouls up the paragraph numbering, and the style i a
applying can take care of the spacing however... when i cycle throug
the selection.paragraphs collection(I hope my terminology isn'
squiffy but i am new to objects and collections...), I can detect th
empty paragraphs and remove them via:

Dim para As Paragraph

For Each para In Selection.Paragraphs
If para.Range.Text = Chr(13) Then
para.Range.Delete
ElseIf para.Range.Text = Chr(10) Then
para.Range.Delete
End If
Next para

Which works fine, it removes the empty paragraphs. However if th
next paragraph begins with a bracket (which most of them do), Wor
adds a space before the opening bracket. Not a big deal you woul
think but appanrently Word thinks it is...

Following this process I then proceed to take out the numbering whic
because is inconsistent I can't take out by case example so in eac
paragraph I do a do while loop which contains if statements to remov
characters that are not in the range A-Z and a-z. However on findin
a space before the bracket, it refuses to delete it. code i
something like this: (from memory I'm afraid as i'm running my lapto
in linux at the moment and can't swap to windows)

the paragraphs have varying number of characters before the main tex
start which has lead me to this solution...

Dim para As Paragraph
Dim selStart As Integer
Dim selEnd As Integer
Dim charVal As Integer
Dim remRes As Integer

selStart = Selection.Start
selEnd = Selection.End

For Each para In Selection.Paragraphs
remRes = 0
'more than 1 char to remove in each para so:
Do While remRes = 0
'if character is outside of the 'letter ranges, delete it
charVal = AscW(para.Range.Characters.First)
If charVal < 65 Or charVal > 122 Then
para.Range.Characters.First.Delete
'loses selection so recalculate and select again
selEnd = selEnd - 1 'as 1 char has been removed
Selection.End = selEnd
ElseIf charVal > 90 And charVal < 97 Then
para.Range.Characters.First.Delete
'loses selection so recalculate and select again
selEnd = selEnd - 1 'as 1 char has been removed
Selection.End = selEnd
Else 'char is a letter and should be kept
remRes = 1
End If
Loop
Next para

Unfortunately when it encounters a space at the beginning of th
sentance before a bracket, it refuses to delete it. I have trie
changing the space to another character and then deleting that, bu
the space reappears. Also if i take out the first space manually an
then run the process it runs fine up until the point where it finds
spaces before the start of the proper paragraph text, then, instea
of just deleting the 'First' space as I specify in the code, i
decides to remove both of the remaining spaces which then messes u
my re-select method.

I'm just really confused how something which should be so simple i
thwarting me. I expect there is a setting somewhere to stop Wor
doing this but I can't find it to switch it off :(

Any help/ideas you can give me on this would be very very gratefull
recieved, hopefully while I still have some hair to tear out :(

Many thanks in advance,

Da
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top