Click here to Skip to main content
Click here to Skip to main content

Switch between Header and CPP File for Visual Studio .NET

By , , 3 Jun 2008
 

Introduction

Here are two methods that allow you to quickly switch between the associated header and implementation files in Visual Studio .NET.

Pavel Sokolov's Solution

A simple macro that allows you to quickly switch between the associated header and implementation files. This is a modification for Visual Studio .NET of the macro by Nooruddin Kapasi.

This macro simply switches between a *.h file and a *.cpp file, without any errors (if the *.h or the *.cpp file does not exist).

The Code

Option Strict Off
Option Explicit Off

Imports EnvDTE
Imports Microsoft.VisualBasic

Public Module Switch

    Sub Switch()
        '////////////////////////////////////////////
        'Nooruddin Kapasi 1998.
        'Pavel Sokolov , CEZEO software , http://www.cezeo.com , Adaptation for .NET
        'DESCRIPTION: Switch Between Header and cpp
        '////////////////////////////////////////////

        Dim a As String
        Dim b As String
        Dim Flag As Integer
        Flag = 0
        a = DTE.ActiveDocument.FullName()
        tmp = InStr(a, ".cpp")
        If tmp Then
            b = Left(a, Len(a) - 3) + "h"
            Flag = 1
        Else
            tmp = InStr(a, ".h")
            If tmp Then
                b = Left(a, Len(a) - 1) + "cpp"
                Flag = 1
            End If
        End If

        If Flag Then
            DTE.Documents.Open(b, "Text")
        End If
    End Sub

End Module

Pierre Arnaud's Solution

The Switch_H_CPP macro switches between the active document (either source file or header file) and its counterpart (either header file or source file). Whereas simpler macros get this job done more efficiently (see the above), they do not work if the header files and source files are not stored in the same directory.

Switch_H_CPP looks for a matching counterpart by walking through all the loaded project files. It can, therefore, deduce the full path of the file and open it even if it is in a different directory as the active document.

Source

Here comes the source:

Option Strict Off
Option Explicit Off

Imports EnvDTE
Imports System.Diagnostics

Public Module Opac

    Function WalkProjectItemsAndOpen(ByRef items As ProjectItems, _
                                     ByRef name As String) As Boolean
        Dim item As ProjectItem

        If ReferenceEquals(items, Nothing) Then
            Return False
        End If

        For Each item In items
            Dim sub_items As ProjectItems
            sub_items = item.SubProject
            If Not sub_items Is Nothing Then
                If WalkProjectItemsAndOpen(sub_items, name) Then
                    Return True
                End If
            End If
            If item.Name = name Then
                'MsgBox("Found " + name + " in project " + _
                                   items.ContainingProject.UniqueName)
                Dim op As ItemOperations
                op = DTE.ItemOperations
                op.OpenFile(item.FileNames(0), Constants.vsViewKindTextView)
                Return True
            End If
        Next
        Return False
    End Function

    Sub Swap_H_CPP()
        Dim proj As Project
        Dim doc As Document
        Dim name As String

        doc = DTE.ActiveDocument
        name = doc.Name

        If name.EndsWith(".h") Then
            name = name.Replace(".h", ".cpp")
        ElseIf name.EndsWith(".cpp") Then
            name = name.Replace(".cpp", ".h")
        End If

        For Each proj In DTE.Solution.Projects
            If WalkProjectItemsAndOpen(proj.ProjectItems, name) Then
                Exit Sub
            End If
        Next

        Beep()

    End Sub

End Module

History

  • 20th September, 2002 - Updated Pierre Arnaud's solution to stop an exception being thrown in certain circumstances
  • 2nd June, 2008 - Fixed a typo in WalkProjectItemsAndOpen

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Authors

Pavel Sokolov
Web Developer
Russian Federation Russian Federation
Member
C++ programmer.

Pierre Arnaud
Web Developer
Switzerland Switzerland
Member

Pierre Arnaud got a Ph.D. in computer science at the Swiss Federal Institute of Technology; he currently works both as an independent contractor on hardware and software projects at OPaC bright ideas and as a senior software designer at EPSITEC.

Pierre was a key contributor to the Smaky computer, a real time, multitasking system based on the Motorola 680x0 processor family.

Now, Pierre works on his spare time for the Creative Docs .NET project: it is a vector based drawing and page layout software based on .NET and AGG.


Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionTry thismvpDave Kerr3 Feb '12 - 23:05 
There's also a useful addin called Switch that switches between source and header, code and designer and so on - it's on the codeproject here:
 
Switch[^]

GeneralVisual Studio plugin that does thismembereggplantriot8 Apr '10 - 20:01 
Cool macro!
 
Possibly of interest to the same audience, PhatStudio (free open source Visual Studio plugin) has a header switch feature.   It's written in C# and the code is also available on the website.
 
http://phatstudio.sourceforge.net/
GeneralRe: Visual Studio plugin that does thismemberHoward Robinson15 Sep '10 - 4:58 
Thanks! PhatStudio is working well with VS2010.
Generaltypomemberkamnas2 Jun '08 - 1:09 
In this line you have an error which drive me nuts:
 
If WalkProjectItemsAndOpen(sub_itemsm, name) Then
AnswerVisual Studio 2005 Header Switch Macro [modified]memberSnakefoot21 Jun '06 - 3:21 
Adjusted the macro so it can be used for Visual Studio 2005
 
New Improved Search order:
1. Look in open documents (Inspired by Switch between Header and CPP file)
2. Look in the same directory as the opponent file
3. Look through all projects in the entire solution using DTE.Solution.FindProjectItem (Inspired by robertwrose.com)
4. Look through all include paths in the attached projects (Inspired by BorisJ's Blog)
 
The search of include paths, requires that one adds a reference to Microsoft.VisualStudio.VCProjectEngine
 
To install the macro do the following:
 
1. Start Visual Studio 2005
2. In the menu select Tools -> Macros -> Macro Explorer.
3. In the Macro Explorer right-click "MyMacros" and choose "New Module..."
4. Create a new Module with the name "Opac"
5. In the Macro Explorer double-click the newly created module "Opac".
6. In the Macro IDE overwrite the contents with the big chunk of code below
7. In the Macro IDE Project Explorer right-click References and choose Add Reference... to add:
   Microsoft.VisualStudio.VCProjectEngine
To assign a keyboard shortcut-key to the macro do the following:
1. Start Visual Studio 2005
2. In the menu select Tools -> Customize
3. Press the "Keyboard..." button
4. In the "Search Commands Containing" search for the name "Opac".
5. Change focus to "Press Shortcut keys" and assign a shortcut like CTRL+F6 (Or whatever)
6. Press "Assign" and THEN "Ok".
 
Enjoy
 
-Snakefoot
 
Option Strict Off
Option Explicit Off
 
Imports EnvDTE
Imports System.Diagnostics
' add reference to Microsoft.VisualStudio.VCProjectEngine
Imports Microsoft.VisualStudio.VCProjectEngine
 

Public Module Opac
 
    Function OpenLocalFile(ByVal sFile As String) As Boolean
        If My.Computer.FileSystem.FileExists(sFile) Then
            On Error Resume Next
            DTE.ItemOperations.OpenFile(sFile, Constants.vsViewKindCode)
            OpenLocalFile = (Err.Number = 0)
            Err.Clear()
            On Error GoTo 0
        Else
            OpenLocalFile = False
        End If
    End Function
 
    Function FilePath(ByVal file)
        'DESCRIPTION: Returns file path (without the trailing backslash "\").
        Dim iPos, iBS, strPath
 
        strPath = file
        iBS = Nothing
        Do While True
            iPos = InStr(1, file, "\", 1)
            If iPos = 0 Or iPos = Nothing Then
                Exit Do
            Else
                file = Right(file, Len(file) - iPos)
                iBS = iPos
            End If
        Loop
 
        If iBS = Nothing Then
            FilePath = ""
        Else
            FilePath = Left(strPath, Len(strPath) - Len(file) - 1)
        End If
    End Function
 

    Sub Swap_H_CPP()
        Dim name As String
 
        If ReferenceEquals(DTE.ActiveDocument, Nothing) Then
            DTE.StatusBar.Text = "Error no active document"
            Exit Sub
        End If
 
        If DTE.ActiveDocument.Language <> EnvDTE.Constants.dsCPP Then
            DTE.StatusBar.Text = "Error not a C++ document"
            Exit Sub
        End If
 
        name = DTE.ActiveDocument.Name.ToLower
 
        ' Try to switch from '.h' to '.cpp' then vice versa
        If name.EndsWith(".h") Then
            name = name.Replace(".h", ".cpp")
        ElseIf name.EndsWith(".hpp") Then
            name = name.Replace(".hpp", ".cpp")
        ElseIf name.EndsWith(".cpp") Then
            name = name.Replace(".cpp", ".h")
        ElseIf name.EndsWith(".c") Then
            name = name.Replace(".c", ".h")
        Else
            DTE.StatusBar.Text = "Error " + name + " has an unknown file-extension"
            Exit Sub
        End If
 
        DTE.StatusBar.Text = "Searching for " + name + " ..."
 
        ' Search open documents
        Dim iDoc As Integer
        For iDoc = 1 To DTE.Documents.Count
            If DTE.Documents.Item(iDoc).Name.ToLower = name Then
                OpenLocalFile(DTE.Documents.Item(iDoc).FullName)
                DTE.StatusBar.Text = "Ready"
                Exit Sub
            End If
        Next iDoc
 
        ' Search current directory
        If OpenLocalFile(DTE.ActiveDocument.Path & name) Then
            DTE.StatusBar.Text = "Ready"
            Exit Sub
        End If
 
        ' Attempt to find document in solution
        Dim item As EnvDTE.ProjectItem
        item = DTE.Solution.FindProjectItem(name)
        If Not ReferenceEquals(item, Nothing) Then
            If OpenLocalFile(item.FileNames(0)) Then
                DTE.StatusBar.Text = "Ready"
                Exit Sub
            End If
        End If
 
        ' Attempt to find document in include paths (Requires Microsoft.VisualStudio.VCProjectEngine reference)
        For Each proj As Project In DTE.Solution.Projects
            Dim vcProj As VCProject = proj.Object
            If Not ReferenceEquals(vcProj, Nothing) Then
                Dim configs As IVCCollection = vcProj.Configurations
                Dim index As Integer
                For index = 1 To configs.Count
                    Dim cfg As VCConfiguration = configs.Item(index)
                    Dim tools As IVCCollection = cfg.Tools
                    Dim compilerTool As VCCLCompilerTool = tools.Item("VCCLCompilerTool")
 
                    Dim path As String
                    For Each path In compilerTool.FullIncludePath.Split(";")
                        ' if relative path then adjust filepath accordingly
                        If InStr(path, "..") <> 0 Then
                            sFilePath = FilePath(proj.FullName)
                            Do While InStr(path, "..") <> 0
                                sFilePath = Left(sFilePath, InStrRev(sFilePath, "\") - 1)
                                path = Right(path, Len(path) - InStr(path, "..") - 1)
                            Loop
                            path = sFilePath + path
                        End If
 
                        If OpenLocalFile(path + "\" + name) Then
                            DTE.StatusBar.Text = "Ready"
                            Exit Sub
                        End If
                    Next
                Next
            End If
        Next
 
        DTE.StatusBar.Text = "Error File " + name + " could not be found in solution"
        Beep()
 
    End Sub
 
End Module
 
-- modified at 5:45 Thursday 30th November, 2006 (Now using DTE.Solution.FindProjectItem)
GeneralRe: Visual Studio 2005 Header Switch Macromemberjcem24 Feb '07 - 4:24 
Option Strict Off
Option Explicit Off
 
Imports System
Imports System.Collections
Imports EnvDTE
Imports EnvDTE80
Imports System.Diagnostics
 

Public Module Opac
 
Function OpenLocalFile(ByVal sFile As String) As Boolean
On Error Resume Next
Dim op As ItemOperations
op = DTE.ItemOperations
op.OpenFile(sFile, Constants.vsViewKindTextView)
OpenLocalFile = (Err.Number = 0)
Err.Clear()
On Error GoTo 0
End Function
 
Function WalkProjectItemsAndOpen(ByRef items As ProjectItems, ByRef name As String) As Boolean
Dim item As ProjectItem
Dim item_name As String
 
'MsgBox("Looking for " + name + " in project " + items.ContainingProject.UniqueName)
If ReferenceEquals(items, Nothing) Then
Return False
End If
 
For Each item In items
If Not ReferenceEquals(item.ProjectItems, Nothing) Then
If item.ProjectItems.Count > 0 Then
If WalkProjectItemsAndOpen(item.ProjectItems, name) Then
Return True
End If
End If
End If
 
'MsgBox("Testing for " + name + " matching " + item.Name.ToLower)
If name = item.Name.ToLower Then
' MsgBox("Found " + name + " in project " + items.ContainingProject.UniqueName)
If Not OpenLocalFile(item.FileNames(0)) Then
MsgBox("Cannot open " + item.FileNames(0) + " found in " + items.ContainingProject.UniqueName)
End If
Return True
End If
Next
 
Return False
End Function
 
Sub Swap_H_CPP()
Dim proj As Project
Dim doc As Document
 
If ReferenceEquals(DTE.ActiveDocument, Nothing) Then
DTE.StatusBar.Text = "Error no active document"
Exit Sub
End If
 
Dim name As String
 
name = DTE.ActiveDocument.Name.ToLower()
 
Dim cppExts As New ArrayList()
 
'all ext should be lower string - see above -> name = name.ToLower(), below If name.EndsWith(objLoop) Then
cppExts.Add(".cpp")
cppExts.Add(".cxx")
cppExts.Add(".cc")
cppExts.Add(".c")
 
Dim hppExts As New ArrayList()
 
'all ext should be lower string - see above -> name = name.ToLower(), below If name.EndsWith(objLoop) Then
hppExts.Add(".hpp")
hppExts.Add(".hxx")
hppExts.Add(".hh")
hppExts.Add(".h")
hppExts.Add(".inl")
'hppExts.Add(".l")
'hppExts.Add(".h")
 
' array of array : if exts[0] is ".cpp" then aSubs[0] holds { ".hpp" ".hxx" ".hh" ".h" ".inl" ".cxx" ".cc" ".c" }
' ...
' array of array : if exts[4] is ".hpp" then aSubs[0] holds { ".cpp" ".cxx" ".cc" ".c" ".hxx" ".hh" ".h" ".inl" }
' ...
Dim exts As New ArrayList() ' one extension of cppExts And hppExts
Dim aSubs As New ArrayList()
 
Dim objLoop As Object
Dim objTemp As Object
Dim ALTemp
 
For Each objLoop In cppExts
exts.Add(objLoop)
ALTemp = New ArrayList()
aSubs.Add(ALTemp)
For Each objTemp In hppExts
ALTemp.Add(objTemp)
Next objTemp
For Each objTemp In cppExts
If objLoop <> objTemp Then
ALTemp.Add(objTemp)
End If
Next objTemp
Next objLoop
 
For Each objLoop In hppExts
exts.Add(objLoop)
ALTemp = New ArrayList()
aSubs.Add(ALTemp)
For Each objTemp In cppExts
ALTemp.Add(objTemp)
Next objTemp
For Each objTemp In hppExts
If objLoop <> objTemp Then
ALTemp.Add(objTemp)
End If
Next objTemp
Next objLoop
 
Dim count As Integer
 
count = 0
 
Dim nameTemp As String
 
For Each objLoop In exts
If name.EndsWith(objLoop) Then
For Each objTemp In aSubs.Item(count)
nameTemp = name
nameTemp = nameTemp.Replace(objLoop, objTemp)
 
DTE.StatusBar.Text = "Searching for " + nameTemp + " ..."
 
'not necessary???
'' Search open documents
'For Each doc In DTE.Documents
'If doc.Name.ToLower = nameTemp Then
'OpenLocalFile(doc.FullName)
'DTE.StatusBar.Text = "Ready"
'Exit Sub
'End If
'Next
 
' Search current directory
If OpenLocalFile(DTE.ActiveDocument.Path & nameTemp) Then
DTE.StatusBar.Text = "Ready"
Exit Sub
End If
 
'too slow to search... uncomment this block if you want
'' Attempt to find document i solution
'Dim item As EnvDTE.ProjectItem
'item = DTE.Solution.FindProjectItem(nameTemp)
'If Not ReferenceEquals(item, Nothing) Then
'If OpenLocalFile(item.FileNames(0)) Then
'DTE.StatusBar.Text = "Ready"
'Exit Sub
'End If
'End If
 
'too slow to search... uncomment this block if you want
'DTE.StatusBar.Text = "Searching sequential for " + nameTemp + " ..."
'' Search all projects in the solution (slow)
'For Each proj In DTE.Solution.Projects
'If WalkProjectItemsAndOpen(proj.ProjectItems, nameTemp) Then
'DTE.StatusBar.Text = "Ready"
'Exit Sub
'End If
'Next
 
DTE.StatusBar.Text = "Error File " + nameTemp + " could not be found in solution"
Next objTemp
Exit For
End If
count = count + 1
Next objLoop
 
DTE.StatusBar.Text = "Error " + name + " has an unknown file-extension"
Beep()
 
Exit Sub
End Sub
 
End Module

 
The meaning of life is to give life a meaning.

GeneralYet another adaptation...memberantoinecloutier27 Apr '06 - 14:39 
Hello and special thanks to all contributors to this macro.
 
Here is my adaptation.
 
Highlights:
The filename match is now done in a case-insensitive way. This saves us the full conversion of both operands to lowercase prior to the comparison. The whole macro now feels snappier.
 
The way the "swap" was implemented did not completely suit my needs. I documented my changes with an ASCII diagram (I was bored...) so that you can see right away if it makes sense to you or not.
 
Tested in VS 2005 only.
 
Antoine
 

Imports System
Imports EnvDTE
Imports EnvDTE80
Imports System.Diagnostics
 
Public Module Utilities
 
#Region "ToggleDeclarationAndImplementation"
#Region "Helper function: WalkProjectItemsAndOpen"
Function WalkProjectItemsAndOpen(ByRef items As ProjectItems, ByRef name As String) As Boolean
Dim item As ProjectItem
Dim item_name As String
 
If ReferenceEquals(items, Nothing) Then
Return False
End If
 
For Each item In items
Dim sub_items As ProjectItems
sub_items = item.ProjectItems
If Not sub_items Is Nothing Then
If WalkProjectItemsAndOpen(sub_items, name) Then
Return True
End If
End If
 
item_name = item.FileNames(1).ToLower
If String.Compare(item_name, name, True) = 0 Then
Dim op As ItemOperations
op = DTE.ItemOperations
op.OpenFile(item.FileNames(1), Constants.vsViewKindTextView)
Return True
End If
Next
Return False
End Function
#End Region
 
Sub ToggleDeclarationAndImplementation()
Dim proj As Project
Dim doc As Document
Dim name As String
Dim ext As String
Dim toggledName As String
 
' Get the active document's name and extension
doc = DTE.ActiveDocument
name = doc.Path & doc.Name
Dim pos = name.LastIndexOf(".")
If pos <> -1 Then
ext = name.Substring(pos).ToLower()
name = name.Substring(0, pos)
Else
ext = String.Empty
End If
 
' Graph:
'
' ,---<----<----<----<----<---,  ,--<----<----<----<----<----<-,
' |      ____         ____    |  |      ____          ____     |
' '-->  /    \       /    \---'  '-->  /    \        /    \    |
'      | .cpp |---> | .hpp |          |  .h  |----> |  .c  | --'
' ,-->  \____/       \____/---->---->  \____/        \____/
' |        |                            |  |
' |        '---->---->---->---->---->---'  |
' |                                        |
' '---<----<----<----<----<----<----<----<-'
'
' Possible transitions:
'
' .cpp --> .hpp
' .cpp --> .h
' .c --> .h
' .hpp --> .h
' .hpp --> .cpp
' .h --> .c
' .h --> .cpp
'
' For any given file type, there are at most two outgoing transitions.
' We will try each transition until one is found or none can be made.
'
' Note: The order in which we try the transitions is important if we
' want to be able to cycle through the following sequence : cpp-hpp-h-cpp-...
 
' First possible transition for each of the four supported file types:
If ext = ".cpp" Then
toggledName = name & ".hpp"
ElseIf ext = ".hpp" Then
toggledName = name & ".h"
ElseIf ext = ".h" Then
toggledName = name & ".cpp"
ElseIf ext = ".c" Then
toggledName = name & ".h"
Else
' Unsupported file type
Beep()
Exit Sub
End If
 
' Try the first possible transition
For Each proj In DTE.Solution.Projects
If WalkProjectItemsAndOpen(proj.ProjectItems, toggledName) Then
Exit Sub
End If
Next
 
' Second possible transition for the file types having two transitions
If ext = ".cpp" Then
toggledName = name & ".h"
ElseIf ext = ".hpp" Then
toggledName = name & ".cpp"
ElseIf ext = ".h" Then
toggledName = name & ".cpp"
Else
Beep()
Exit Sub
End If
 
' Try the second possible transition
For Each proj In DTE.Solution.Projects
If WalkProjectItemsAndOpen(proj.ProjectItems, toggledName) Then
Exit Sub
End If
Next
 
Beep()
 
End Sub
 
#End Region
 
End Module

GeneralCumulative VC++ 2003 (ver 7.1.3088) CPP-H-SwappermemberTedimDim1 Apr '06 - 1:50 
Option Strict Off
Option Explicit Off
 
Imports EnvDTE
Imports System.Diagnostics
 
Public Module Opac
 
Function WalkProjectItemsAndOpen(ByRef items As ProjectItems, ByRef name As String) As Boolean
Dim item As ProjectItem
Dim item_name As String
 
If ReferenceEquals(items, Nothing) Then
Return False
End If
 
For Each item In items
Dim sub_items As ProjectItems
sub_items = item.ProjectItems
If Not sub_items Is Nothing Then
If WalkProjectItemsAndOpen(sub_items, name) Then
Return True
End If
End If
item_name = item.FileNames(1).ToLower
If item_name = name Then
' MsgBox("Found " + name + " in project " + items.ContainingProject.UniqueName)
Dim op As ItemOperations
op = DTE.ItemOperations
op.OpenFile(item.FileNames(0), Constants.vsViewKindTextView)
Return True
End If
Next
Return False
End Function
 
Sub Swap_H_CPP()
Dim proj As Project
Dim doc As Document
Dim name As String
 
doc = DTE.ActiveDocument
name = doc.Path & doc.Name
name = name.ToLower
 
' First try to switch from '.h' to '.cpp' then vice versa
If name.EndsWith(".h") Then
name = name.Replace(".h", ".cpp")
ElseIf name.EndsWith(".hpp") Then
name = name.Replace(".hpp", ".cpp")
ElseIf name.EndsWith(".cpp") Then
name = name.Replace(".cpp", ".h")
ElseIf name.EndsWith(".c") Then
name = name.Replace(".c", ".h")
 
ElseIf name.EndsWith(".h") Then
name = name.Replace(".h", ".c")
ElseIf name.EndsWith(".hpp") Then
name = name.Replace(".hpp", ".c")
ElseIf name.EndsWith(".cpp") Then
name = name.Replace(".cpp", ".hpp")
ElseIf name.EndsWith(".c") Then
name = name.Replace(".c", ".hpp")
End If
 
For Each proj In DTE.Solution.Projects
If WalkProjectItemsAndOpen(proj.ProjectItems, name) Then
Exit Sub
End If
Next
 
Beep()
 
End Sub
 
End Module

GeneralRe: Cumulative VC++ 2003 (ver 7.1.3088) CPP-H-SwappermemberPierre Arnaud1 Apr '06 - 19:52 
Thank you for the updates. I have switched to VS.NET 2005 and almost exclusively use C# now, so I won't try this new polished version of the macro in the coming months.;)
GeneralHOWmemberalan937 Apr '05 - 5:12 
yea, how do you load this in visual studio .net. I know how in 6.0 but not in .net
 
Does the .vb need to be somewhere? or else how is it loaded in IDE? How about assigning keyboard short cuts? how? not much details to this.
 
Tools - Customize - Command only displays macros
GeneralFolder problemmemberMr Scotty5 Jan '05 - 4:17 
Inside i have organised my sources in Sub Folders. The macro then doesn't work. It doesn't check the sub folder names. I've added the Bold stuff following to solve the problem
 

...
sub_items = item.SubProject
If Not sub_items Is Nothing Then
If WalkProjectItemsAndOpen(sub_items, name) Then
Return True
End If
End If

sub_items = item.ProjectItems
If sub_items.Count > 0 Then
If WalkProjectItemsAndOpen(sub_items, name) Then
Return True
End If
End If

If item.Name = name Then
'MsgBox("Found " + name + " in project " + items.ContainingProject.UniqueName)
...

GeneralExcellentsussAnonymous27 Nov '03 - 23:38 
It's amazing that such a simple macro can be so useful Smile | :)
Just a pity that VS.NET macros run so much slower than in VS6 Frown | :(

QuestionHow to install?sussAnonymous7 Nov '03 - 1:18 
Sorry if this seems to be a stupid question, but what is the correct way to install these macros?
I tried just opening the macros IDE and simply pasting the code into the samples project, but then when I try to assign a key it does not appear in the listbox.
QuestionHow to install?sussAnonymous7 Nov '03 - 1:18 
Sorry if this seems to be a stupid question, but what is the correct way to install these macros?
I tried just opening the macros IDE and simply pasting the code into the samples project, but then when I try to assign a key it does no appear in the listbox.
AnswerI have the same problem in VS .NET 2003memberSkyhawk13 Jan '04 - 12:13 
I doesn't able to assign the keyboard shortcut, because the macro doesn't appear in Options/Environment/Keyboard page. I can see only Macros.Samples.* items in the listbox. Anyone can help?
GeneralRe: I have the same problem in VS .NET 2003memberPavel Sokolov22 Jan '04 - 21:28 
Customise -> Commands -> Macros

 
Pavel Sokolov
 
-- modified at 20:46 Sunday 16th October, 2005
GeneralRe: I have the same problem in VS .NET 2003memberShroombaker19 Apr '04 - 12:11 
How can I create a toolbar button that will launch the macro?

GeneralRe: I have the same problem in VS .NET 2003sussRehan A30 Apr '04 - 7:36 
All of you having problem installing the macro!
 
Make sure the Module name is the same as specified in the macro above Opac. If you create a new macro it is named as Module1, just rename it to Opac. Also dont forget to compile the project in which u created the module.

AnswerRe: How to install?memberAlan Balkany17 Mar '10 - 7:22 
The author should have mentioned how to install it; it's worthless if you can't use it.
 
In Visual Studio 2005, I selected: Tools | Macros | New Macro Project.
 
Then I pasted the code, removing the duplicate import statement, and moving the option statements to the beginning. I named it "Switch".
 
Then I selected: Tools | Customize, and selected the Macros category.
 
Then I highlighted the "Switch" macro, and clicked the "Keyboard..." button.
 
I entered "Switch" into the "Show commands containing:" box.
 
Then I highlighted the Switch macro in the list, entered ctrl-Q (single key stroke) in the "Press shortcut keys" box, and clicked the Assign button.
GeneralFix for filename case sensitivitymemberhgohel21 Sep '03 - 6:38 
Here's a simple fix for files that have a mix of .H, .CPP, .Cpp or any other styles for which Pierre's code won't work off the bat.
 
Simply change these two lines:
 
In Swap_H_CPP() change
name = doc.Name
to
name = doc.Name.ToLower
 
and also in Function WalkProjectItemsAndOpen() change
If item.Name = name Then
to
If item.Name.ToLower = name Then
 
Now it should work in more cases.

GeneralFix for VS .NET 2003memberdr00g7 May '03 - 23:43 
This macro no longer works in VS. NET 2003. It's easy to fix though - replace this line:
 
sub_items = item.SubProject
 
with this one:
 
sub_items = item.ProjectItems
 
It should work fine now.

GeneralRe: Fix for VS .NET 2003memberTodd C. Gleason28 Aug '03 - 7:00 
There's also a simple typo:
 
If Not sub_items Is Nothing Then
If WalkProjectItemsAndOpen(sub_itemsm, name) Then
"sub_itemsm" should be "sub_items"
 
After doing this and the above-listed fix, the code works as in the article. It's too bad it's a little slow--it would be faster if it knew to check the project containing the current file first. (I'm not sure if multiple projects might contain the same files--they don't in our company--but it could still fall back to checking the other projects in the solution)
 
--Todd C. Gleason
www.cool-man.org
GeneralThis doesn't work in Final Beta 2003memberNevering22 Mar '03 - 12:58 
Got an error on Constants.. resolved by adding namespace.
 
Then it doesn't switch files at all.
 

GeneralRe: This doesn't work in Final Beta 2003memberNevering22 Mar '03 - 13:11 
Using Pierre Arnaud's version.
GeneralRe: This doesn't work in Final Beta 2003membersakamaki18 Jul '06 - 2:40 
try this...
 
EnvDTE.Constants.vsViewKindTextView

GeneralError when using Swap_H_CPPmemberhector0319 Jan '03 - 9:36 
Whenever I try to use Swap_H_CPP I get the following error:
 
QI for IEnumVARIANT failed on the unmanaged server.
 
Any help would be appreciated.
 
Thanks,
Jason
GeneralRe: Error when using Swap_H_CPPmemberPierre Arnaud9 Jan '03 - 19:41 
No idea what this message is all aboutConfused | :confused: . Perhaps you might want to run the macro step-by-step and locate when exactly this problem happens.
I suppose there might be an installation problem of your VS.NET.
 
Good luck.
 
Pierre

GeneralProblem defining 'proj As Project'sussAnonymous11 Sep '02 - 11:44 
If anyone can help, I'd be greatful.
 
The second macro (the useful one) doesn't work correctly on my .NET system. The IDE complains that:
 
Dim proj As Project
 
is not permitted because "Module 'Project' cannot be used as a type.". If this line is commented out, then the macro runs, completes it's task then puts up an error box stating "Object not found". I have tracked this to the "proj" variable (I think), but am now stuck.
 
Thanks
 
J

GeneralRe: Problem defining 'proj As Project'memberPierre Arnaud12 Sep '02 - 10:49 
This behaviour is really strangeConfused | :confused: . I suggest you try to replace:
 
Dim proj As Project
 
with:
 
Dim proj as EnvDTE.Project
 
which should help .NET find the Project class. Maybe you aliased the Project name by defining a Project function in your macros?
 
Hope this helps...
GeneralExcellent workmemberSimon Hughes13 May '02 - 23:25 
You wouldn't believe such a small utility can be so useful. I use a .h/.cpp switcher for VC6 and using .NET without it was like having my left arm cut-off.
 
Microsoft should take note and make this a standard feature!
 
Regards,
Simon Hughes
E-mail: simon@hicrest.net
Web: www.hicrest.net
General.c and .hpp filesmemberThomas Freudenberg9 May '02 - 0:42 
IMHO the macro should consider the extensions .c and .hpp as well.
So Swap_H_CPP() should be:
Sub Swap_H_CPP()
    Dim proj As Project
    Dim doc As Document
    Dim name As String
 
    doc = DTE.ActiveDocument
    name = doc.Name
 
    ' first try to switch to .cpp resp. .h 
    If name.EndsWith(".h") Then
        name = name.Replace(".h", ".cpp")
    ElseIf name.EndsWith(".hpp") Then
        name = name.Replace(".hpp", ".cpp")
    ElseIf name.EndsWith(".cpp") Then
        name = name.Replace(".cpp", ".h")
    ElseIf name.EndsWith(".c") Then
        name = name.Replace(".c", ".h")
    End If
 
    For Each proj In DTE.Solution.Projects
        If WalkProjectItemsAndOpen(proj.ProjectItems, name) Then
            Exit Sub
        End If
    Next
 
    name = doc.Name
 
    ' now try to switch to .c resp. .hpp
    If name.EndsWith(".h") Then
        name = name.Replace(".h", ".c")
    ElseIf name.EndsWith(".hpp") Then
        name = name.Replace(".hpp", ".c")
    ElseIf name.EndsWith(".cpp") Then
        name = name.Replace(".cpp", ".hpp")
    ElseIf name.EndsWith(".c") Then
        name = name.Replace(".c", ".hpp")
    End If
 
    For Each proj In DTE.Solution.Projects
        If WalkProjectItemsAndOpen(proj.ProjectItems, name) Then
            Exit Sub
        End If
    Next
 
    Beep()
 
End Sub

 
Regards
Thomas
 
Sonork id: 100.10453 Thömmi
 

Disclaimer:
Because of heavy processing requirements, we are currently using some of your unused brain capacity for backup processing. Please ignore any hallucinations, voices or unusual dreams you may experience. Please avoid concentration-intensive tasks until further notice. Thank you.

GeneralRe: .c and .hpp filesmemberPierre Arnaud9 May '02 - 4:05 
You are right, but since my current projects only use .cpp/.h and .cs, I did not implement support for .c/.h or .cpp/.hpp. I guess this should be something easy to customise Wink | ;) . Thank you for your feed-back.
 
Pierre

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web01 | 2.6.130516.1 | Last Updated 3 Jun 2008
Article Copyright 2002 by Pavel Sokolov, Pierre Arnaud
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid