+ Reply to Thread
Results 1 to 2 of 2

Calling a function inside a sub routine

  1. #1
    Registered User
    Join Date
    10-18-2017
    Location
    USA
    MS-Off Ver
    2007
    Posts
    1

    Calling a function inside a sub routine

    Hello friends,
    After several searches on google, it looks this is one of the best places to get VBA Excel help.
    I am trying to login to different websites automatically. The goal is to create a function that will handle all repetitive tasks and then call it inside the sub routine, so the code for each different website login will be shorter.

    First here is the code that is currently working for one test website "FaceBook":

    Sub OpenFacebook()Dim ie As Object
    Set ie = CreateObject("InternetExplorer.Application")
    ie.navigate Sheets("Sheet1").Cells(2, 5) ' Sheet1, Row 2, column 5 has the Link to FaceBook
    ie.Visible = True
    Do While ie.Busy And Not ie.readyState = 4
    DoEvents
    Loop
    DoEvents
    On Error Resume Next
    ie.document.all.Item("email").Value = Sheets("Sheet1").Range("B2") ' Cell "B2" in Sheet1 has the user name
    ie.document.all.Item("pass").Value = Sheets("Sheet1").Range("C2") ' Cell "C2" in Sheet1 has the password
    ie.document.all.Item("loginbutton").Click
    End Sub

    I tried to create a function/private sub called "OpenLink" that could handle the repetitive tasks as shown below, but it is not working:

    Private Sub OpenLink(ByVal url As String, ByVal isVisible As Boolean)
    Dim ie As Object
    Set ie = CreateObject("InternetExplorer.Application")
    ie.navigate url
    ie.Visible = isVisible

    Do While ie.Busy And Not ie.readyState = 4
    DoEvents
    Loop
    DoEvents
    End Sub

    And then I am including "OpenLink" in the sub routine:

    Sub OpenFacebookTest()
    OpenLink Sheets("Sheet1").Cells(2, 5), True
    On Error Resume Next
    ie.document.all.Item("email").Value = Sheets("Sheet1").Range("B2")
    ie.document.all.Item("pass").Value = Sheets("Sheet1").Range("C2")
    ie.document.all.Item("loginbutton").Click
    End Sub

    When I step through it, it does not show me any errors, but it does not automatically enters the user name and password in the fields and the submit button is not clicked.

    Any help you can provide will be greatly appreciate it.
    Thank you
    Toni Ln.

  2. #2
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: Calling a function inside a sub routine

    The variable ie is not shared between the two procedures. So in the OpenFacebookTest procedure, ie is not defined and the On Error Resume Next is ignoring the error.

    Try this...
    Please Login or Register  to view this content.
    Surround your VBA code with CODE tags e.g.;
    [CODE]your VBA code here[/CODE]
    The # button in the forum editor will apply CODE tags around your selected text.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Calling function inside function. (aka nested function)
    By jakopak in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 11-02-2015, 05:58 AM
  2. Calling Procedure or Function inside another Procedure - variables problem
    By Rudo123 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 05-22-2015, 03:12 PM
  3. [SOLVED] naming a routine and calling it in another routine - not a loop
    By HeyInKy in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-16-2015, 11:54 AM
  4. Calling a Browse routine from a UserForm
    By j_Southern in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 01-27-2015, 06:23 AM
  5. Problem calling routine from a class in an add-in
    By scottbass in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 02-04-2014, 10:07 AM
  6. [SOLVED] VBA Calling Another Sub Routine to Run
    By mowens74 in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 01-15-2013, 12:34 PM
  7. Calling Routine by Variable Name
    By Caligula in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 04-20-2008, 11:44 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1