Click here to Skip to main content
Licence 
First Posted 26 Sep 2001
Views 73,501
Bookmarked 16 times

Switch between Header and CPP file

By | 26 Sep 2001 | Article
A simple macro that allows you to quickly switch between associated header and implementation files.

Introduction

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 (miezoo@f2s.com)

	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"
	Else
		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
	Next

	' 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"
		Err.Clear
		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
			ActiveDocument.Selection.SelectLine
			sLine = ActiveDocument.Selection
			ActiveDocument.Close
			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
		ActiveDocument.Close
	Next

	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
		Else
			file = Right(file, Len(file) - iPos)
			iDot = iPos
		End If
	Loop

	If iDot = Null Then
		fileExtension = ""
	Else
		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
		Else
			file = Right(file, Len(file) - iPos)
			iBS = iPos
		End If
	Loop

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

License

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

Miezoo



United States United States

Member



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. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralCodeWinPos Pinmembermichaelmakuch16:39 19 Jul '07  
GeneralExcellent, slight fix though PinmemberJohn-Lucas Brown1:43 26 Jun '06  
GeneralDoes the job...Thanks! PinmemberJithendriya6:19 16 Sep '05  
GeneralIt's useful! thank you! Pinmemberxiaohe52120:21 12 Sep '05  
QuestionIsn't that already implemented in VC++? PinmemberAnonymous11:55 8 Oct '01  
GeneralAnother macro that does the job as well... PinmemberRob Schivas20:31 4 Oct '01  
GeneralCodeWiz add-in does a better job PinmemberSplintor23:20 2 Oct '01  
GeneralFilename matching problems PinmemberJanne21:58 2 Oct '01  
GeneralHmmmm PinmemberRob Schivas21:15 2 Oct '01  
GeneralGood I love this macro. PinmemberKevin Cao18:51 27 Sep '01  
GeneralVery handy indeed ! PinmemberEric Nitzsche9:12 27 Sep '01  
General.NET PinmemberThomas Freudenberg2:02 27 Sep '01  
GeneralRe: .NET PinmemberAnonymous10:23 28 Sep '01  
GeneralRe: .NET PinmemberThomas Freudenberg10:39 22 Oct '01  
GeneralRe: .NET PinmemberPavel Sokolov0:57 8 Apr '02  
GeneralRe: .NET PinmemberThomas Freudenberg8:27 8 Apr '02  
GeneralRe: .NET PinmemberS Fewings21:51 15 Apr '03  
JokeRe: .NET PinmemberSnakefoot7:12 21 Jun '06  
GeneralRe: .NET PinmemberThomas Freudenberg7:18 21 Jun '06  
GeneralRe: .NET PinmemberSnakefoot7:25 21 Jun '06  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    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 | Mobile
Web01 | 2.5.120517.1 | Last Updated 27 Sep 2001
Article Copyright 2001 by Miezoo
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid