From: Larry on 23 Jun 2010 19:39 I developed this script to change smart quotes and apostrophes to straight ones. I select the text, run the VBS file, it makes the changes, and then copies the changed text to the Clipboard. It words, but the code I have seems clunky and repetitive. Is there a more economical way of doing the same thing? Thanks. Dim oHtml, ws, myText Dim oShell Set oHtml = CreateObject("htmlfile") Set ws = WScript.CreateObject("WScript.Shell") ' Copy the selection. set oShell = CreateObject("WScript.Shell") oShell.SendKeys"^c" ' Convert clipboard data into string. myText=oHtml.parentwindow.clipboardData.getData("text") ' Use Regular Expression pattern and Replace function to change smart quotes ' to straight quotes, etc. ' change smart open quotes to straight quotes Set RegEx = New RegExp RegEx.Pattern = "[�]" RegEx.Global = True ' affects all instances not just the first instance. myText = RegEx.Replace(myText,"""") ' change smart close quotes to straight quotes Set RegEx = New RegExp RegEx.Pattern = "[�]" RegEx.Global = True ' affects all instances not just the first instance. myText = RegEx.Replace(myText,"""") ' change smart open single quote to plain single quote Set RegEx = New RegExp RegEx.Pattern = "[�]" RegEx.Global = True ' affects all instances not just the first instance. myText = RegEx.Replace(myText,"'") ' change smart close single quote or apostrophe to plain single quote Set RegEx = New RegExp RegEx.Pattern = "[�]" RegEx.Global = True ' affects all instances not just the first instance. myText = RegEx.Replace(myText,"'") ' convert string back to clipboard: Set oAutoIt = WScript.CreateObject("AutoItX3.Control") oAutoIt.ClipPut(myText)
From: gimme_this_gimme_that on 23 Jun 2010 21:29 What you might be thinking of is a regular expression that works like this - with a pipe delimiter: /A|B|C|D/E/ That changes every appearance of the letter A, B, C, or D with the letter E. Or you might be thinking of a refactoring so you can clean up the code : Function doRegEx(myOrigText,myNewText,myPattern) Set RegEx = New RegExp RegEx.Pattern = myPattern RegEx.Global = True doRegEx = RegEx.Replace(myText,myNewText) End Function - That's not tested - but you get the idea. Regarding your post ... What am I not seeing? Because of this: > > ' Use Regular Expression pattern and Replace function to change smart quotes > ' to straight quotes, etc. > ' change smart open quotes to straight quotes > Set RegEx = New RegExp > RegEx.Pattern = "[ ]" > RegEx.Global = True ' affects all instances not just the first instance. > myText = RegEx.Replace(myText,"""") All instances of "[ ]" have been replaced so these instructions get executed but don't do anything: > > ' change smart close quotes to straight quotes > Set RegEx = New RegExp > RegEx.Pattern = "[ ]" > RegEx.Global = True ' affects all instances not just the first instance. > myText = RegEx.Replace(myText,"""") > > ' change smart open single quote to plain single quote > Set RegEx = New RegExp > RegEx.Pattern = "[ ]" > RegEx.Global = True ' affects all instances not just the first instance. > myText = RegEx.Replace(myText,"'") > > ' change smart close single quote or apostrophe to plain single quote > Set RegEx = New RegExp > RegEx.Pattern = "[ ]" > RegEx.Global = True ' affects all instances not just the first instance. > myText = RegEx.Replace(myText,"'") > By the way... Statements like this one only needs to be executed once so you can pull these out of the Sub and put them at the top of your script... Set RegEx1 = New RegExp RegEx1.Pattern = "[ ]" RegEx1.Global = True If you ever loop (you're not looping so forget it) - but that's something I flash on.
From: Mayayana on 23 Jun 2010 23:24 Why do you need RegExp? Can't you just use: s = Replace(s, Chr(147),Chr(34)) s = Replace(s, Chr(148),Chr(34)) (Of course you can also use the literals. I personally prefer Chr when dealing with quotes. It's less confusing.) If you read and write the file instead of using the clipboard then you can also cut out the bloat of needing to load an IE instance and AutoIt. | I developed this script to change smart quotes and apostrophes to straight | ones. I select the text, run the VBS file, it makes the changes, and then | copies the changed text to the Clipboard. | | It words, but the code I have seems clunky and repetitive. Is there a more | economical way of doing the same thing? Thanks. | | | Dim oHtml, ws, myText | Dim oShell | Set oHtml = CreateObject("htmlfile") | Set ws = WScript.CreateObject("WScript.Shell") | | ' Copy the selection. | | set oShell = CreateObject("WScript.Shell") | oShell.SendKeys"^c" | | ' Convert clipboard data into string. | | myText=oHtml.parentwindow.clipboardData.getData("text") | | | ' Use Regular Expression pattern and Replace function to change smart quotes | ' to straight quotes, etc. | ' change smart open quotes to straight quotes | Set RegEx = New RegExp | RegEx.Pattern = "[�]" | RegEx.Global = True ' affects all instances not just the first instance. | myText = RegEx.Replace(myText,"""") | | ' change smart close quotes to straight quotes | Set RegEx = New RegExp | RegEx.Pattern = "[�]" | RegEx.Global = True ' affects all instances not just the first instance. | myText = RegEx.Replace(myText,"""") | | ' change smart open single quote to plain single quote | Set RegEx = New RegExp | RegEx.Pattern = "[�]" | RegEx.Global = True ' affects all instances not just the first instance. | myText = RegEx.Replace(myText,"'") | | | ' change smart close single quote or apostrophe to plain single quote | Set RegEx = New RegExp | RegEx.Pattern = "[�]" | RegEx.Global = True ' affects all instances not just the first instance. | myText = RegEx.Replace(myText,"'") | | | | ' convert string back to clipboard: | | Set oAutoIt = WScript.CreateObject("AutoItX3.Control") | oAutoIt.ClipPut(myText) |
From: Mayayana on 24 Jun 2010 09:27 Rereading your post I see that I missed something, and you left something out. You select text? Are you saying that you've got a webpage loaded in IE and then you run a script to get altered, selected text? That's not in your code. If you don't post the actual code it's hard for anyone to help. If you're dealing with a page in IE then you might find it worthwhile to access the DOM, maybe writing an HTA. The following code will test for a selection and copy it: If (Document.selection.Type = "Text") Then Set TRSel = Document.selection.createRange LRet = TRSel.compareEndPoints("StartToEnd", TRSel) If (LRet <> 0) Then If (Len(TRSel.Text) > 0) Then sClip = TRSel.htmlText End If Set TRSel = Nothing End If sClip is the selected text. The TextRange htmlText property is also read/write. So you can process sClip and then replace the selection if you like. See here for a sample editor project: http://www.jsware.net/jsware/scripts.php5#domed It's an HTA that has a split window, with a toolbar at top. It's designed as a WYSIWYG HTML editor, in order to demonstrate how to access various IE DOM methods. If this is not the sort of thing you want then maybe you could explain more. Your post is not telling the whole story.
From: Larry on 24 Jun 2010 12:36
Thanks, based on what you showed me, I'm trying something out along these lines: ' The purpose of this is to copy text into a blog editing window ' where I want to avoid using smart quotes, etc. ' The "—" and "é" code enables the correct display of ' special characters such as M dashes and accented letters in a blog ' that can't otherwise display them correctly. ' Before running VBS file, manually select the text ' (which is usually in an email or webpage). Dim oHtml, ws, sText Dim oShell Set oHtml = CreateObject("htmlfile") Set ws = WScript.CreateObject("WScript.Shell") ' Copy the selection. set oShell = CreateObject("WScript.Shell") oShell.SendKeys"^c" ' Convert clipboard data into string. sText=oHtml.parentwindow.clipboardData.getData("text") ' replace smart quotes with straight, etc. sText = Replace(sText, Chr(147),Chr(34)) sText = Replace(sText, Chr(148),Chr(34)) sText = Replace(sText, Chr(145),Chr(39)) sText = Replace(sText, Chr(146),Chr(39)) sText = Replace(sText, Chr(151),"—") sText = Replace(sText, Chr(130),"é") ' put string into Clipboard: Set oAutoIt = WScript.CreateObject("AutoItX3.Control") oAutoIt.ClipPut(sText) |