VBA Word Find and Replace characters but excluding certain characters

I am trying to write VBA code in Word that I will eventually run from a VBA Excel module. The aim of the project is to find specific strings in the open Word document that have length of either one or two characters and are of a certain format, and replace
them with other strings in the same format. This is to do with transposing (i.e. changing the musical key) of chord symbols in a songsheet in Word. The Find and Replace strings are contained in ranges in an Excel workbook, which is why I want to eventually
run the code from Excel. I'm much more experienced in writing VBA code in Excel than in Word, and I'm fairly confident with transferring the 'Word VBA' code into an Excel module.
At the moment I'm trying out code entirely in Word, and I've come across a stumbling block. For example, I want it to Find "A" and replace with "B",
BUT only if the "A" is NOT followed by "#" (sharp) or "b" (flat).
Here is the code I've got in Word VBA, which I obtained by editing code produced by the recorder:
Sub F_R()
'Find text must have specific font
With Selection.Find.Font
.Bold = True
.Underline = wdUnderlineWords
.Superscript = False
.Subscript = False
End With
'Replacement text must have specific font
With Selection.Find.Replacement.Font
.Bold = True
.Underline = wdUnderlineWords
.Superscript = False
.Subscript = False
End With
'Find & Replace strings
With Selection.Find
.Text = "A" 'hard-coded here for testing, but this will
'eventually be referenced to a cell in Excel
.Replacement.Text = "B" 'hard-coded here for testing, but this will
'eventually be referenced to a cell in Excel
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = True
.MatchWholeWord = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
End Sub
For the Find & Replace section I want to do something like:
With Selection.Find
.Text = "A"
.Text <> "A#"
.Text <> "Ab"
.Replacement.Text = "B"
End With
- but this produces a syntax error, presumably because you can have only one .Text line (or it won't accept <>?)
I tried adopting the way of excluding chars when using the Like operator, and while it compiles, it will not replace
any "A":
With Selection.Find
.Text = "A[!b#]"
.Replacement.Text = "B"
End With
I suspect that I'm going to have to change tack completely in the way I'm doing this. Do you have any suggestions, please?
The chord names/symbols are preceded/succeeded by either spaces or paragraph returns and can look like these, for example (all Font Bold and Underlined words only):
C<sup>7</sup>
Dm<sup>7</sup>
Eb<sup>-5</sup>
Bb<sup>+11</sup>
F#m<sup>7</sup>
i.e. [ABCDEFG][b # | optional][m |optional][- + | superscript, optional][2 3
5 6 7 9 11 13 | superscript, optional]
The crux of my problem is that the note A should be treated as entirely distinct from Ab or A# (and similar for other flattened/sharpened notes).
Sorry for long post.

Hi Ian,
It is not easy to find Microsoft forums. However this forum is for the Visual Studio Net version. 
Try this forum for VBA.
https://social.msdn.microsoft.com/Forums/en-US/home?forum=isvvba
Success
Cor

Similar Messages

  • Microsoft Word - find and replace tips

    have a large file with coding and I will be doing this weekly. I want to replace all the text between symbols See example. And I don't want to just highlight and delete...there is too much text.
    This is the following text paragraph.
    <rb0><*kn0><*kt(A)><p(0, 0, 0, 8, 0, 0)><*L>NOTICE UNDER ASSUMED BUSINESS NAME ACT<\n>COUNTY OF SMITH<\n>TO WHOM IT MAY CONCERN:<\n>Notice is hereby given that on the 16TH DAY OF JANUARY 2008 a Certificate of Ownership of business was filed in the Office of the County Clerk of Smith County, stating that<\n>LISA Doe<\n>Intend to transact, or are transacting business in Smith County, under the fictitious name to wit: <\n>ONE-L GROUP<\n>And that is/are the sole Owner(s) and Proprietor(s) of said business; and that the principal place of said business is located <\#13><*rb(.15, 0, K, 100%, 0, 0, .015)><*p(0,0,0,8.5,0,0)>
    I need it to appear like the following
    NOTICE UNDER ASSUMED BUSINESS NAME ACT COUNTY OF SMITH TO WHOM IT MAY CONCERN: Notice is hereby given that on the 16TH DAY OF JANUARY 2008 a Certificate of Ownership of business was filed in the Office of the County Clerk of Smith County, stating that LISA DOE Intend to transact, or are transacting business in Smith County, under the fictitious name to wit: ONE-L GROUP And that is/are the sole Owner(s) and Proprietor(s) of said business; and that the principal place of said business is located.
    mmm

    Have you tried the "forms" function of M$ Word. You prepare the standard text & insert blank spaces where the name data is typed in.
     Cheers, Tom

  • How To: Find and replace multiple words in one Click ?

    I have an file with about 150 Pages where I want to replace a word about 500 time by one click.
    Find and Replace works but only with one word.
    How can I do it by one click ?

    You can't, not in Acrobat anyway.
    This is the kind of thing that should be done in the original file format,
    not in a PDF.

  • Superscripts in Find and Replace

    How can I enter superscripted text in the Replace box of the Find and Replace dialog without losing the superscript formatting when it pastes?
    Thanks.
    Dennis

    Thank you for the reply, Arnis.  I don't find that option in my Find/Replace dialog.  I've tried copying the superscripted text to the clipboard and pasting in the Replace box of the Find and Replace dialog, but it loses the superscripting, and pastes into the document without it.  I find I have to go into each occurrence individually and superscript each one.  I'm using Illustrator CS5....and I just realized I'm on the wrong forum.   Never mind.........

  • Finding and replacing (international??) characters

    I'm having trouble finding and replacing characters. Can anyone help me see the wood for the trees?
    The characters in question are single quote characters (I asume) which have been entered in a windows box/word program/filemaker progremme or some other. As such, when I open them in a flat file on my Mac I see the following:
    �A L O E�
    where the "�" and the "�" should actually be single quotes.
    On my Mac I can weed these out with the following:
    public String cleanString(String text)
         if(text != null && text.length() > 0)
              if(text.indexOf("�") > -1)
                   text = replace(text, "�", "'");
              if(text.indexOf("�") > -1)
                   text = replace(text, "�", "'");
              if(text.indexOf("�") > -1)
                   text = replace(text, "�", "'");
              if(text.indexOf("�") > -1)
                   text = replace(text, "�", "'");
         return text;
    }But on my server (Linux) this just isn't working. It doesn't find the characters. I've tried converting each character to unicode and looking for it that way.
    public String cleanString(String text)
         String cleaned = "";
         if(text != null && text.length() > 0)
              boolean flag = false;
              for (int i = 0; i < text.length(); i++)
                   int j = (int)text.charAt(i);
                   String u = Integer.toHexString(j);
                   if("221a".equals(u))
                        flag = true;
                        continue;
                   if(flag && "b4".equals(u) || "2260".equals(u) || "a8".equals(u) || "c6".equals(u))
                        cleaned += "'";
                        flag = false;
                   else
                        if(flag)
                             cleaned += text.charAt(i -1) + text.charAt(i);
                        else
                             cleaned += text.charAt(i);
                        flag = false;
         return cleaned;
    }But the same thing happens. It works on my mac, but not on my server.
    Part of my problem is that these 'offending characters' are actually comprised of two chars:
    eg:
    '\u221a' + '\u00b4'
    Can anyone point me in the right direction?

    As such, when I open them in a flat file on my Mac I see the following:First you should open the file using the correct character encoding. Since the file originates from a Windows box, the encoding is probably Windows-1252, aka CP1252. You'll find the code for opening and reading here, just change "UTF-8" to "CP1252."
    http://javaalmanac.com/egs/java.io/ReadFromUTF8.html
    Next, you should not write non-ASCII characters in source code because it is not portable. If the source code is moved from one system to another, like from Mac OS X to Linux, the way the compiler translates bytes to characters may change. Only ASCII is safe, so it is better to use the portable Unicode escapes (\uXXXX) to represent non-ASCII characters in strings.
    From Microsoft's own reference, the "smart" single quotes are U+2018 and U+2019: http://www.microsoft.com/globaldev/reference/sbcs/1252.mspx
    so the code to replace them would be
    text = text.replace('\u2018', '\'');
    text = text.replace('\u2019', '\'');You would need to do the same for the double quotes, \u201C and \u201D
    btw as there's a method for replacing charcters in the String class, you don't need to write a new one

  • Use VBA and Excel to open Dreamweaver HTML (CS5), find and replace text, save and close

    I wish to use VBA and Excel to programmatically open numbered Dreamweaver HTML (CS5) and find and replace text in the code view of these files, save and close them.
    I have  5000 associations between Find: x0001 and Replace: y0001 in an Excel sheet.
    I have the VBA written but do not know how to open, close and save the code view of the ####.html files. Please ... and thank you...
    [email protected]

    This is actually the code view of file ####.html that I wish to find and replace programmatically where #### is a four digit number cataloguing each painting.... In 1995 I thought this was clever... maybe not so clever now :>)) Thank you for whatever you can do Rob!
    !####.jpg!
    h2. "Name####"
    Oils on acrylic foundation commercial canvas - . xx X xx (inches) Started
    Back of the Painting In Progress </p> </body> </html>
    Warmest regards,
    Phil the Forecaster, http://philtheforecaster.blogspot.ca/ and http://phils-market.blogspot.ca/

  • How to use Microsoft Word's Find and Replace with HTML tags?

    Hello to all!
    I'm trying to figure out how to use the find and replace function in Word to replace html tags. I'd like to be able to change something like this:
    <span class="B01-K-ITAL">random text</span>
    To something like this:
    <em>random text</em>
    I want to replace the open and close tags without changing or interfering with the text between the tags. I'm pretty sure I should use wildcards, but I can't figure out how to use them properly.
    Anyone able to lend a hand?

    Here is my latest regular expression with Perl.   I think it matches the spirit of the request in the original post.
    Note, this isn't as easy as you think.  You need to code up the complete set of html rules in you implementation. You need to allow for a certain amount of mal-formed html.
    perl -0660pe 's^<[sS][pP][aA][nN]\s+class="B01-K-ITAL"\s*>(.*?)</[sS][pP][aA][nN]>^<em>$1</em>^gs' i.html >|o.html
    input text
     <html> <head>...</head> <body>I'd like to be able to change something like this: <span class="B01-K-ITAL">#1 one line</span> I want to replace the open and close tags without changing or interfering with the text between the tags. I'm pretty sure I should use wildcards, but I can't figure out how to use them properly. <p>note, this isn't as easy as you think.  You need to code up the complete set of html rules in you implementation. You need to allow for a certain amount of mal-formed html.</p> <span class="B01-K-ITAL">#2 don't be greedy</span> <span class="B01-K-ITAL">$3 multiline text</span> <span class="B01-K-ITAL">#4 multiline tag. I believe html allow a carriage return in white space of tags</span> <span class="B01-K-ITAL">#5 split after the class tag. optional white space</span> <sPan class="B01-K-ITAL">#6 mixed case tag</Span> <p>no text #7</p><span class="B01-K-ITAL"></span> <!-- Apparently, this is valid     http://www.positioniseverything.net/articles/cc-plus.html --> <!--[if IE]> <div id="IEroot"> <![endif]--> <p id="IE">This browser is IE.</p> <p id="notIE">This browser is not IE.</p><!--[if IE]> </div> <![endif]--></body> </html>
    output text
     <html> <head>...</head> <body>I'd like to be able to change something like this: <em>#1 one line</em> I want to replace the open and close tags without changing or interfering with the text between the tags. I'm pretty sure I should use wildcards, but I can't figure out how to use them properly. <p>note, this isn't as easy as you think.  You need to code up the complete set of html rules in you implementation. You need to allow for a certain amount of mal-formed html.</p> <em>#2 don't be greedy</em> <em>$3 multiline text</em> <em>#4 multiline tag. I believe html allow a carriage return in white space of tags</em> <em>#5 split after the class tag. optional white space</em> <em>#6 mixed case tag</em> <p>no text #7</p><em></em> <!-- Apparently, this is valid     http://www.positioniseverything.net/articles/cc-plus.html --> <!--[if IE]> <div id="IEroot"> <![endif]--> <p id="IE">This browser is IE.</p> <p id="notIE">This browser is not IE.</p><!--[if IE]> </div> <![endif]--></body> </html>

  • How to create mail merge(feature in word) in adobe pdf and programmatically find and replace these t

    I am very new to adobe.I am required to do the following and am very confused.The scenario is as follows:
    I am required to create a letter template (using adobe designer) where name and few other details will be tags(which needs to be replaced by actual values according to the particular customer later on).We can create such a feature in word using mail merge feature available there.How do we do the same in pdf??Can it be done using adobe designer 7.0???If so,how?
    After I create this template,I am required to write a javascript or a VB program to access this pdf template and find and replace these tags with the actual values and generate a pdf document.Somebody told me that this is to be done using adobe document server(ADS). Can this be done??If so,how?If not what is the alternative product in adobe?
    There are so many adobe products that i am totally at sea and not able to figure out which to use and how.
    Please help me at the very earliest.
    Thanks in advance.
    regards,
    Sowmya

    Let me thank u for a prompt reply but I guess I still need ur help before I accomplish what I set out to do.
    As per your reply I used Forms API in my vb program and it is accessing the fields but I am not able to change the property of the fields.
    I created the pdf form using adobe designer 7.0 and have created a floating field which I want to populate using my code.This is invisible in the pdf template but I when I set the value for it using vb code I need to change its property to visible.
    But when I try to change the "NoViewFlag" property, I get an error as follows:
    Runtime error'-2147220988(80040204)
    Document's Security Settings prohibit the operation being performed
    Please give your suggestions.
    2.Another question is how do I differentiate floating text field and ordinary text field in vb code because the type of both is displayed as text ??
    Thanks again.
    regards,
    Sowmya
    I am putting my code below for ur viewing.

  • Find and Replace Japanese characters in pdf file on iPhone

    Hi eveybody !
    I want to find and replace Japanese characters in pdf on iPhone.
    I using zlib to deflate stream - endstream block and extract text.It's work fine with latin-text.
    But when i work with japanese characters , I don't know how to do it ?
    I decode a sample japanese pdf file, and I know that each Japanese characters are performances as hex string : "<01b7><0e230a23>..."
    But i don't know how to convert Japanese characters to the hex string like that.
    Can evrybody help me?
    Thanks!

    Searching is the same process as extracting - since it's about turning page content into something understandable.  So that still remains what you need to learn/understand - of course, referring back to all the previous sections about font formats, etc.
    Replacing in PDF is EXTREMELY DIFFICULT for two reasons - subset fonts and explicit glyph positioning.  Have you determine (conceptually, if nothing else) how you plan to addresses these two issues?
    PDF doesn't do UTF8 for page content - so don't worry about that.

  • How do I find and replace a word in PDF?

    How do I find and replace a word in PDF?

    In the Find window (Ctrl+F) there's an option to replace the searched text.
    This is in Acrobat XI, and there's no Replace All-function.
    On Thu, Jan 15, 2015 at 4:27 PM, claireb88184899 <[email protected]>

  • How can I find and replace multiple words at once?

    I need to be able to find and replace words into queens English. For example, I need to replace all instances of "color" with "colour," "tire" with "tyre" etc.
    I'm new to scripting, so simple answers would be appreciated :)

    Hi hobs707,
    Please use the below code for your required output:
    Replace ("color", "colour")
    Replace("tire", "tyre")           
    //Replace("hobs707", "hobs007")     just copy and paste and edit the contents as your wish
    function Replace(input, output)
    app.findTextPreferences = app.changeTextPreferences = NothingEnum.nothing;
    app.findTextPreferences.findWhat = input;
    app.changeTextPreferences.changeTo = output;
    app.activeDocument.changeText();
    If the above answer is correct please provide Correct Answers for me.
    Regards
    Beginner

  • Find and replace characters in file names

    I need to transfer much of my user folder (home) to a non-mac computer. My problem is that I have become too used to the generous file name allowances on the Mac. Many of my files have characters such as "*" "!" "?" and "|". I know these are problems because they are often wild cards (except the pipe). Is there a way that I can do a find and replace for these characters?
    For example, search for all files with an "*" and replace the "*" in the file name with an "@" or a letter? I don't mind having to use the terminal for this (I suspect it will be easier).
    Is this possible? Does anyone have any suggestions?
    Thank you in advance for any help you may be able to provide.
      Mac OS X (10.4.8)  

    Yep.
    "A Better Finder Rename" is great for batch file renaming.
    http://www.versiontracker.com/dyn/moreinfo/macosx/11366
    Renamer4mac may be all you need.
    Best check out VersionTracker. In fact everybody should have this site bookmarked and visited daily.
    http://www.versiontracker.com/macosx/

  • Address book: Find and replace characters?

    Hi all,
    I just imported VCF file from Outlook to Address book. I am in Iceland and the Icelandic characters came wrong. Is there any way to batch find and replace characters in Address book? That is in individual cards.
    Thanks,
    Hilmar

    I believe the only way would be to write an AppleScript.

  • XML tag markers moved: Find and Replace causing problem in xml elements

    Hi All,
    I am doing find and replace using GREP. While using the expression like $1, $2 (Found Items) in the change to field it changes the placement of tag marker. If the found item is a part of two of more xml elements, I am getting a serious problem while replacing it. (ie. The xml tag markers are moved.)
    See the screen shot below, then you may get better idea. And help me to overcome this issue.
    This is just an example to show you what i'm trying to say, there are so many cases like this.
    Original text/ Before doing find replace
    After replacing
    Green4ever

    Hi Peter and John,
    but it seems to me that the example is looking for any space that
    follows a semi-colon and has two word characters following it, and
    repalce that with an em space. I think you could do the same using look
    behind and look ahead and not need to replace the found text.
    Yes you are right about the look behind and look ahead. I'd like to show some more examples to show what the actual problem is,
    Original/Before Replacing,
    (Consider there is another case here, instead of em-space some times normal word space will also be there)
    Using the Grep:
    Find What---------> ^(\d+\.(?:\d+)?)~m
    Change To------------->$1\t
    After Replace:
    Did I make any sense? Eventhough this will not make any changes in the layout, my requirement is to insert the tab out-side the tag marker not indise.
    Green4ever

  • Find and Replace Subscript

    Hi,
    I am trying to figure out how to find and replace subscripted characters, e.g. H2O (with the "2" subscripted). How is this done?

    Yeah, it has two options. Use formatting (e.g., subscript, but also font, weight, size, leading, colour and whatnot) or do not use formatting -- which does not help, as it is the same as your search string ...
    But fear not. If your "H2O" is the only one, you can do this -- what Peter meant with his two-step.
    1. Search for "H2O" and replace it with "H[ha!]O". Be sure to check both "whole word" and "case sensitive" icons below the replace field. Every "2"
    i inside
    a text "H2O" will be replaced by something unique -- that's what the "[ha!]" should be.
    2. Replace "[ha!]" with "2".
    i Uncheck
    "whole word" and "case sensitive" (because elsewise it won't find anything), and in the Replace Formatting field, select "Subscript" in the Type panel. This 2nd time, you are only replacing the unique string that was put into its unique position in the previous step.
    A 2nd option (one that I don't recommend to freshmen!) is to use the GREP Find-and-replace. This is, in the right hands, an extremely powerful tool, which can
    i search
    for "H2O" and
    i change
    just the "2" part. If I only knew how, off the top of my mind! But, it's neither an easy to remember shortcut, nor perfectly logical, and the 2-step method is.

Maybe you are looking for

  • MSI K7T Turbo Limited Ed. + XP1700+

    Hi I would like to know if my mainboard (MSI K7T Turbo Limited Edition on KT133A) supports XP1700+ 0,13 processor? I've already read on this site that it doesn't :( but from the other hand I've heard that some people have those Xp1700+ on the same bo

  • Portal version 9.0.2.41.0

    Hi, We are currently experiencing problems with the Portal version 3.0.9.8.0 - The Bug Number is 1844002 titled " REPORTS: MAXIMUM OF 10000 ROWS ARE DISPLAYED " in Portal. Oracle Support in Australia explained to me that the new version of Portal 9.0

  • Can't make payment by phone or online

    I have been trying ro make a payment today on my account. By phone it says cannot be processed and unfortunately we cannot connect you call. I have tried customer service multiple times with the same cannot connect call. I have gone online to pay and

  • HT1937 Why is GiffGaff not recognised as a Carrier?

    I am successfully using my iPhone on the GiffGaff network but in order to Tether my phone I need Apple to officially recognise GiffGaff as a carrier.  Can someone explain why GiffGaff is not recognised as such? It would be a shame to have to jailbrea

  • Can I deploy Mathscript subfunctions without revealing .m source code?

    Good morning. I am developing MATLAB algorithms containing functions and subfunctions. To integrate them in LabVIEW, I plan to use Mathscript nodes. And if I do so, I have to leave the subfunctions .m files in a repository on the PC (right?). My prob