2

I am writing a macro which would keep only the initials of all the words in a document by keeping all the spaces between these words and keeping the special characters of the text.

The code is as follows

Sub rewrite_document_with_Initials()
Set myRange = ActiveDocument.Range
Dim w As String

For Each aWord In myRange.Words
w = aWord
If IsAlphabet(w) = True Then

 aWord.Select
Set A = Selection.Range
A = Left(aWord, 1)
Selection.TypeText A & " "
Else
Debug.Print aWord

 End If
 
Next aWord
End Sub
Function IsAlphabet(inpChar As String) As Boolean
    Dim chkChar As String
    
    'Convert the character to Uppercase.
    'So that there is no need to do a check for Lower and Uppercase seperately.
    chkChar = UCase(inpChar)
    
    'Check whether input character is Alphabet or not
    IsAlphabet = Asc(chkChar) > 64 And Asc(chkChar) < 91
End Function

in above code it illiterate through each word in document and check if it is contains alphabets

-if yes then it replace it with first letter.

But the problem is, it stuck at the first word don't know if I am making correct range or string?

if there is shortest method of doing so please suggest!

VBAbyMBA
  • 806
  • 2
  • 12
  • 30
  • You can use regular expressions I guess. – Naresh Jul 09 '21 at 08:23
  • Please share more details. Thanks – VBAbyMBA Jul 09 '21 at 08:35
  • Refer [this question](https://stackoverflow.com/questions/46863457/how-to-select-first-letter-of-each-word-of-a-string-using-regex) – Naresh Jul 09 '21 at 09:08
  • Your `IsAlphabet` checks the first letter of the word. So `a1234` would be alphabet, and so will be `b$@!`. Did you intend to keep those words completely untouched? – GSerg Jul 09 '21 at 09:37
  • @GSerg No I only need alphabets, thanks for pointing it out – VBAbyMBA Jul 09 '21 at 09:56
  • 2
    Those are kind of conflicting requirements, you should strictly define what a "word" means for you. E.g. in the text `my address is ibn@ashiq.com` Word sees words `my`, `address`, `is`, `ibn`, `ashiq`, `com`, whereas you probably want to interpret `ibn@ashiq.com` as a single word. You could say the word boundary is the space then, but then punctuation marks will be included in words when not required too. – GSerg Jul 09 '21 at 11:03

1 Answers1

2

Try this procedure with regular expressions. Instead of each word, it loops through each paragraph and hence, it will work faster.

Sub rewrite_document_with_Initials()
Dim regex As Object
Dim mColl As Object

Set regex = CreateObject("VBScript.RegExp")        
regex.Global = True
regex.IgnoreCase = True
regex.Pattern = "\B[A-Za-z]+"
'I missed special characters earlier
For i = 1 To ThisDocument.Paragraphs.Count
    ThisDocument.Paragraphs(i).Range.Text = _
        regex.Replace(ThisDocument.Paragraphs(i).Range.Text, "")
Next i
End Sub

enter image description here

Naresh
  • 2,984
  • 2
  • 9
  • 15
  • No.. You don't need to reference anything. Adding image of my references. Also just noted... If there is a special character between word then that special character and letter after is kept and not replaced – Naresh Jul 09 '21 at 10:01
  • Referred [this question](https://stackoverflow.com/questions/4790375/regular-expression-match-all-but-first-letter-in-each-word-in-sentence) .. Whatever characters you don't want in the final output, you can throw them in the brackets keeping minus (-) sign last in the brackets. If you don't want numbers, then mention them in brackets after a-z as 0-9 – Naresh Jul 09 '21 at 10:09
  • 1
    Dear Naresh I think thisDcoument is not working here. I have changed `ThisDocument` to `ActiveDocument` then its working. can you explain it please. thanks for providing me such a great solution – VBAbyMBA Jul 10 '21 at 05:17
  • Thisdocument is the document in which this macro module resides. Activedocument is any document that is currently selected. You can record an macro in one document which becomes "thisdocument" for that macro... and you can use that macro for other document which is selected by replacing "thisdocument" with "activedocument" .. I didn't know you wanted it for other document. But be careful. When you say activedocument and run the macro it will run on any doc selected and the changes made by macro cannot be undone. If you don't want the changes, only option then is to close the doc without saving – Naresh Jul 10 '21 at 05:32
  • 1
    Thanks again. My document also contain fe images and when I run the macro it delete those images. Is there a way to not do anything to those images? – VBAbyMBA Jul 10 '21 at 07:03
  • 1
    Yes, I tested it. No idea why images are deleted. – Naresh Jul 10 '21 at 08:47
  • I think this is because we are looping through each paragraph. if we do it with each word may be get it right – VBAbyMBA Jul 10 '21 at 08:50
  • This can make a good question.. You can post a new question and give reference of this question asking why images are deleted with regex in microsoft word. – Naresh Jul 10 '21 at 09:04
  • Just last question then I will accept it. Do i need to change anything on MAC OS because it is not working on Mac? Anything special to enable. On mac? – VBAbyMBA Jul 11 '21 at 14:05
  • It seems "VBScript.regexp" doesn't work with MAC ..[Refer this question](https://stackoverflow.com/questions/60157447/regular-expression-library-missing-in-excel-vba-for-mac) and [this also](https://stackoverflow.com/questions/27344932/regex-with-excel-vba-on-mac) – Naresh Jul 11 '21 at 17:17