locked
VBA Sending Only First Character from Text File RRS feed

  • Question

  • I'm using Nuance Dragon NaturallySpeaking to run the following script using VBA:

    Sub Main
        Dim objFSO
        Set objFSO = CreateObject("Scripting.FileSystemObject")
    
        Dim objTextStream
    
        Const strFileName = "C:\Users\Username\Desktop\Clipboard.txt"
        Const fsoForReading = 1
    
        If objFSO.FileExists("C:\Users\Username\Desktop\Clipboard.txt") Then
            'The file exists, so open it and output its contents
            Set objTextStream = objFSO.OpenTextFile(strFileName, fsoForReading)
            SendKeys objTextStream.ReadAll
            objTextStream.close
            Set objTextStream = Nothing
        Else
            'The file did not exist
            SendKeys "was not found."
        End If
    
        'Clean up
        Set objFSO = Nothing
    End Sub

    The text file contains a domain name.  However, instead of reading and sending the entire domain name, it's just writing the first character from the text file. 

    For example, if the text file contains the "http://www.msdn.microsoft.com", my script would just send "h".

    I've spent many hours trying to fix this, and can't figure out what I'm doing wrong.  Any help greatly appreciated.

    • Edited by Alex Fe Monday, July 17, 2017 2:59 PM
    Monday, July 17, 2017 2:57 PM

All replies

  • Well, SendKeys does exactly what is says: sending one key.. maybe you should copy the content into the clipboard and paste it.

    Monday, July 17, 2017 3:06 PM
  • Hi Alex,

    I'm not sure I follow. Where are you sending the text? To Naturally Speaking?

    Monday, July 17, 2017 3:17 PM
  • Stefan,

    I'm actually using the clipboard for something else. Specifically, I'm trying copy and paste a sentence from an article (in html, with linked text) into IE, and then ALSO write the url where I found the article into IE. 

    So unless I figure out how to save the linked text from the article as an object, I need to use the clipboard for that.

    That's why I was trying to save the url to a text file and then retrieve it.

    Monday, July 17, 2017 3:58 PM
  • theDBguy,

    I'm trying to send the text to IE. Specifically, I have a script that opens IE, navigates to my web page, and then logs in.

    The script above would just send the keys to the open IE webpage.

    Monday, July 17, 2017 3:59 PM
  • theDBguy,

    I'm trying to send the text to IE. Specifically, I have a script that opens IE, navigates to my web page, and then logs in.

    The script above would just send the keys to the open IE webpage.

    Hi Alex,

    I asked because the Help topics states the SendKeys command sends the keystrokes to the "active" window. If you're trying to send the text to IE by using SendKeys, do you know if IE was the active window?

    Just curious...

    Monday, July 17, 2017 4:45 PM
  • theDBguy,

    Yes, IE is definitely the active window. Likewise, when I try to use the script - for test purposes - to send to Word, or Firefox, or any other program, it still just sends the first character.


    So not a focus issue ... it's something else.
    • Edited by Alex Fe Monday, July 17, 2017 5:31 PM
    Monday, July 17, 2017 5:31 PM
  • theDBguy,

    Yes, IE is definitely the active window. Likewise, when I try to use the script - for test purposes - to send to Word, or Firefox, or any other program, it still just sends the first character.


    So not a focus issue ... it's something else.

    Okay, I guess it might be then. However, depending on exactly what you're doing, I would tend to use the HTTPRequest class object rather than rely on SendKeys. To learn more about the HTTPRequest class, take a look at this Wiki article at UtterAccess.

    Hope it helps...

    Monday, July 17, 2017 5:37 PM
  • Thanks, theDBguy. But it looks like HTTPRequest is used when one will be drawing info from the same website over and over.

    My scenario is where I spontaneously find something good while surfing, and then want to save a quote and url where I found the quote into IE.

    In other words, I may only reference a given website once.

    Monday, July 17, 2017 5:56 PM
  • Thanks, theDBguy. But it looks like HTTPRequest is used when one will be drawing info from the same website over and over.

    My scenario is where I spontaneously find something good while surfing, and then want to save a quote and url where I found the quote into IE.

    In other words, I may only reference a given website once.

    Hi,

    Not sure I understand what you're doing. Are you trying to save/enter the text into IE or trying to grab the info from IE, while browsing, and then trying to save it into a table or text file or something?

    I could be wrong but I think the HTTPRequest class might have methods to both grab data from IE and also send data to the server. For example, if you know the name of the Textbox in IE, you can assign text to it and then call the Submit action using the HTTPRequest class.

    Just my 2 cents...

    Monday, July 17, 2017 6:00 PM
  • Let me explain what I'm doing ...

    I'm a journalist. I spend countless hours copying brief passages from various webpages, and then pasting those passages - along with attribution to the websites I found them - into web-based articles.

    For example, many of my articles have passages which look like this:

    The Mexican finance minister wrote (http://www.cnbc.com/2017/02/06/german-finance-minister-agrees-euro-too-low-for-germany.html):

    The euro exchange rate is, strictly speaking, too low for the German economy's competitive position.

    I've already got a working VBA script which opens IE, navigates to my webpage, logs me in, and then opens a new article page for me.

    Now I want to use VBA to do the following:

    (1) When I highlight the text I want to copy - in the example "The euro exchange rate is, strictly speaking, too low for the German economy's competitive position"

    (2) I would also automatically copy the url where the text comes from (in this case http://www.cnbc.com/2017/02/06/german-finance-minister-agrees-euro-too-low-for-germany.html).  This is the piece that my original question focused on.

    (3) So when I paste the text into my website, it would automatically paste the text and ALSO the url. In other words, I would end up with what I wrote above.

     

    • Edited by Alex Fe Monday, July 17, 2017 6:12 PM
    Monday, July 17, 2017 6:09 PM
  • Hi Alex,

    Thanks for the clarification. It sounds like the main problem with your current approach is the use of SendKeys. If so, you might try using a different approach such as assigning the contents of the text file into a memory variable and then assigning the content of the variable into the web page using the DOM (Document Object Model) of a browser object. Or, perhaps you can use API to copy the text file into the clipboard and then paste it onto the web page.

    Just a thought...

    By the way, have you been able to use SendKeys in this manner before and just trying to troubleshoot now why it stopped working?

    Monday, July 17, 2017 6:27 PM
  • Thanks, theDBguy.   Before I try to DOM approach, a question on the API:  if I copy text into clipboard using API, then can I ALSO copy into the clipboard manually using control-c on my keyboard?  Or is there simply one clipboard, whether you copy into it using API or manual method?

    To answer your question, this SendKeys script has never worked. However, I think I've seen people use SendKeys to send a previously-saved string before.

    Monday, July 17, 2017 6:35 PM
  • Hi Alex,

    There's only one memory location for clipboard contents; however, a possible approach you could try is do a Ctrl+C to copy the text from a web page and then use API to save the content of the clipboard into a text file or memory variable. You can then use your script to get the domain or URL for the article and then paste it into the clipboard along with the previously saved clipboard content, which results in having both the article text and the address together. Now, whether you can use both as-is or have to parse them apart depends on your needs.

    Hope it helps...

    Monday, July 17, 2017 6:45 PM
  • IE.LocationURL is supposed to automatically detect the url (https://msdn.microsoft.com/en-us/library/aa752058%28v=vs.85%29.aspx?f=255&MSPPError=-2147217396l, but I can't figure out how to make it write for me.



    • Edited by Alex Fe Monday, July 17, 2017 9:04 PM
    Monday, July 17, 2017 9:01 PM
  • This doesn't do what I want (I want to write it a text field in IE, not debug) ... but this is a script to get the url from IE:

    Sub GetIEWindows() 
         'Requires reference to Microsoft Internet Controls
        Dim SWs As SHDocVw.ShellWindows, vIE As SHDocVw.InternetExplorer 
         
         'Establish link to IE application
        Set SWs = New SHDocVw.ShellWindows 
         
        For Each vIE In SWs 
            If Left(vIE.LocationURL, 4) = "http" Then 'avoid explorer windows/etc this way
                If MsgBox("IE Window found.  The URL is:" & vbCrLf & vIE.LocationURL & vbCrLf & _ 
                vbCrLf & "Do you want to see the html?", vbYesNo) = vbYes Then 
                     'Show html in a msgbox
                    MsgBox vIE.Document.Body.innerHTML 
                     'Or put it to a file
                     'dim vFF as long
                     'vff=freefile
                     'open "C:\thehtml.txt" for output as #vff
                     'print #vff,vie.document.body.innerhtml
                     'close #vff
                End If 
            End If 
        Next 
         
        Set SWs = Nothing 
        Set vIE = Nothing 
    End Sub 

    Tuesday, July 18, 2017 12:20 AM
  • hmm, while it's not really a solution in the terms of the form:

    Have you considered writing an accelerator?

    Tuesday, July 18, 2017 7:42 AM