0

I tried to get the path, copied by Win7-function of context menue "Copy Path" this path always comes to clipboard with quotation marks. I made a makro in Word where I dropped the marks off. Then I tried to do this in VBS and it won't work. In my test I found that not any text set between quotation marks is put back into the clipboard if I do this code. (simplified)

Dim string

' Get clipboard text
Set objHTML = CreateObject("htmlfile")
String= objHTML.ParentWindow.ClipboardData.GetData("text")


String = Left(String,2)
Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.Run "cmd.exe /c echo " & String & " | clip", 0, TRUE

works when text is not in quotation marks - only 2 chars left. put a text in marks in clipboard the whole string is put back into clipboard. is there any reason known?

czil
  • 11
  • 4

1 Answers1

0

The Left Function is defined as follows:

Syntax

Left(string, length)

Returns a specified number of characters from the left side of a string.

You need to write your own function to strip a string from double quotes:

VBScript:

option explicit

Function Dequote( sString )
  If Left( sString, 1 ) = """" And Right( sString, 1 ) = """" Then 
    Dequote = Mid( sString, 2, Len( sString) - 2)
  Else
    Dequote = sString
  End If
End Function

Dim String, objHTML, WshShell
' Get clipboard text
Set objHTML = CreateObject( "htmlfile")
String = objHTML.ParentWindow.ClipboardData.GetData( "text")

Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.Run "cmd.exe /c echo(" & Dequote( String) & "|clip", 0, TRUE

Wscript.Echo String, Dequote( String)   ' debugging output

VBA (single-line if syntax, not verified):

Private Function Dequote(S As String) As String
If Left(S, 1) = """" And Right(S, 1) = """" Then Dequote = Mid(S, 2, Len(S) - 2) Else Dequote = S
End Function
JosefZ
  • 28,460
  • 5
  • 44
  • 83
  • This works fine. Thanks. I used the left() for demonstration thought, I manipulate the string in a variable and give it to the clip. you do the same but call it by a function inside the string given to the cmd.exe. why doesn't it accept my variable when I gave it in quotes but does it, when not? (I got 2 characters when the string is not in quotes but the whole string when in quotes. thats odd?) fine thin, thank you very much!! – czil Feb 26 '19 at 15:18
  • The part of VBA works with a string fine. Was the same, I tried this code: `Sub zwischI() Set objHTML = CreateObject("htmlfile") strDrin = objHTML.parentWindow.clipboardData.GetData("text") strDrin = Mid(strDrin, 2, Len(strDrin) - 2) Debug.Print Text objHTML.parentWindow.clipboardData.setdata "Text", strDrin End Sub` this works too (without the check if the quotes are there) but don't put it back in to clipboard. – czil Mar 01 '19 at 22:08
  • @czil Unfortunately, the `.clipboardData.SetData` method does not change the clipboard for _htmlfile_ COM object in VBScript (and `.clipboardData.ClearData` as well). Works e.g. for [`InternetExplorer.Application` COM object](https://stackoverflow.com/a/19297281/3439404). – JosefZ Mar 02 '19 at 09:56