views:

226

answers:

2

We are using Visual Studio 2008 and would like to know if there is a way to create a (keyboard or toolbar) shortcut for the 'View in Browser'-command, but with a specific page from a specific (loaded) project.

We always start testing/debugging our app from "Somepage.aspx" from "Project-x". I would like to make a shortcut that does 'View in Browser' with this specific page/file, from this specific project. So even if I am currently working on another file in another project (from the same solution) it should still work...

Anybody know if this is possible, and if so, how this can be achieved?

Thanks! W.

A: 

The following macro opens specific page in your default browser:

Sub OpenMyPage()
    Try
        Dim url As String
        url = "C:\HTMLPage1.htm"
        'enclose URL in double quotes
        url = """" & url & """"
        DTE.ExecuteCommand("nav", url & " /new /ext")
        'nav is alias for View.ShowWebBrowser command
        'Syntax:
        'View.ShowWebBrowser URL [/new][/ext]
        '
        '/new 
        ' Optional. Specifies that the page appears in a new instance of the Web browser.
        '/ext 
        ' Optional. Specifies that the page appears in the default Web browser outside of the IDE.
    Catch ex As Exception
    End Try
End Sub

Create the macro and modify the url variable. Then you can create a toolbar or menu button or assign keyboard shortcut to it.

Peter Macej
Hi Peter... Looks promising. It works, however, it does not launch my development webserver like the Visual Studio context-menu 'View in browser' does before opening the browser to show the page. Any suggestions on that ? Thanks!
WowtaH
+1  A: 

You are right, my first answer opens the page in browser but doesn't launch webserver. Try the following macro. It uses ViewinBrowser command so it should work as expected.

Sub OpenMyPage()
    Dim solutionExplorerHier As EnvDTE.UIHierarchy
    solutionExplorerHier = DTE.Windows.Item(EnvDTE.Constants.vsWindowKindSolutionExplorer).Object
    Dim oldSelected As Object = solutionExplorerHier.SelectedItems
    solutionExplorerHier.GetItem("MySolution\MyProject\HTMLPage1.htm").Select(vsUISelectionType.vsUISelectionTypeSelect)
    DTE.ExecuteCommand("File.ViewinBrowser")

    'restore selected items
    Dim item As EnvDTE.UIHierarchyItem
    For Each item In DirectCast(oldSelected, Array)
        item.Select(vsUISelectionType.vsUISelectionTypeSelect)
    Next
End Sub    

Just change the path in GetItem method. It is the complete path to the file you see in your Solution explorer. This macro assumes that the file is a part of your solution.

Peter Macej
Hi Peter.. Thanks again.. this works better than the first one, however ( :) ) it only works when the focus is already in the Solution Explorer, it doesn't work when the focus is in the code editor. Any suggestion on this ?
WowtaH
Just add the following at the beginning of macro:Dim oldActiveWindow As EnvDTE.Window = DTE.ActiveWindowDTE.Windows.Item(EnvDTE.Constants.vsWindowKindSolutionExplorer).Activate()and the following at the end:oldActiveWindow.Activate()
Peter Macej
There is a newline and not the space before DTE.Windows.Item....
Peter Macej
Works like a charm.. Thanks !
WowtaH