Consulting

Results 1 to 5 of 5

Thread: Macro to remove range of unicode characters

  1. #1

    Macro to remove range of unicode characters

    Hi,
    How can I make a macro that will remove from a selection all the characters in a unicode range?
    For example, if I want to remove from a selection all the characters from unicode u01000 to unicode u01100?

    Thank you very much

  2. #2
    I will make it easier:

    A simple replacement macro is like this:

    Sub Replacement()
    ' Replacement Macro
    Selection.Find.ClearFormatting
        Selection.Find.Replacement.ClearFormatting
        With Selection.Find
            .Text = "^u01000"
            .Replacement.Text = ""
            .Forward = True
            .Wrap = wdFindStop
                End With
        Selection.Find.Execute Replace:=wdReplaceAll
    End Sub
    Now, how can I change it to replace all the unicode values from ^u01000 to ^u01100?

    Maybe it could be done by creating a variable "n" with a value 01000 and after wdReplaceAll n=n+1, and adding a loop do while u<01101 . But how should I modify .Text = "^u01000" ? if I write "^u0n" I suppose it will not understand that n means my variable.
    Thank you
    Last edited by Aussiebear; 08-01-2022 at 01:08 PM. Reason: Added code tags to supplied code

  3. #3
    I found the solution by myself!

    Sub Replacement()
    ' Replacement Macro
    Sub Replacement()
    Selection.Find.ClearFormatting
    Selection.Find.Replacement.ClearFormatting
    Dim n As Integer
    n = 1000
    Do Until n > 1100
    With Selection.Find
    .Text = ChrW(n)
    .Replacement.Text = ""
    .Forward = True
    .Wrap = wdFindStop
    End With
    Selection.Find.Execute Replace:=wdReplaceAll
    n = n + 1
    Loop
    End Sub
    Last edited by Aussiebear; 08-01-2022 at 01:07 PM. Reason: Added code tags to supplied code

  4. #4
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    Rather more efficient:
    Sub Demo()
    Application.ScreenUpdating = False
    With ActiveDocument.Range.Find
      .ClearFormatting
      .Replacement.ClearFormatting
      .Format = False
      .Forward = True
      .Wrap = wdFindStop
      .MatchWildcards = True
      .Text = "[" & ChrW(&H1000) & "-" & ChrW(&H109F) & "]"
      .Replacement.Text = ""
      .Execute Replace:=wdReplaceAll
    End With
    Application.ScreenUpdating = True
    End Sub
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  5. #5
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,702
    Location
    Quote Originally Posted by Simple View Post
    I found the solution by myself!
    Best way to learn
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •