Automating steps to copy URL from IE into Word

L

Larry

This seemed like a Windows question more than a Word question, I sent
it to a Windows newsgroup, and was told it was a Word question. So here
goes.



Here's the kind of thing I do a million times. I've just copied some
text from a web page into Word. Now I want to copy the URL of that web
page into Word.

I do Alt+Tab to switch back to the Internet Exporer page.

I press Alt+D to place cursor in address bar.

I press Ctrl+C to copy the address.

I use Alt+Tab switch back to Word.

I press Ctrl+V to paste the url into Word.

Would there be a way to automate these five steps into one action,
whether done by a .vbs file or some other means?
 
S

Steve Yandl

Larry,

This is more a vbs and html solution than Word and it isn't quite what you
asked for but I think the end result is something you will like. I've done
a script that adds a new item to your Internet Explorer context menu. If
you select some text on a web page and then right click the selected text,
instead of selecting 'Copy' you select the new item 'Research Entry'. The
script looks in your MyDocuments folder for a Word document named
"Web_Research.doc" and creates it if it doesn't already exist. It then
appends that document with the date and time, the URL of the page, the
document title for the page and the selected text. You get a pop up html
window to confirm that you want all this to happen and you can enter a typed
comment if you want to add a note to the text capture.

The easy way to get all this is to go to http://yandlfiles.home.comcast.net/
and click the link in the upper left for my text archive programs (you will
want the second one which requires Word). The zip files contain a ReadMe
file, the core htm file and installation and uninstall scripts. All can be
opened in notepad allowing you to check for security concerns or modify the
scripts if you're comfortable with that sort of thing.

If you want do do this the manual way, it isn't that tough. Essentially, a
new registry key called HKEY_CURRENT_USER\Software\Microsoft\Internet
Explorer\MenuExt\Research_Entry is created. It is given a default value
that is the path and file name to the new htm file with the working
components of the script. There is also a value named "Flags" with DWord
value of 1.

The critical part of the new htm file (beside placing where the above
registry key points) are the lines below:
Set oWindow=window.external.menuArguments
Set oDocument=oWindow.document
docAddress=oDocument.URL
docTitle=oDocument.Title

When those lines are in a script launched from the IE context menu, the
appropriate information from the document that was right click is returned.
The entire contents of the html file I use is below:

<html>
<head>
<TITLE>Retrieve and Save Web Document Information</TITLE>
</head>

<BODY STYLE="BACKGROUND-COLOR:#EEEEEE; MARGIN:10">
<P ID=para1></P>
<P ID=para2></P>
<P ID=para3></P>
<P ID=para4></P>
<P ID=para5></P>
<p><font size="4" color="red">Enter any comments to go with the document
information</font></p>

<TEXTAREA ID="comments" cols="52"></TEXTAREA>
<P ID=para6></P>
<P ID=para7></P>
<p><input type="button" name="B1" value=" SAVE TO WEB_RESEARCH.DOC ">

<script language="VBScript"><!--

Dim oWindow, oDocument, docAddress, docTitle, oSelect, oSelectRange,
strSelection, WSH, fso
Set oWindow=window.external.menuArguments
Set oDocument=oWindow.document
docAddress=oDocument.URL
docTitle=oDocument.Title
If docTitle = "" Then
docTitle = "(no document title)"
End If
Set oSelect=oDocument.selection
Set oSelectRange=oSelect.createRange()
strSelection=oSelectRange.text
Set WSH = CreateObject ("WScript.shell")
strMyDocs = WSH.SpecialFolders("MyDocuments")
' strMyDocs = "C:\Test\myfolder"
Set fso = CreateObject("Scripting.FileSystemObject")

Dim dy, mo, yr, hr, mi, se, dyStr, moStr, yrStr, hrStr, miStr, seStr, serStr
mo=month(Now)
dy=day(Now)
yr=year(Now)
hr=hour(Now)
mi=minute(Now)
se=second(Now)
'
If len(CStr(mo))=1 Then
moStr="0"&CStr(mo)
Else
moStr=CStr(mo)
End If
'
If len(CStr(dy))=1 Then
dyStr="0"&CStr(dy)
Else
dyStr=CStr(dy)
End If
'
yrStr=Right(CStr(yr),2)
'
If len(CStr(hr))=1 Then
hrStr="0"&CStr(hr)
Else
hrStr=CStr(hr)
End If
'
If len(CStr(mi))=1 Then
miStr="0"&CStr(mi)
Else
miStr=CStr(mi)
End If
'
If len(CStr(se))=1 Then
seStr="0"&CStr(se)
Else
seStr=CStr(se)
End If
'
serStr="T"&yrStr&moStr&dyStr&hrStr&miStr&seStr

document.all("para1").innerText="Information collected at: " & Now
document.all("para2").innerText="Document URL = " & docAddress
document.all("para3").innerText="Document Title = " & docTitle
document.all("para4").innerText="Document Last Modified on " &
oDocument.LastModified
If strSelection <> "" Then
document.all("para5").innerText="Document Selection = " & strSelection
End If
document.all("para6").innerText="The info will be bookmarked as " & serStr &
" in Word document below"
document.all("para7").innerText=strMyDocs & "\Web_Research.doc"

Sub B1_onClick
On Error Resume Next
Dim oWd, doc
Set oWd = CreateObject("Word.Application")
If Err.Number <> 0 Then
Alert "Problem opening Word or initiating some other object"
Window.Close
End If
If fso.FileExists(strMyDocs & "\Web_Research.doc") Then
Set doc = oWd.documents.open(strMyDocs & "\Web_Research.doc")
Else
Set doc = oWd.documents.add
doc.SaveAs strMyDocs & "\Web_Research.doc"
End If
Dim rngCurrent
oWd.Selection.WholeStory
oWd.Selection.MoveRight
oWd.Selection.TypeParagraph
oWd.Selection.TypeText("Information gathered at " & Now)
oWd.Selection.TypeParagraph

Set rngCurrent = oWd.Selection
oWd.ActiveDocument.Bookmarks.Add serStr,rngCurrent

oWd.Selection.TypeText("From ")
oWd.Selection.Font.Color=255
oWd.Selection.TypeText("URL ")
oWd.Selection.Font.Color=0
oWd.Selection.TypeText(docAddress)
oWd.Selection.TypeParagraph
oWd.Selection.TypeText("Document Title = " & docTitle & " then last modified
on " & oDocument.LastModified)
If strSelection <> "" Then
oWd.Selection.TypeParagraph
oWd.Selection.Font.Color=255
oWd.Selection.TypeText("Selected Text = ")
oWd.Selection.Font.Color=0
oWd.Selection.TypeText(strSelection)
End If
If comments.Value <> "" Then
oWd.Selection.TypeParagraph
oWd.Selection.Font.Color=255
oWd.Selection.TypeText("Comment = ")
oWd.Selection.Font.Color=0
oWd.Selection.TypeText(comments.Value)
End If
oWd.Selection.TypeParagraph
oWd.Selection.TypeText("********************************")
oWd.ActiveDocument.Save
oWd.ActiveDocument.Close
oWd.Quit
Set oWd = Nothing
Window.Close

End Sub

Set WSH = Nothing

--></script>

</BODY>
</html>
 
L

Larry

Steve,

This is just amazing. It will be fantastic to be able to do all this in
one step. I can't wait to install and try this out. I didn't know that
an html file could have text that was executible code that could be run
like a macro or a .vbs script.

I'm going to use your install files rather than try to create this
manually. The only change I will have to make is changing the path of
Web_Research" doc since I long ago change my "My Documents" folder's
name to "Documents."

I'll let you know how it goes.

I'm curious to see if character formatting in the selection, such as
italics, is carried over.

Larry
 
S

Steve Yandl

Larry,

You may not have to worry about changing the "My Documents" folder. Try it
first to see. The Windows Script Host has a set of special folders and the
path should be returned even if it isn't in the usual location or has been
renamed.

The only questions I've received from people have involved notifications
from Norton AV or some other anti-virus software. If you're familiar with
using vbs, this won't throw you off but non scripting types can get
concerned and don't know what to check to feel safe.


Steve
 
L

Larry

Steve,

This is great, it's wonderful to know that such capabities exist, but this program is geared for a different set up from mine. Here are my thoughts and suggestions.

What this does is append each entry to a _closed_ Word document. This is perfect for accumulating a lot of material from the web in a kind of research project. However, if the document is open, it opens a second instance of Word without my custom toolbar and prompts the user to save it. So that would have to be changed. The program needs to insert the material into open document. And it must not open additional instances of Word if Word is already open. That creates problems for me.

Also, I don't need a single saved document into which to put the information from many different such operations. Each such insert could go into its own, new Word document, which I could then save manually.

When the application opens, it prompts the user for a comment, which then makes it necessary to scroll down to where the button is to execute the program. It would be better to skip all that and have the whole operation take place in one step. I could do without the comment, since I'm going to be working on the Word document into which I've inserted the material, I can add whatever I want directly in the Word document.

Another thing. This does not carry over character formatting of the selected text, such as italics. I don't supposed that would be possible? If it's not possible, what I might want to do is change the program so that it inserts the title and url into a new Word document, while copying the selected text in the webpage AND switching the focus to the Word document, so then all I have to do is paste the (formatted) text underneath the title and url.

Thanks,
Larry



Thanks,
Larry
 
S

Steve Yandl

Larry,

The original incarnation of the program was created in response to a
question on a newsgroup. It basically created a new text file that was
given a name based on date and time. It grabbed the text as a string plus
the URL as a reference. People in the newsgroup where I provided it started
asking for more features, like the option to choose some other file naming
convention, the option to add comments, etc. After using it a bit myself, I
found it would work better for me if I used Word because I could then use
Word's find feature to locate text by key term or phrase. I incorporated
bookmarks with my date time strings from the previous version so I could
quickly zoom in to an approximate time once the archive document got large.
For my own use, I wasn't concerned about the formatting, only the actual
text, the URL, and the date/time reference. I added the ability to add
comments based on feedback on the earlier version but like you I could live
without that and might make a more streamlined version for my own use.

I might experiment with using the clipboard for the text seletion itself and
that might retain the font properties.

What might work better for you would be to write some VBA for a Word
template and have your VBA launch an instance of Internet Explorer. That
should give you access to the document properties for any page opened with
that particular instance of IE.

Steve




Steve,

This is great, it's wonderful to know that such capabities exist, but this
program is geared for a different set up from mine. Here are my thoughts
and suggestions.

What this does is append each entry to a _closed_ Word document. This is
perfect for accumulating a lot of material from the web in a kind of
research project. However, if the document is open, it opens a second
instance of Word without my custom toolbar and prompts the user to save it.
So that would have to be changed. The program needs to insert the material
into open document. And it must not open additional instances of Word if
Word is already open. That creates problems for me.

Also, I don't need a single saved document into which to put the information
from many different such operations. Each such insert could go into its
own, new Word document, which I could then save manually.

When the application opens, it prompts the user for a comment, which then
makes it necessary to scroll down to where the button is to execute the
program. It would be better to skip all that and have the whole operation
take place in one step. I could do without the comment, since I'm going to
be working on the Word document into which I've inserted the material, I can
add whatever I want directly in the Word document.

Another thing. This does not carry over character formatting of the
selected text, such as italics. I don't supposed that would be possible?
If it's not possible, what I might want to do is change the program so that
it inserts the title and url into a new Word document, while copying the
selected text in the webpage AND switching the focus to the Word document,
so then all I have to do is paste the (formatted) text underneath the title
and url.

Thanks,
Larry
 
L

Larry

What might work better for you would be to write some VBA for a Word
template and have your VBA launch an instance of Internet Explorer.
That
should give you access to the document properties for any page opened
with
that particular instance of IE.

That would be great. Then I would put the appropriate code in the
webpage document and it would run when the webpage was opened?

Except, doesn't the text in the webpage already have to be selected when
the procedure is started? Using Word to open the Webpage seems to be
doing it backward.

Also, this idea of executible code inside a html file is very new to me.
You don't see the code displayed in the visible web page. So what is it
that triggers it?

In any case, how would I start to set this up?

Larry
 
L

Larry

In fact, what I want is relatively simple, compared to your program.
For example, I already have a Word template that I open with a Winkey
and that opens a new document, and pastes in the text that I've copied
from a webpage, and cleans up any formatting problems so that I get a
uniform result each time. All I really want to add on top of that is to
get the url automatically copied into the Word document along with the
text (and maybe the title of the webpage as well).

So it seems to me that the code to open the template could be part of
the code that's in the html file. This code makes the following things
happen.

It copies the selected text.
It gets a handle on the url and the title.
It opens the Word template which opens a new Word document.
It sticks the url and the title into the new document.
The AutoExec macro in the Word Template pastes the Clipboard contents
into the Word document and formats it.
 
L

Larry

Another thing I'm wondering. The code in web_research.htm which sets up
everything, how are its commands directed at the webpage that was last
opened? That is, I open web page X, I select text and run the Research
Entry item on the menu which opens web_research htm which then carries
out the commands to turn the selected text into a string and captures
the url and so on But how does this code know what webpage it's
supposed to be doing this to?
 
S

Steve Yandl

Larry,

That shouldn't be too hard.

I'm getting ready for a meeting this evening and can't look at this tomorrow
but I've got a few ideas on ways to get you started.

Steve
 
S

Steve Yandl

Larry,

Inside an html document, VBScript is contained between script tags. You
don't even need to have a document "body" that generates a document in a
window but you generally do. However, script in an html file is subject to
the user's security settings for IE and can make results hard to predict.
On the other hand, you can create an html file and convert it to an hta file
(just by changing the extension) and it will operate like a standalone
program outside the IE restrictions but with all the tools of DHTML and vbs.

Steve
 
S

Steve Yandl

The line
Set oWindow=window.external.menuArguments

That basically creates a reference "oWindow" that is not in the window where
the script is running but, rather, it is the window upon which a context
menu item (created by the new registry key) was run. Then, when you have
Set oDocument=oWindow.document
docAddress=oDocument.URL
docTitle=oDocument.Title
docAddress and docTitle are the returned strings that represent the URL and
document title of the open page represented by oWindow.

Steve
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top