PDA

View Full Version : VBA code for spacing



machunt
05-27-2012, 12:10 PM
Hi,
I was wondering if there is a way to do a spacing expanded by 0.6 only in the characters but not in word throughout the word document where there is repeated characters like aa, bb, cc, ee etc.
thanks in advance.
Mac

macropod
05-27-2012, 10:52 PM
You could do it with a macro like:
Sub SpaceOut()
Application.ScreenUpdating = False
Dim Rng As Range, i As Long
For i = 97 To 122
Set Rng = ActiveDocument.Range
With Rng.Find
.ClearFormatting
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchWildcards = True
With .Replacement
.ClearFormatting
.Font.Spacing = 0.6
.Text = "^&"
End With
.Text = "[" & Chr(i) & "]{2,}"
.Execute Replace:=wdReplaceAll
.Text = "[" & UCase(Chr(i)) & "]{2,}"
.Execute Replace:=wdReplaceAll
End With
Next
Set Rng = Nothing
Application.ScreenUpdating = True
End Sub
Note that this works with both upper and lower case letters.

machunt
05-28-2012, 12:25 AM
Waooo... macropod, that's exactly what i wanted. Thanks mate.

I was also wondering is there a way so that the special characters and symbols will have double space after that, throughout the document.
example:
If there is :
Mr.Jack’s,

It will be converted to :
Mr. Jack’ s,

Please guide me, thanks in advance.
Mac

Paul_Hossler
05-28-2012, 04:49 AM
just out of courosity, why do you want to expand double letters and double up the spaces after special characters?

Paul

machunt
05-28-2012, 05:16 AM
Hi paul,
Thanks for your reply. I want it to make it more clear and as per the specification. Doing it manually is taking a lot of time and I am missing lots of gaps in preparing my final report. So I was wondering if I could run a macro, so that after I type in the full report, I could run the code and it will incorp the spaces.
Thanks in advance.

Paul_Hossler
05-28-2012, 12:07 PM
Actually I understood that part. I was wondering about the typographical reasons you wanted to insert a little extra space between doubled letters, and 2 spaces after some special characters.

I just never saw that requirement or specification before

Esp the part about

Mr.Jack’s

It will be converted to :

Mr. Jack’ s

A space after a possessive apostriphe seems a little unusual

Paul

macropod
05-28-2012, 03:50 PM
Indeed, it is odd. I very much doubt there is any such specification.

And yes, increasing the space after an apostrophe is quite easy. Simply insert the following code between 'Next' and 'Set Rng = Nothing':
Set Rng = ActiveDocument.Range
With Rng.Find
.Text = "[! ]'[! ]"
.Execute Replace:=wdReplaceAll
End With

Edit: refined code to not impact opening single quotes.

machunt
05-28-2012, 05:31 PM
Hi friends,thanks for your reply,
I can understand it is a bit weird but yes this is my specification for some report documentation. I used the above code but somehow it is not reflecting the update.Am I missing something ? please guide me, as I also require double space after some specific characters like
' . ; : " —
Thanks in advance.
Mac

macropod
05-28-2012, 07:22 PM
Hi Mac,

Sorry, the additional code I posted only increases the character spacing for the apostrophe by the same amount as for the other characters. If you put double-spaces between apostropes and the letters that follow, you'll generate a lot of spelling errors. Try the following instead - it increases the character spacing for nearly everything that isn't a letter:

Set Rng = ActiveDocument.Range
With Rng.Find
.ClearFormatting
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchWildcards = True
With .Replacement
.Font.Spacing = 6
.Text = "^&"
End With
.Text = "?[\!\""\#\$\%\&\'\(\)\*\+\,\-\.\/\:\;\<\=\>" & _
"\?\@\[\\\]\^^\_\`\{\|\}\~\€\‚\ƒ\„\…\†\‡\ˆ\‰" & _
"\‹\‘\’\“\”\•\–\—\™\›\¡\¢\£\¤\¥\¦\§\¨\©\ª\«" & _
"\¬\­\®\¯\°\±\²\³\´\·\¸\¹\º\»\¼\½\¾\¿\÷]{1,}"
.Execute Replace:=wdReplaceAll
End With
If there are characters in the Find string that you specifically don't want to process, simply delete them and the preceding '\'.

fumei
05-28-2012, 09:20 PM
"I can understand it is a bit weird but yes this is my specification for some report documentation."

This is not an answer. It is telling us what we already know (and therefore do not need an answer for): this is what you are wanting to do. However, the question was why.

You have not actually answered WHY you need this very odd spacing. Just because you are feeling personally cranky or perverse? Why? It is NOT a normal typographical specification. In 40 years of technical documentation, I have never heard of such a spacing after an apostrophe. And most certainly have never been asked to do such a thing.

macropod
05-28-2012, 09:28 PM
Not only is there no such typographical specification, it also reduces a document's readability.

Mac might like the look of it, but more consideration should be given to the expectaion of whoever else is going to read to the document. I am quite sure the type foundries that design the fonts most of us use today employ specialists to advise them on all aspects of character spacing.

Be that as it may, I've provided the code to achieve what Mac asked for. It's up to him now to use it (or not) wisely.

machunt
05-28-2012, 09:53 PM
Hi Friends,
The word documents are basically uploaded to a separate software for preservation. There is some strange errors is I upload it without double space. It is not only with asostropies but there are only these types of special character that come up
' . ; : " —
As such I just need a code that will just insert double space after these characters. Spelling mistakes is not a concern as presently I manually insert the double space. Please help me.
Thanks in advance.

macropod
05-28-2012, 11:13 PM
The word documents are basically uploaded to a separate software for preservation. There is some strange errors is I upload it without double space.
What kinds of errors? That suggests whatever software you're using has bugs in it - the kind that change the content of a file and I don't see how changing the character spacing will fix that. That is a serious bug and, if it's doing that, it could be changing more than you've recognised so far. You should fix the problem, not just play around with the symptoms.

Spelling mistakes is not a concern as presently I manually insert the double space. Please help me.
Have you at least tried the code from my last post? It increases the spacing without the risk of spelling errors.

fumei
05-29-2012, 01:03 AM
Sorry if I sound crabby. It is just curiosity as to why you think this is needed. macropod's code should do what you say you need. As he asks - have you tried it? If it does not do what you think is needed, state what the problem seems to be.

That being said, and given that macropod's code works for you, it is still an extremely strange thing. Any software that makes a mess of Word documents as you describe does not sound very good at "preservation". It is not preserving them, it is screwing them up. I would be very leery of trusting something that is that buggy.

In fact, you STILL have not actually answered the why, because you still have not actually stated what actually happens.

"There is some strange errors is I upload it without double space."

OK. But "strange errors" does not say what really happens to your documents. It is possible (but who knows yet) that there may be some other solution. What happens without the double spaces?

The word is deleted?
The word is bolded?
The word changes to a different font?
The characters are reversed?

What are the strange errors?