Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Pasting from Web page into opened Word document

Status
Not open for further replies.
Sep 27, 2001
179
AU
Hi

Is it possible to do the following:

Open a word document, select an area of the document switch to a web page and click a button to paste the contents of a text box into the document?

So far I have managed to get a script developed that will paste the text correctly but into a new Word window.

It looks as though the new ActiveXObject('Word.Application'); will always open a new Word window.

The code I have so far is:

Code:
<script language="javascript">
function paste2word(){
var adoc=new ActiveXObject('Word.Application'); 

var text = document.all.addr_address1.value;
if (document.all.addr_address2.value >''){text=text+'\r'+document.all.addr_address2.value};
if (document.all.addr_address3.value >''){text=text+'\r'+document.all.addr_address3.value};
if (document.all.addr_address4.value >''){text=text+'\r'+document.all.addr_address4.value};
if (document.all.addr_state.value >''){text=text+'\r'+document.all.addr_state.value};
if (document.all.addr_postcode.value >''){text=text+'\r'+document.all.addr_postcode.value};

if (adoc != null)
    {
    adoc.Visible = true;
    adoc.Documents.Add();
    adoc.Selection.TypeText(text);
    }
}
</script>
</HEAD>
<body>
<input type=button onClick="paste2word();" value="Word Paste">
</BODY>

Thanks

Rob
 
well, this isn't technically javascript, but you could rewrite it using jscript if you wanted... and i was bored, so this should get you started:

requires WMI, which i think is Win2K and above, and it should go without saying that scripts of this nature will only run in IE

Code:
<script language="vbscript">

dim wordHandle, bWordRunning
dim oShell

set oShell = wscript.createobject("wscript.shell")

bWordRunning = false
wordHandle = getWordHandle()

rem see if we got the handle
if wordHandle = false then
	oShell.run "winword"
	wordHandle = getWordHandle()
else
	bWordRunning = true
end if

rem activate word
oShell.appActivate wordHandle
wscript.sleep 500

if bWordRunning then
	oShell.sendkeys "word was already running~"
else
	oShell.sendkeys "word was not running~"
end if

rem returns the process id of winword.exe if running
rem else returns false
function getWordHandle
	dim WMI, wmiWin32Objects, wmiWin32Object, varHandle
	
	set WMI = getObject("WinMgmts://")
	set wmiWin32Objects = WMI.InstancesOf("Win32_Process")
	varHandle = false

	for each wmiWin32Object in wmiWin32Objects
		if (lcase(wmiWin32Object.description) = "winword.exe") then
			varHandle = wmiWin32Object.handle
		end if
	next
	
	getWordHandle = varHandle
end function

</script>

-jeff
try { succeed(); } catch(E) { tryAgain(); } finally { rtfm(); }
i like your sleeves...they're real big
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top