Open URL without Webbrowser or Inet control vb6 - web

Is there a way to open a URL in VB6 application without using Webbrowser or MSInet components?
thanks

If you just want to open the URL in a browser window, then use ShellExecute: http://support.microsoft.com/kb/224816
Private Declare Function ShellExecute _
Lib "shell32.dll" _
Alias "ShellExecuteA"( _
ByVal hwnd As Long, _
ByVal lpOperation As String, _
ByVal lpFile As String, _
ByVal lpParameters As String, _
ByVal lpDirectory As String, _
ByVal nShowCmd As Long) _
As Long
Private Sub Command1_Click()
Dim r As Long
r = ShellExecute(0, "open", "http://www.microsoft.com", 0, 0, 1)
End Sub
This will open the URL in the default browser.
Otherwise, if you need to display the webpage inside your app, use the WebBrowser control.

No. VB6 does not have any intrinsic means of displaying a web page in an application. You have to use a third party control. On the other hand, this shouldn't be a problem, because you are essentially using a component of Microsoft Internet Explorer. In fact, you should not be distributing this control, because you would likely damage the end user's Windows installation.

Related

Download Excel file from Sharepoint, Save to Desktop

I am trying to download an excel file stored in sharepoint (no unc path available) to my desktop.
This code below seems to work and create "CST.xlsx" but I get an error msg:
Notes:
Url for excel file is taken directly from the web address bar and everthing after ".xlsx" is removed.
Code:
Private Declare Function URLDownloadToFile Lib "urlmon" Alias "URLDownloadToFileA" (ByVal pCaller As Long, _
ByVal szURL As String, ByVal szFileName As String, ByVal dwReserved As Long, ByVal lpfnCB As Long) As Long
Function DownloadFileFromWeb(strURL As String, strSavePath As String) As Long
' strSavePath includes filename
DownloadFileFromWeb = URLDownloadToFile(0, strURL, strSavePath, 0, 0)
End Function
Sub download()
Call DownloadFileFromWeb("url.xlsx", "Desktop\download\CST.xlsx")
End Sub
See my answer in this thread about the same problem for a workaround.

Save webpage concluded instead HTML only

I'm currently using a VBA code in a spreadsheet to download a webpage, the issue is that this is downloading only the html page, without the folder that is downloaded when we go to the browser and select "Save As".
Below I'm showing the explanation with images - I would like to know if is possible to, directly from VBA, download the concluded page (with folder that comes with images, etc.) instead only the HTML.
Private Declare PtrSafe Function URLDownloadToFile Lib "urlmon" _
Alias "URLDownloadToFileA" (ByVal pCaller As Long, ByVal szURL As String, _
ByVal szFileName As String, ByVal dwReserved As Long, ByVal lpfnCB As Long) As Long
Sub datafeed()
src = "https://google.com"
dlpath = "C:\Users\User\Downloads\Test\"
URLDownloadToFile 0, src, dlpath & "test.html", 0, 0
End Sub
How is currently downloading:
How I would like to download (like when I select Save As in the browser):
Does someone know how to configure VBA to download the webpage and the folder with the files instead only the webpage?

Downloading PDF document in Chrome using VBA Selenium

I currently need to download multiple PDF files from a specific website.
Once I reach to this point the usual action is to click on the save button or to type "CTRL + S"
Click on Save button
I retrieved this on this post of IE Automation
But I'm trying to excecute the download action with the following code:
bot.SendKeys Keys.Control, "s"
And is not working.
How can I make this work on Chrome?
Thanks,
You could use this to download the pdfs.
Dim PDF_URL as string
#If VBA7 Then
Private Declare PtrSafe Function URLDownloadToFile Lib "urlmon" Alias "URLDownloadToFileA" ( _
ByVal pCaller As LongPtr, _
ByVal szURL As String, _
ByVal szFileName As String, _
ByVal dwReserved As LongPtr, _
ByVal lpfnCB As LongPtr) As LongPtr
#Else
Private Declare Function URLDownloadToFile Lib "urlmon" Alias "URLDownloadToFileA" ( _
ByVal pCaller As Long, _
ByVal szURL As String, _
ByVal szFileName As String, _
ByVal dwReserved As Long, _
ByVal lpfnCB As Long) As Long
#End If
Private Sub File_Download()
Dim FileURL As String
Dim DestinationFile As String
FileURL = PDF_URL
DestinationFile = "C:\PDFs\PDFName.pdf"
If URLDownloadToFile(0, FileURL, DestinationFile, 0, 0) = 0 Then
Debug.Print "File downloaded started"
Else
Debug.Print "File downloaded not started"
End If
End Sub
Private Sub GetURL
PDF_URL = Chrome_Driver.FindElementById("EleID").Attribute("src")
File_Download
end sub
There are a ton of websites that may not allow bots. You should go to the website's /robots.txt to see if there are any restrictions on bots pulling the content you are trying to grab. These sections will show "disallow" anything bots are forbidden to access.
example: google/robots.txt
Yes, websites can even tell if you are using Selenium.

printing specific pages form pdf file using print scope string

I need to print selected pages of *.pdf file using excel VBA.
I need to do this not by providing scope "from - to" but giving scoope of pages like "1-3,4,8, 17-25"
I can only print whole file using below code:
Public Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" ( _
ByVal hwnd As Long, _
ByVal lpOperation As String, _
ByVal lpFile As String, _
ByVal lpParameters As String, _
ByVal lpDirectory As String, _
ByVal nShowCmd As Long) As Long
Public Function PrintThisDoc(formname As Long, FileName As String)
On Error Resume Next
Dim X As Long
X = ShellExecute(formname, "Print", FileName, 0&, 0&, 3)
End Function
Sub testPrint()
Dim printThis
Dim strDir As String
Dim strFile As String
strDir = "C:\Users\any\Desktop"
strFile = "somefile.pdf"
printThis = PrintThisDoc(0, strDir & "\" & strFile)
End Sub ```
Judging from this post, it is not possible using ShellExecute alone. What you would need is to write a script which will open the file in whatever PDF reader you have installed, find a "Print" button there and provide all the options. This will take a huge amount of work and will depend on the version of Windows and the PDF reader, so basically it will work only for you, and not for other users.
If you want to go this path though, take a look at FindWindow and SendMessage WinAPI functions.

Use image in Userform Caption

I am working on a UserForm and I am trying to use an IE/Chrome etc logo in the caption property of the UserForm so that the logo is displayed in the window frame followed by some text.
I have done some browsing and have found the following code online but I get an sub/function not defined error on the line involving ExtractIcon.
UserForm Code
Private Sub UserForm_Initialize
Dim strIconPath As String
Dim lngIcon As Long
Dim lnghWnd As Long
' Change to the path and filename of an icon file
strIconPath = "C:\Users\suttond\Desktop\Picture2.gif"
' Get the icon from the source
lngIcon = ExtractIcon(0, strIconPath, 0)
' Get the window handle of the userform
lnghWnd = FindWindow("ThunderDFrame", UserForm1.Caption)
'Set the big (32x32) and small (16x16) icons
SendMessage lnghWnd, WM_SETICON, True, lngIcon
SendMessage lnghWnd, WM_SETICON, False, lngIcon
End Sub
Module Code
Private Declare Function FindWindow _
Lib "user32" Alias "FindWindowA" _
(ByVal lpClassName As String, _
ByVal lpWindowName As String) As Long
Private Declare Function ExtractIcon _
Lib "shell32.dll" Alias "ExtractIconA" _
(ByVal hInst As Long, _
ByVal lpszExeFileName As String, _
ByVal nIconIndex As Long) As Long
Private Declare Function SendMessage _
Lib "user32" Alias "SendMessageA" _
(ByVal hWnd As Long, _
ByVal wMsg As Long, _
ByVal wParam As Integer, _
ByVal lParam As Long) As Long
Private Const WM_SETICON = &H80
Essentially a small IE Explorer logo would display to the left of text already in the caption.
Edit
Module code functions updated to public to allow them to be called from the initialize code. No longer getting the extract error but the image is not appearing in the UserForm caption.
As Mistella and Rory mentioned in the comments of your questions, the functions and the constant needs to be declared as Public. If you declare them as Private, they are only known within the Module, but not in the Form.
Second thing is you need to read the Icon from an ICO-file, not from a gif, so you need to convert it. I use IrfanView for tasks like this, but there are tons of tools (even online) available. I did a quick test and it worked:

Resources