Share via

Open Web Link using VBA

Anonymous
2014-04-29T18:37:10+00:00

Hi,

I am trying to set a Macro to Load a WebLink/WebSite/URL, I would like the Browser to Open in Maximized size.

I have the following Code, but not sure if it is right.

Please can you check my Code and let me know the best way to Open a URL in Maximized?

Here is my Code:

Private Sub Microsoft_Click()

    Set Browser = CreateObject("InternetExplorer.Application")

    Browser.Navigate ("http://microsoft.com")

    Browser.Visible = True

End Sub

Thank you in advance,

Neil

Microsoft 365 and Office | Word | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

Andreas Killer 144.1K Reputation points Volunteer Moderator
2014-04-30T07:05:38+00:00

Compile error:

Only commands may appear after End Sub, End Function, or End Property

Declarations must be at the top of the module, you have placed the declarations after sub SetRef, move that sub to the bottom of the module.

Andreas.

Was this answer helpful?

0 comments No comments

3 additional answers

Sort by: Most helpful
  1. Anonymous
    2014-04-30T08:13:05+00:00

    Hi Andreas,

    Thanks, it works a treat.

    Yours,

    Neil

    Was this answer helpful?

    0 comments No comments
  2. Anonymous
    2014-04-29T22:11:56+00:00

    Hi Andreas,

    I copied your code, and I and getting the following error message:

    Compile error:

    Only commands may appear after End Sub, End Function, or End Property

    I will also need to set other subs to refer to other website, will I just add these after the link for the Microsoft one?

    Here is the code as I placed it and set [BOLDITALIC] where the Error message is Highlighted:

    Option Explicit

    Sub SetRef()

        On Error Resume Next

        'Microsoft Internet Controls

        Application.VBE.ActiveVBProject.References.AddFromGuid _

            "{EAB22AC0-30C1-11CF-A7EB-0000C05BAE0B}", 1, 1

    End Sub

        Private Type POINTAPI

            x As Long

            y As Long

        End Type

        Private Type RECT

            Left As Long

            Top As Long

            Right As Long

            Bottom As Long

        End Type

        Private Type WINDOWPLACEMENT

            Length As Long

            flags As Long

            showCmd As Long

            ptMinPosition As POINTAPI

            ptMaxPosition As POINTAPI

            rcNormalPosition As RECT

        End Type

        Private Const SW_HIDE = 0

        Private Const SW_NORMAL = 1

        Private Const SW_SHOWMINIMIZED = 2

        Private Const SW_SHOWMAXIMIZED = 3

        Private Const SW_MAXIMIZE = 3

        Private Const SW_SHOWNOACTIVATE = 4

        Private Const SW_SHOW = 5

        Private Const SW_MINIMIZE = 6

        Private Const SW_SHOWMINNOACTIVE = 7

        Private Const SW_SHOWNA = 8

        Private Const SW_RESTORE = 9

        Private Const SW_SHOWDEFAULT = 10

        Private Const SW_MAX = 10

        #If Win64 Then

            Private Declare PtrSafe Function GetWindowPlacement Lib "user32" ( _

                ByVal hWnd As LongPtr, lpwndpl As WINDOWPLACEMENT) As Long

            Private Declare PtrSafe Function SetWindowPlacement Lib "user32" ( _

                ByVal hWnd As LongPtr, lpwndpl As WINDOWPLACEMENT) As Long

        #Else

    Private Declare Function GetWindowPlacement Lib "user32" ( _

    ByVal hWnd As Long, lpwndpl As WINDOWPLACEMENT) As Long

            Private Declare Function SetWindowPlacement Lib "user32" ( _

                ByVal hWnd As Long, lpwndpl As WINDOWPLACEMENT) As Long

        #End If

    Sub Microsoft_Click()

        Dim Browser As Object 'SHDocVw.InternetExplorer

        Dim WP As WINDOWPLACEMENT

        'Create the object

        Set Browser = CreateObject("InternetExplorer.Application")

        'Setup the size of our variable

        WP.Length = Len(WP)

        'Get the settings from the window

        GetWindowPlacement Browser.hWnd, WP

        'Show it maximized (and visible)

        WP.showCmd = SW_MAXIMIZE

        'Execute

        SetWindowPlacement Browser.hWnd, WP

        Browser.Navigate ("http://microsoft.com")

    End Sub

    Then add other copies of "Sub Microsoft_Click()"

    Just a feed back I am using Word 2010 under the Windows 7 Operating System.

    I am using "Image (ActiveX Control)" for me to click on Picture to load Website, and these are put within the "ThisDocument".

    Could this be why I am getting the Error?

    Does your code need to be placed within the "Modules"?

    Thanks,

    Neil

    Was this answer helpful?

    0 comments No comments
  3. Andreas Killer 144.1K Reputation points Volunteer Moderator
    2014-04-29T19:25:23+00:00

    The best way? Hmm... no idea.

    I can't see something like a "WindowState" property inside the object, but a HWND property, an usual name for the window handle. And with that handle we can use some API code (see example below).

    BTW, I recommend to declare variables and place the line "Option Explicit" at the top of the module, so you can be sure to avoid typos, etc.

    BTW2, when you want to see/browse the object structure, resp. use the Intellisense, set a reference to "Microsoft Internet Controls" or execute this sub:

    Sub SetRef()

      On Error Resume Next

      'Microsoft Internet Controls

      Application.VBE.ActiveVBProject.References.AddFromGuid _

        "{EAB22AC0-30C1-11CF-A7EB-0000C05BAE0B}", 1, 1

    End Sub

    Then you are also able to develop your code with early binding and if you're done, remove the reference and change it to late binding.

    Andreas.

    Option Explicit

    Private Type POINTAPI

      x As Long

      y As Long

    End Type

    Private Type RECT

      Left As Long

      Top As Long

      Right As Long

      Bottom As Long

    End Type

    Private Type WINDOWPLACEMENT

      Length As Long

      flags As Long

      showCmd As Long

      ptMinPosition As POINTAPI

      ptMaxPosition As POINTAPI

      rcNormalPosition As RECT

    End Type

    Private Const SW_HIDE = 0

    Private Const SW_NORMAL = 1

    Private Const SW_SHOWMINIMIZED = 2

    Private Const SW_SHOWMAXIMIZED = 3

    Private Const SW_MAXIMIZE = 3

    Private Const SW_SHOWNOACTIVATE = 4

    Private Const SW_SHOW = 5

    Private Const SW_MINIMIZE = 6

    Private Const SW_SHOWMINNOACTIVE = 7

    Private Const SW_SHOWNA = 8

    Private Const SW_RESTORE = 9

    Private Const SW_SHOWDEFAULT = 10

    Private Const SW_MAX = 10

    #If Win64 Then

    Private Declare PtrSafe Function GetWindowPlacement Lib "user32" ( _

        ByVal hWnd As LongPtr, lpwndpl As WINDOWPLACEMENT) As Long

    Private Declare PtrSafe Function SetWindowPlacement Lib "user32" ( _

        ByVal hWnd As LongPtr, lpwndpl As WINDOWPLACEMENT) As Long

    #Else

    Private Declare Function GetWindowPlacement Lib "user32" ( _

        ByVal hWnd As Long, lpwndpl As WINDOWPLACEMENT) As Long

    Private Declare Function SetWindowPlacement Lib "user32" ( _

        ByVal hWnd As Long, lpwndpl As WINDOWPLACEMENT) As Long

    #End If

    Sub Microsoft_Click()

      Dim Browser As Object 'SHDocVw.InternetExplorer

      Dim WP As WINDOWPLACEMENT

      'Create the object

      Set Browser = CreateObject("InternetExplorer.Application")

      'Setup the size of our variable

      WP.Length = Len(WP)

      'Get the settings from the window

      GetWindowPlacement Browser.hWnd, WP

      'Show it maximized (and visible)

      WP.showCmd = SW_MAXIMIZE

      'Execute

      SetWindowPlacement Browser.hWnd, WP

      Browser.Navigate ("http://microsoft.com")

    End Sub

    Was this answer helpful?

    0 comments No comments