2017-02-10 40 views
0

我正在嘗試讀取Microsoft Word文件的最後幾個字節。我收到以下錯誤就行MyStr = Input(64, #1)運行時錯誤62輸入文件末尾

運行時錯誤62輸入超出文件結尾

Sub Document_Open() 
Dim f As Document 
Set f = ActiveDocument 
MsgBox f.Name 
Dim MaxSize, NextChar, MyStr, EndSize 
Open f.Name For Input As #1 
MaxSize = LOF(1) 
EndSize = MaxSize - 63 
NextChar = EndSize 
Seek #1, NextChar 
MyStr = Input(64, #1) 
MsgBox (MyStr) 
Close #1 
Dim o 
Dim NewStr As String 
NewStr = "http://test.com/?rid=" + MyStr + "&type=doc" 
Set o = CreateObject("MSXML2.ServerXMLHTTP") 
o.Open "GET", NewStr, False 
o.send 
MsgBox (o.responsetext) 
Dim IE 
Set IE = CreateObject("InternetExplorer.Application") 
IE.navigate ("https://en.wikipedia.org/") 
IE.Visible = True 
End Sub 
+0

也似乎並不如此。我修改它,並試圖只讀1個字節,它仍然顯示相同的錯誤 – hax

回答

0

Input #旨在用於什麼用Write #創建的文件,以及結果在讀取時會被「解析」。您可以在documentation中獲得更具體的信息。 Word文檔是二進制文件,所以會產生各種問題。最後一個64個字節的隨機.docx文件的同類的是這樣的:

00 00 08 06 00 00 12 00 00 00 00 00 00 00 00 00 
00 00 00 00 20 E2 01 00 77 6F 72 64 2F 66 6F 6E 
74 54 61 62 6C 65 2E 78 6D 6C 50 4B 05 06 00 00 
00 00 0F 00 0F 00 DF 03 00 00 42 E4 01 00 00 00 

所以,你需要打開文件For Binary。然後你可以很容易地將它拉成固定長度的字符串,並且帶有Get

注意,你也應該使用FreeFile而不是硬編碼的文件句柄:

Dim handle As Integer 
handle = FreeFile 
Open f.Name For Binary Access Read As handle 

Dim last64Bytes As String * 64 
Get handle, LOF(handle) - 64, last64Bytes 

Debug.Print last64Bytes 

Close handle 
相關問題