Copy and paste the web page data into a notepad - vba

I need to copy open a XML in IE and select the content(Ctrl+A) in the webpage and copy them (Ctrl+c) and paste them in a notepad. Below is the code but it is not working.
Dim ie As Object
Dim ieDoc As Object
Dim Data As String
Set ie = CreateObject("InternetExplorer.Application")
ie.navigate "C:\Data\test_10.xml" ie.Visible = True
Do Until (ie.readyState = 4 And Not ie.Busy)
DoEvents
Loop
SendKeys "^a", True
Application.Wait (5)
SendKeys "^c"
Dim FileNo As Integer
FileNo = FreeFile
Open "C:\Data\Sample.txt" For Output As FileNo
SendKeys "^v", True
Close FileNo

The Open statement doesn't open a Notepad application, it just creates a file handle for Input / Output to a file from within VBA. You need to create a notepad application object similar to the way you create the IE application object.
Also consider avoiding SendKeys all together. Instead
read out the data from the IE object into a string variable using the InnerHTML property
write out the string into a flat file using Open / Write
optionally re-open the text file in the notepad application

Try this:
Sub pExtractXMLData()
Dim strURLtoNavigate As String
Dim strHTML As String
strURLtoNavigate = "C:\Data\test_10.xml"
strHTML = UsingXmlParser(strURLtoNavigate)
Call WriteVarToDisk(strHTML, "C:\Data\Sample.txt")
End Sub
Public Function UsingXmlParser(strUrl As String)
Dim objxmlhttp As Object
Set objxmlhttp = CreateObject("MSXML2.XMLHTTP")
objxmlhttp.Open "GET", strUrl, False
objxmlhttp.send
'objxmlhttp.WaitForResponse
UsingXmlParser = objxmlhttp.ResponseText
Set objxmlhttp = Nothing
End Function
Public Sub WriteVarToDisk(vartowrite, FiletoWrite)
On Error Resume Next
Dim fso, MyFile
Set fso = CreateObject("Scripting.FileSystemObject")
Set MyFile = fso.CreateTextFile(FiletoWrite, True)
MyFile.WriteLine (vartowrite)
MyFile.Close
End Sub

Try this .. you can open the notepad in excel. do all the works and save back as notepad..
Below codes will help you.
Sub ImportXMLtoList()
Dim strTargetFile As String
Dim wb as Workbook
dim dwb as workbook
Application.Screenupdating = False
Application.DisplayAlerts = False
strTargetFile = "C:\Data\test_10.xml"
Set wb = Workbooks.OpenXML(Filename:=strTargetFile,LoadOption:=xlXmlLoadImportToList)
Application.DisplayAlerts = True
wb.Sheets(1).UsedRange.Copy
set dwb = workbooks.open("C:\Data\Sample.txt")
dwb.activesheet.range("A1").PasteSpecial xlPasteValues
dwb.close true
wb.Close False
Application.Screenupdating = True
End Sub

Related

Excel VBA Can't Find a xlsx File in The Folder

Hi I have a code and it pops up a massage box with
run time error 1004
that it can't find the file that I want to open. How can I handle it? I got stuck in it already for hours and I can't find what was going wrong. I want to open an XLSX file that is saved in the same folder with the macro file and they are the only files in the folder, can someone help?
Private Sub cmdStartMonth_Click()
'Optimize Macro Speed
Application.ScreenUpdating = False
Application.EnableEvents = False
'Analyze month by selecting
Dim myPath As String
Dim FileName As String
Dim UnionWB As Workbook
Dim MonthName As String
MonthName = ListMonth.Value
myExtension = "*.xlsx*"
Dim sWrkbkPath As String
sWrkbkPath = Dir$(ThisWorkbook.Path & "\*.xlsx")
'Only expecting a single file so no need to loop.
If sWrkbkPath <> "" Then
Set UnionWB = Workbooks.Open(sWrkbkPath)
End If
UnionWB.Worksheets("Union").Range("A1:Y1").AutoFilter
With UnionWB.Worksheets("Union")
.Activate
.Range("L1:W1").Find(MonthName, , xlValues, xlWhole).Activate
End With
Application.ScreenUpdating = True
Application.EnableEvents = True
End Sub
that is the problematic row in the code:
Set UnionWB = Workbooks.Open(sWrkbkPath)
Change this
Set UnionWB = Workbooks.Open(sWrkbkPath)
to this
Set UnionWB = Workbooks.Open(Thisworkbook.Path & "\" & sWrkbkPath)

Print/Import all web page source data using vba

I have code below which imports only part of source code into sheet. I want all source code as it is.`Sub GetSourceCode()
Dim ie As Object
Dim str As String
Dim arr
str = Sheets("sheet2").Range("I1").Value
Set ie = CreateObject("INTERNETEXPLORER.APPLICATION")
ie.Navigate "https://tiweb.industrysoftware.automation.com/prdata/cgi-bin/n_prdata_index.cgi?"
ie.Visible = False
Do Until ie.ReadyState = 4
DoEvents
Loop
ie.Document.getelementsbyname("pr_numbers")(0).Value = str
Application.SendKeys ("~")
Do Until ie.ReadyState = 4
DoEvents
Loop
Worksheets("Download_PRdata2").Activate
arr = Split(ie.Document.body.outertext)
Worksheets("Download_PRdata2").Activate
ActiveSheet.Range("A1").Resize(UBound(arr) + 1, 1).Value = Application.Transpose(arr)
End Sub`
Hi you can refer the below code
' Fetch Entire Source Code
Private Sub HTML_VBA_Excel()
Dim oXMLHTTP As Object
Dim sPageHTML As String
Dim sURL As String
'Change the URL before executing the code
sURL = "http://www.google.com"
'Extract data from website to Excel using VBA
Set oXMLHTTP = CreateObject("MSXML2.ServerXMLHTTP")
oXMLHTTP.Open "GET", sURL, False
oXMLHTTP.send
sPageHTML = oXMLHTTP.responseText
'Get webpage data into Excel
' If longer sourcecode mean, you need to save to a external text file or somewhere,
' since excel cell have some limits on storing max characters
ThisWorkbook.Sheets(1).Cells(1, 1) = sPageHTML
MsgBox "XMLHTML Fetch Completed"
End Sub
Source : http://www.vbausefulcodes.in/usefulcodes/get-data-or-source-code-from-webpage-using-excel-vba.php
Hope this will be useful to you!
you can save source code in a text file like this. add the below function instead of this line ThisWorkbook.Sheets(1).Cells(1, 1) = sPageHTML
Createtextfile (sPageHTML)
and add this below function after End Sub.
Sub Createtextfile(sPageHTML)
Dim fso As Object
Set fso = CreateObject("Scripting.FileSystemObject")
Dim oFile As Object
strPath = "E:\test.txt"
Set oFile = fso.Createtextfile(strPath)
oFile.WriteLine sPageHTML
oFile.Close
Set fso = Nothing
Set oFile = Nothing
End Sub
Change the location where you want to save.

VBA - How to download .xls from website and put data into excel file

I managed to use VBA to get to the point where I'm ready to download an excel file from the web but I'm having trouble figuring out how to actually download that file and put its contents into an excel file I'm working in. Any suggestions? Thanks
Here is the code so far:
Sub GetData()
Dim IE As InternetExplorer
Dim HTMLDoc As HTMLDocument
Dim objElement As HTMLObjectElement
Set IE = New InternetExplorer
With IE
.Visible = True
.Navigate "http://www.housepriceindex.ca/default.aspx"
While .Busy Or .ReadyState <> READYSTATE_COMPLETE: Wend
.Document.getElementById("lnkTelecharger2").Click
While .Busy Or .ReadyState <> READYSTATE_COMPLETE: Wend
Set HTMLDoc = .Document
Set objElement = HTMLDoc.getElementById("txtEmailDisclaimerEN")
objElement.Value = "abc#abc.com"
Set objElement = HTMLDoc.getElementById("lnkAcceptDisclaimerEN")
objElement.Click
' ... Get CSV somehow ...
'.Quit
End With
Set IE = Nothing
End Sub
Try the below code:
Option Explicit
Sub ImportHistoricalDataSheet()
Const SXH_SERVER_CERT_IGNORE_ALL_SERVER_ERRORS = 13056
Const adSaveCreateOverWrite = 2
Dim aBody, sPath
' Download Historical Data xls file via XHR
With CreateObject("MSXML2.XMLHTTP")
'With CreateObject("MSXML2.ServerXMLHTTP")
'.SetOption 2, SXH_SERVER_CERT_IGNORE_ALL_SERVER_ERRORS
.Open "GET", "http://www.housepriceindex.ca/Excel2.aspx?langue=EN&mail=abc%40abc.com"
.Send
' Get binary response content
aBody = .responseBody
' Retrieve filename from headers and concatenate full path
sPath = ThisWorkbook.Path & "\" & Replace(Split(Split(.GetAllResponseHeaders, "filename=", 2)(1), vbCrLf, 2)(0), "/", "-")
End With
' Save binary content to the xls file
With CreateObject("ADODB.Stream")
.Type = 1
.Open
.Write aBody
.SaveToFile sPath, adSaveCreateOverWrite
.Close
End With
' Open saved workbook
With Workbooks.Open(sPath, , True)
' Get 1st worksheet values to array
aBody = .Worksheets(1).UsedRange.Value
.Saved = True
.Close
End With
' Delete saved workbook file
CreateObject("Scripting.FileSystemObject").DeleteFile sPath, True
' Insert array to target worksheet
ThisWorkbook.Sheets("Sheet1").Cells(1, 1).Resize(UBound(aBody, 1), UBound(aBody, 2)).Value = aBody
End Sub

File loop is wrongfully skipping files

I have written the below code to loop through all the files in a directory and copy certain values from them and paste it back into the master file.
The problem that I am having is that the code never fully runs through all the files and I never get an error.
As shown in the picture the file names are depicted as 1 - #####, then 2 - ####, etc.
Sometimes there are multiple of the first number like in the picture there are two 1 - ###'s but the end numbers are still different.
The problem is that instead of going by the actual numerical order the code is using only the first number and going from 1, 10, 11, 100 and completely skipping the rest.
Any ideas on how to solve this?
Sub ReadDataFromCloseFile()
On Error GoTo ErrHandler
Application.EnableEvents = False
Application.ScreenUpdating = False
Dim FileType As String
Dim FilePath As String
FileType = "*.xlsm*" 'The file type to search for
FilePath = "\\filepath\" 'The folder to search
Dim src As Workbook
Dim OutputCol As Variant
Dim Curr_File As Variant
OutputCol = 9 'The first row of the active sheet to start writing to
Curr_File = Dir(FilePath & FileType)
Do Until Curr_File = ""
' OPEN THE SOURCE EXCEL WORKBOOK IN "READ ONLY MODE".
Set src = Workbooks.Open(FilePath & Curr_File, True, True)
Sheets("Reporting").Range("I7:I750").Copy
Workbooks("Master.xlsm").Activate
Sheets("Sheet2").Select
Sheets("Sheet2").Cells(4, OutputCol).Select
ActiveCell.PasteSpecial Paste:=xlPasteValuesAndNumberFormats
Application.CutCopyMode = False
OutputCol = OutputCol + 1
' CLOSE THE SOURCE FILE.
src.Close False ' FALSE - DON'T SAVE THE SOURCE FILE.
Curr_File = Dir
Loop
Set src = Nothing
Application.EnableEvents = True
Application.ScreenUpdating = True
ErrHandler:
Application.EnableEvents = True
Application.ScreenUpdating = True
End Sub
Filepath
I've no idea why it doesn't open the 2 to 9 files. This version puts all the file paths into a collection and then steps through the collection.
It also does away with selecting the sheets before pasting, etc.
Sub ReadDataFromCloseFile()
Dim FileType As String
Dim FilePath As String
Dim colFiles As Collection
Dim src As Workbook
Dim tgt As Workbook
Dim OutputCol As Variant
Dim Curr_File As Variant
Set colFiles = New Collection
FileType = "*.xlsm*" 'The file type to search for
FilePath = "\\filepath\" 'The folder to search
EnumerateFiles FilePath, FileType, colFiles
OutputCol = 9 'The first row of the active sheet to start writing to
'If Master.xlsm is the book containing this code then use '=ThisWorkbook'
Set tgt = Workbooks("Master.xlsm")
For Each Curr_File In colFiles
Set src = Workbooks.Open(Curr_File, True, True)
src.Worksheets("Reporting").Range("I7:I750").Copy
tgt.Worksheets("Sheet2").Cells(4, OutputCol).PasteSpecial xlPasteValuesAndNumberFormats
OutputCol = OutputCol + 1
src.Close False
Next Curr_File
End Sub
Sub EnumerateFiles(ByVal sDirectory As String, _
ByVal sFileSpec As String, _
ByRef cCollection As Collection)
Dim sTemp As String
sTemp = Dir$(sDirectory & sFileSpec)
Do While Len(sTemp) > 0
cCollection.Add sDirectory & sTemp
sTemp = Dir$
Loop
End Sub
It might be easier for you to loop through the files using the below loop.
Sub LoopFiles()
Dim FSO As New FileSystemObject
Dim Fldr As Folder
Dim Fl As File
'Loop through files in folder
For Each Fl In FSO.GetFolder(filePath).Files
'Check for file type
If Fl.Type = "Excel Macro-Enabled Workbook" Then
'Open file & do procedure
End If
Next
Set FSO = Nothing
End Sub
Try declaring your variable OutputCol as an integer and not a variant. If you know your data is always going to be a number, it's never a good idea to use a variant. It takes more resources to execute the code and you don't know all of the internal logic that is going on behind the scenes. It also gives you more control of the code's execution and probably won't give you headaches like this one. Only use a variant if you don't know what data you are going to output.
Hope this helps!

Search column for urls, save webpages as individual text files

I have code here that works for a url that is hard coded, and it only works for one url and one text file.
Sub saveUrl_Test()
Dim FileName As String
Dim FSO As Object
Dim ieApp As Object
Dim Txt As String
Dim TxtFile As Object
Dim URL As String
URL = "www.bing.com"
FileName = "C:\mallet\bing.com.txt"
Set FSO = CreateObject("Scripting.FileSystemObject")
Set TxtFile = FSO.OpenTextFile(FileName, 2, True, -1)
Set ieApp = CreateObject("InternetExplorer.Application")
ieApp.Visible = True
ieApp.Navigate URL
While ieApp.Busy Or ieApp.ReadyState <> 4
DoEvents
Wend
Txt = ieApp.Document.body.innerText
TxtFile.Write Txt
TxtFile.Close
ieApp.Quit
Set ieApp = Nothing
Set FSO = Nothing
End Sub
What I want it to do is search in column B for urls (possibly using InStr(variable, "http://") as a boolean), and then save each webpage as an individual text file. Would there be a way to name the text files using part of the URL strings? Also, is there a way for the webpage not to open, but still save as a text file? Opening the webpages wastes a lot of time.
I created this additional sub based on #MikeD's suggestion, but I get the wend without while error.
Sub url_Test(URL As String, FileName As String)
Dim FSO As Object
Dim ieApp As Object
Dim Txt As String
Dim TxtFile As Object
Set FSO = CreateObject("Scripting.FileSystemObject")
Set TxtFile = FSO.OpenTextFile(FileName, 2, True, -1)
Set ieApp = CreateObject("InternetExplorer.Application")
ieApp.Visible = True
ieApp.Navigate URL
While ieApp.Busy Or ieApp.ReadyState <> 4
DoEvents
Wend
Txt = ieApp.Document.body.innerText
TxtFile.Write Txt
TxtFile.Close
ieApp.Quit
Set ieApp = Nothing
Set FSO = Nothing
End Sub
Sub LoopOverB()
Dim myRow As Long
myRow = 10
While Cells(myRow, 2).Value <> ""
If InStr(1, Cells(myRow, 2).Value, "http:\\", vbTextCompare) Then Call url_Test(Cells(myRow, 2).Value, "C:\mallet\test\" & Cells(myRow, 1).Value & ".txt")
myRow = myRow + 1
Wend
End Sub
First you could parameterize the sub
Sub saveUrl_param(URL as String, FileName as String)
....
End Sub
and remove the Dim and assignment statements for URL and FileName
Secondly you write another Sub which loops through non-empty cells in column B, retrieving values and conditionally calling the saveUrl_param() routine.
example:
Sub LoopOverB()
Dim C As Range
For Each C In Intersect(ActiveSheet.Columns("B"), ActiveSheet.UsedRange).SpecialCells(xlCellTypeConstants)
' If C = .... Then ' note: URL in [B], filename in [C]
' saveUrl_param(C, C(1,2))
' End If
Next C
End Sub
and no - you can't do it without opening the Web page; you somehow have to get the page from the server (or the proxy). This is done by
ieApp.Navigate URL
and the following While ... Wend construct waits until the page is fully loaded into the browser object.
To speed up things you could skip
ieApp.Visible = True
once you have confidence that your Sub is working correctly, and you could move
Dim ieApp As Object ' I would prefer As SHDocVw.InternetExplorer .... don't like late binding
Set ieApp = CreateObject("InternetExplorer.Application")
to the calling sub and hand over the ieApp object to the subroutine as a parameter in order to not open/close the browser again & again.