#include <Word.au3>
#include <Array.au3>
$sURL = 'http://www.rts-tender.ru/DFile.ashx?guid=87864c7c-1741-4e0b-9a81-04152506e117'
$iStart = TimerInit()
$sHost = StringRegExpReplace($sURL, '.*://(.*?)/.*', '\1')
$sPage = StringRegExpReplace($sURL, '.*://.*?(/.*)', '\1')
$sResponse = _HTTPGetResponse($sHost, $sPage)
If Not $sResponse Then _Exit()
$sFileName = StringRegExpReplace($sResponse, '(?s).*filename=(.*?)[\r\n].*', '\1')
If Not $sFileName Then _Exit()
$hDownload = InetGet($sURL, @TempDir & '\' & $sFileName, 1, 1)
Do
Sleep(20)
Until InetGetInfo($hDownload, 2)
InetClose($hDownload)
If Not FileExists(@TempDir & '\' & $sFileName) Then _Exit()
$oWordApp = _WordCreate('', 0, 0)
If @error Then _Exit(1)
$oDoc = _WordDocOpen($oWordApp, @TempDir & '\' & $sFileName)
If @error Then _Exit(1)
$oLinks = _WordDocLinkGetCollection($oDoc)
If @error Then _Exit(1)
$iCount = @extended
If Not $iCount Then _Exit(1)
Dim $aLink[$iCount + 1][2] = [[$iCount]]
For $i = 1 To $iCount
$oLink = _WordDocLinkGetCollection($oDoc, $i)
If @error Then _Exit(1)
$aLink[$i][0] = $oLink.TextToDisplay
$aLink[$i][1] = $oLink.Address
Next
$sTime = StringFormat('%.2f sec', TimerDiff($iStart) / 1000)
_WordDocClose($oDoc)
_WordQuit($oWordApp)
FileDelete(@TempDir & '\' & $sFileName)
_ArrayDisplay($aLink, $sTime)
Func _HTTPGetResponse($sHost, $sPage)
TCPStartup()
Local $sName_To_IP = TCPNameToIP($sHost)
Local $iSocket = TCPConnect($sName_To_IP, 80)
If $iSocket = -1 Then
TCPShutdown()
Return SetError(1, 0, '')
EndIf
Local $sCommand = 'HEAD ' & $sPage & ' HTTP/1.1' & @CRLF
$sCommand &= 'Host: ' & $sHost & @CRLF
$sCommand &= 'User-Agent: AutoIt/' & @AutoItVersion & ' (Windows; U; Windows NT 5.1; en-US; rv:1.8.1)' & @CRLF
$sCommand &= 'Referer: ' & $sHost & @CRLF
$sCommand &= 'Connection: close' & @CRLF & @CRLF
Local $BytesSent = TCPSend($iSocket, $sCommand)
If $BytesSent = 0 Then
TCPShutdown()
Return SetError(2, @error, 0)
EndIf
Local $sRecv = '', $sCurrentRecv
While 1
$sCurrentRecv = TCPRecv($iSocket, 16)
If @error <> 0 Then
ExitLoop
EndIf
If $sCurrentRecv <> '' Then
$sRecv &= $sCurrentRecv
EndIf
WEnd
TCPCloseSocket($iSocket)
TCPShutdown()
Return $sRecv
EndFunc ;==>_HTTPGetResponse
Func _Exit($iFlag = 0)
If $iFlag Then
_WordDocClose($oDoc)
_WordQuit($oWordApp)
FileDelete(@TempDir & '\' & $sFileName)
EndIf
MsgBox(16, 'Error', 'Error')
Exit
EndFunc ;==>_Exit