Click here to Skip to main content
13,763,340 members
Click here to Skip to main content
Add your own
alternative version


16 bookmarked
Posted 26 Sep 2001

Switch between Header and CPP file

, 26 Sep 2001
Rate this:
Please Sign up or sign in to vote.
A simple macro that allows you to quickly switch between associated header and implementation files.
<!-- Main HTML starts here -->


A simple macro that allows you to quickly switch between associated header and implementation files. It's only been tested with Visual Studio 6.

Sub HeaderSourceSwitch()
'DESCRIPTION: Switch between .h and .cpp
'Author: Miezoo (

	On Error Resume Next
	Dim sExt, sFile
	' current file's extension (all lowercase)
	sExt = fileExtension(ActiveDocument.FullName)
	' get file's name (without extension)
	sFile = Left(ActiveDocument.Name, Len(ActiveDocument.Name) - Len(sExt) - 1)

	' valid extension?
	If sExt = "h" or sExt = "hpp" or sExt = "hxx" Then
		sFile = sFile & ".cpp"
	ElseIf sExt = "c" or sExt = "cpp" or sExt = "cxx" Then
		sFile = sFile & ".h"
		MsgBox "Works for .h and .cpp files only!", vbExclamation, "Error"
		Exit Sub
	End If

	' is the file already opened?
	For Each doc In Application.Documents
		If LCase(doc.Name) = LCase(sFile) Then
			doc.Active = True
			Exit Sub
		End If

	' see which project contains the damn file
	sFile = findFileWithinWorkspace(sFile)
	Documents.Open sFile

	If Err.Number > 0 Then
		MsgBox "Pair file not found :(", vbCritical, "Error"
		Exit Sub
	End If

End Sub

Function findFileWithinWorkspace(file)
'DESCRIPTION: Iterates through all projects (*.dsp) within the current workspace
'             and finds which one owns the given file
	Dim project, bFound, sLine
	sLine = Null

	For Each project In Application.Projects
		' open .dsp file as .txt
		Documents.Open project.FullName, "Text", True
		'if this project is "the one", it must contain a line like: SOURCE=.\path\file
		bFound = ActiveDocument.Selection.FindText(file, dsMatchFromStart + dsMatchRegExp)
		If bFound = True Then
			sLine = ActiveDocument.Selection
			sLine = Left(sLine, Len(sLine) - 2)		' eliminate LF & CR
			sLine = Right(sLine, Len(sLine) - 8)	' eliminate SOURCE=.\
			If Left(sLine, 1) <> "." Then
				sLine = filePath(project.FullName) & sLine
			End If
			Exit For
		End If

	findFileWithinWorkspace = sLine
End Function

Function fileExtension(file)
'DESCRIPTION: Returns file extension.
	Dim iPos, iDot
	iDot = Null
	Do While True
		iPos = Instr(1, file, ".", 1)
		If iPos = 0 or iPos = Null Then
			Exit Do
			file = Right(file, Len(file) - iPos)
			iDot = iPos
		End If

	If iDot = Null Then
		fileExtension = ""
		fileExtension = LCase(file)
	End If

End Function

Function filePath(file)
'DESCRIPTION: Returns file path (without the trailing backslash "\").
	Dim iPos, iBS, strPath
	strPath = file
	iBS = Null
	Do While True
		iPos = Instr(1, file, "\", 1)
		If iPos = 0 or iPos = Null Then
			Exit Do
			file = Right(file, Len(file) - iPos)
			iBS = iPos
		End If

	If iBS = Null Then
		filePath = ""
		filePath = Left(strPath, Len(strPath) - Len(file) - 1)
	End If
End Function


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

United States United States
No Biography provided

You may also be interested in...


Comments and Discussions

GeneralCodeWinPos Pin
mikem819-Jul-07 17:39
membermikem819-Jul-07 17:39 
GeneralExcellent, slight fix though Pin
John-Lucas Brown26-Jun-06 2:43
memberJohn-Lucas Brown26-Jun-06 2:43 
GeneralDoes the job...Thanks! Pin
Member 194842616-Sep-05 7:19
memberMember 194842616-Sep-05 7:19 
GeneralIt's useful! thank you! Pin
xiaohe52112-Sep-05 21:21
memberxiaohe52112-Sep-05 21:21 
QuestionIsn't that already implemented in VC++? Pin
8-Oct-01 12:55
suss8-Oct-01 12:55 
GeneralAnother macro that does the job as well... Pin
Robin Schive4-Oct-01 21:31
memberRobin Schive4-Oct-01 21:31 
GeneralCodeWiz add-in does a better job Pin
3-Oct-01 0:20
suss3-Oct-01 0:20 
GeneralFilename matching problems Pin
Janne2-Oct-01 22:58
memberJanne2-Oct-01 22:58 
GeneralHmmmm Pin
Robin Schive2-Oct-01 22:15
memberRobin Schive2-Oct-01 22:15 
GeneralGood I love this macro. Pin
Kevin Cao27-Sep-01 19:51
memberKevin Cao27-Sep-01 19:51 
GeneralVery handy indeed ! Pin
flector27-Sep-01 10:12
memberflector27-Sep-01 10:12 
General.NET Pin
Thomas Freudenberg27-Sep-01 3:02
memberThomas Freudenberg27-Sep-01 3:02 
GeneralRe: .NET Pin
28-Sep-01 11:23
suss28-Sep-01 11:23 
GeneralRe: .NET Pin
Thomas Freudenberg22-Oct-01 11:39
memberThomas Freudenberg22-Oct-01 11:39 
GeneralRe: .NET Pin
Pavel Sokolov8-Apr-02 1:57
memberPavel Sokolov8-Apr-02 1:57 
GeneralRe: .NET Pin
Thomas Freudenberg8-Apr-02 9:27
memberThomas Freudenberg8-Apr-02 9:27 
GeneralRe: .NET Pin
S Fewings15-Apr-03 22:51
memberS Fewings15-Apr-03 22:51 
JokeRe: .NET Pin
Rolf Kristensen21-Jun-06 8:12
memberRolf Kristensen21-Jun-06 8:12 
GeneralRe: .NET Pin
Thomas Freudenberg21-Jun-06 8:18
memberThomas Freudenberg21-Jun-06 8:18 
GeneralRe: .NET Pin
Rolf Kristensen21-Jun-06 8:25
memberRolf Kristensen21-Jun-06 8:25 

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

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web06-2016 | 2.8.181112.3 | Last Updated 27 Sep 2001
Article Copyright 2001 by Miezoo
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid