Ask Question Forum:
Model Library:2025-02-08 Updated:A.I. model is online for auto reply question page
C
O
M
P
U
T
E
R
2
8
Show
#
ASK
RECENT
←
- Underline
- Bold
- Italic
- Indent
- Step
- Bullet
- Quote
- Cut
- Copy
- Paste
- Table
- Spelling
- Find & Replace
- Undo
- Redo
- Link
- Attach
- Clear
- Code
Below area will not be traslated by Google,you can input code or other languages
Hint:If find spelling error, You need to correct it,1 by 1 or ignore it (code area won't be checked).
X-position of the mouse cursor
Y-position of the mouse cursor
Y-position of the mouse cursor
Testcursor
caretPos
Attachment:===
Asked by duncanb7
at 2024-07-17 04:31:44
Point:500 Replies:12 POST_ID:828515USER_ID:11059
Topic:
Visual Basic Programming;;Windows XP Operating System
I get one text file from remote website and its file size is 50K and I try to
open with follow code in VBA
workbooks.open "http://www.example.com/getData" ,false
and excel will open the file and stored it into A1 cell in sheet1, and then I will decode or extract
A1 value into my format by VBA. if the getData file is too big, the Excel's A1 cell
could not store all data and it will split into A1, B1 or C1 that is hard for
me to decode because I would like the data is continuous into one buffer or cell
So I try think why not putting it into memory and then reference to one VBA variable without
opening the getData file into Excel ?
But I don't know how ?
Please advise
Duncan
open with follow code in VBA
workbooks.open "http://www.example.com/getData" ,false
and excel will open the file and stored it into A1 cell in sheet1, and then I will decode or extract
A1 value into my format by VBA. if the getData file is too big, the Excel's A1 cell
could not store all data and it will split into A1, B1 or C1 that is hard for
me to decode because I would like the data is continuous into one buffer or cell
So I try think why not putting it into memory and then reference to one VBA variable without
opening the getData file into Excel ?
But I don't know how ?
Please advise
Duncan
Author: duncanb7 replied at 2024-07-18 11:42:20
Thanks for your quick reply,
finally I pick your #3 and it solved
my issue completely
Duncan
finally I pick your #3 and it solved
my issue completely
Duncan
Assisted Solution
Expert: Atradius replied at 2024-07-18 10:57:37
100 points EXCELLENT
Sorry, forgot to mention that.
You will need to include a reference to following library:
Microsoft WinHTTP Services, version 5.1
Just from Tools > References menu as usual.
You will need to include a reference to following library:
Microsoft WinHTTP Services, version 5.1
Just from Tools > References menu as usual.
Author: duncanb7 replied at 2024-07-17 08:25:29
I gave up the OpenText and switch to use downloadfile()
but I got VBA runtime error, on Dim oHTTP As WinHttp.WinHttpRequest
if I use HTTP protocol; in VBA , do I need to include some library or dll file in VBA editor before using , Please advise
And I am using a lot of myie.navigate "http:www.yahoo.com" in VBA, why I still have such error
Mcirosoft Internet Controls library is already in my VBA
but I got VBA runtime error, on Dim oHTTP As WinHttp.WinHttpRequest
if I use HTTP protocol; in VBA , do I need to include some library or dll file in VBA editor before using , Please advise
And I am using a lot of myie.navigate "http:www.yahoo.com" in VBA, why I still have such error
Mcirosoft Internet Controls library is already in my VBA
Assisted Solution
Expert: Atradius replied at 2024-07-17 07:09:43
100 points EXCELLENT
In that case, I would advise you to use the function I described under #3. Just cut and paste that function into your VBA module.
Public Function DownloadTextFile(url As String) As String
.....
Then call it from your code.
The result is a text string which you can then manipulate all you want.
Easiest will be to make a little loop to fill the cells, using the "|" as delimiter.
Are you familiar with that?
Little sample code ...
Dim i As Integer
Dim s As String
s = DownloadTextFile(yourURLtoDownloadHere)
Dim val As String
Dim pos1 As Integer
Dim pos2 As Integer
pos1 = 1
pos2 = 1
Do While pos2 > 0
pos2 = InStr(pos1, s, "|")
If (pos2 > 0) Then
val = Mid(s, pos1, pos2 - pos1)
Else
val = Mid(s, pos1)
End If
Range("A1").Offset(i, 0).Value = val
If (pos2 > 0) Then
i = i + 1
pos1 = pos2 + 1
If pos1 > Len(s) Then
Exit Do
End If
End If
Loop
Hope that helps
Author: duncanb7 replied at 2024-07-17 06:36:39
Since number of seperate of "|" is more than 1000
Author: duncanb7 replied at 2024-07-17 06:35:36
The text file format is like
AB|1234|124|ER|SDF|
I want excel can parse the data text file by seperator of "|" and show it in the sheet 1
as follows
AB
1234
124
ER
SDF
I try to use OpenText, workbooks.OpenText filename:="http://www.example.com/getData", _
DataType=xldelimited, Other:=True, OtherChar:="|"
IT works but it show and parsing the data as this follows
AB 1234 ER SDF
How I can parsing the data and spreading the result into Row direction NOT column direction
Please advise
Duncan
AB|1234|124|ER|SDF|
I want excel can parse the data text file by seperator of "|" and show it in the sheet 1
as follows
AB
1234
124
ER
SDF
I try to use OpenText, workbooks.OpenText filename:="http://www.example.com/getData", _
DataType=xldelimited, Other:=True, OtherChar:="|"
IT works but it show and parsing the data as this follows
AB 1234 ER SDF
How I can parsing the data and spreading the result into Row direction NOT column direction
Please advise
Duncan
Assisted Solution
Expert: Atradius replied at 2024-07-17 06:32:11
100 points EXCELLENT
Sorry - I cut the source code with my solution # 4 - here is the complete code for #3:
Public Function DownloadTextFile(url As String) As String
Dim oHTTP As WinHttp.WinHttpRequest
Set oHTTP = New WinHttp.WinHttpRequest
oHTTP.Open Method:="GET", url:=url, async:=False
oHTTP.setRequestHeader "User-Agent", "MSIE 6.0"
oHTTP.setRequestHeader "Content-Type", "multipart/form-data; "
oHTTP.Option(WinHttpRequestOption_EnableRedirects) = True
oHTTP.send
Dim success As Boolean
success = oHTTP.waitForResponse()
If Not success Then
Debug.Print "DOWNLOAD FAILED!"
Exit Function
End If
Dim responseText As String
responseText = oHTTP.responseText
Set oHTTP = Nothing
DownloadTextFile = responseText
End Function
Public Function DownloadTextFile(url As String) As String
Dim oHTTP As WinHttp.WinHttpRequest
Set oHTTP = New WinHttp.WinHttpRequest
oHTTP.Open Method:="GET", url:=url, async:=False
oHTTP.setRequestHeader "User-Agent", "MSIE 6.0"
oHTTP.setRequestHeader "Content-Type", "multipart/form-data; "
oHTTP.Option(WinHttpRequestOption_EnableRedirects) = True
oHTTP.send
Dim success As Boolean
success = oHTTP.waitForResponse()
If Not success Then
Debug.Print "DOWNLOAD FAILED!"
Exit Function
End If
Dim responseText As String
responseText = oHTTP.responseText
Set oHTTP = Nothing
DownloadTextFile = responseText
End Function
Assisted Solution
Expert: Atradius replied at 2024-07-17 06:26:53
100 points EXCELLENT
Okay, my error, of course it is not going to work that easily from a web page.
Few things you could do.
(1) First you can change the column width to accomodate large files, using
the .columnWidth property of that cell.
Of course, that's a limited way to do it.
(2) Use the command:
Application.Workbooks.OpenText "http://www.google.com/"
This loads the various lines of the web page into SEVERAL lines, and may
be just what you need.
(3) If not good enough, you could write your own http request handler.
Sample function which should work here below
Public Function DownloadTextFile(url As String) As String
Dim oHTTP As WinHttp.WinHttpRequest
Set oHTTP = New WinHttp.WinHttpRequest
oHTTP.Open Method:="GET", url:=url, async:=False
oHTTP.setRequestHeader "User-Agent", "MSIE 6.0"
oHTTP.setRequestHeader "Content-Type", "multipart/form-data; "
oHTTP.Option(WinHttpRequestOption_EnableRedirects) = True
oHTTP.send
Dim success As Boolean
success = oHTTP.waitForResponse()
If Not success Then
Debug.Print "DOWNLOAD FAILED!"
Exit Function
End If
(4) You can also download "wget.exe" for Windows (browse for it in Google) and run this via a shell object in VBA. It will download the web page into a file. You can get that file then via the FreeFile / Input method.
But #3 above will be easier and faster.
Dim responseText As String
responseText = oHTTP.responseText
Set oHTTP = Nothing
DownloadTextFile = responseText
End Function
Author: duncanb7 replied at 2024-07-17 05:59:52
Error is 52
Author: duncanb7 replied at 2024-07-17 05:58:50
and nSouceFile=1 return from nSouceFile=FreeFile
Author: duncanb7 replied at 2024-07-17 05:41:48
But I try your method and fail and it report the error as the file name or number is not correct.
Could I use open method for URL link in website ?
Dim nSourceFile As Integer, sText As String
'Close any open text files
Close
'Get the number of the next free text file
nSourceFile = FreeFile
Dim sfile as string
sfile="http://www.example.com/getData"
'Write the entire file to sText
Open sFile For Input As #nSourceFile
sText = Input$(LOF(1), 1)
Close
Could I use open method for URL link in website ?
Dim nSourceFile As Integer, sText As String
'Close any open text files
Close
'Get the number of the next free text file
nSourceFile = FreeFile
Dim sfile as string
sfile="http://www.example.com/getData"
'Write the entire file to sText
Open sFile For Input As #nSourceFile
sText = Input$(LOF(1), 1)
Close
Accepted Solution
Expert: Atradius replied at 2024-07-17 05:25:20
100 points EXCELLENT
You can use FreeFile and Input$ to get the text file,
something like this:
Dim nSourceFile As Integer, sText As String
'Close any open text files
Close
'Get the number of the next free text file
nSourceFile = FreeFile
'Write the entire file to sText
Open sFile For Input As #nSourceFile
sText = Input$(LOF(1), 1)
Close
something like this:
Dim nSourceFile As Integer, sText As String
'Close any open text files
Close
'Get the number of the next free text file
nSourceFile = FreeFile
'Write the entire file to sText
Open sFile For Input As #nSourceFile
sText = Input$(LOF(1), 1)
Close