Open header file

Th following macro opens a header file included on the current line (just like right-click on the header file name and choosing Open filename.h).


Sub OpenHeaderFile()
	DESCRIPTION: Opens a header file which is #included on the current line

	ActiveDocument.Selection.SelectLine
	LineText = ActiveDocument.Selection
	StartPos = Instr(LineText,"""") 'find the #include "" style 

	If (StartPos = 0) Then 
		StartPos = Instr(LineText, "<") 'find the #include <> style
	End If

	StartPos = StartPos + 1 'move to the start of the include file
	LineText = Mid(LineText,StartPos)
	EndPos = Instr(LineText,".h")' find the .h 
	EndPos = EndPos + 1 'move to the end of the include file
	FileName = Mid(LineText,1,EndPos)
	PathName = ActiveDocument.Path + "\"
	ActiveDocument.Selection.EndOfLine 'remove selected stuff
	FullFileName = PathName+FileName
	on error resume next
	Documents.Open FullFileName
End Sub


Comments

  • Updated VS2005/2008

    Posted by britonleap on 02/20/2008 02:29pm

    I found this elsewhere, but it wasn't working so I've added enough to make it behave in my VS 2005/2008. Sorry for not having proper credit.
    
    BTW There is an Open Header in the popup menu, but not an Open Implementation. Often, ClassView will not fetch the implementation so this is useful.
    
    Sub HeaderFlip()
    'DESCRIPTION: Open corresponding .h / .cpp file
    
       Dim currentFileName As String
       Dim newFileName As String
       Dim fno As Integer
       currentFileName = Application.ActiveDocument.FullName
       newFileName = ""
    
       If (UCase(Right(currentFileName, 2)) = ".H") Then
          newFileName = Left(currentFileName, Len(currentFileName) - 2) + ".CPP"
       ElseIf (UCase(Right(currentFileName, 4)) = ".CPP") Then
          newFileName = Left(currentFileName, Len(currentFileName) - 4) + ".H"
       End If
    
       On Error Resume Next
       If newFileName <> "" Then
          Application.Documents.Open(newFileName)
       End If
    End Sub

    Reply
  • Updated Macro

    Posted by Legacy on 10/26/2003 12:00am

    Originally posted by: Mig

    This macro allows you to open for editing header/source file, corresponding to the one that open now: 
    
    (tested for VC++ 6.0)

    FileName = Application.ActiveDocument
    FileExt = Right(FileName, 2)

    if FileExt = "PP" Then FileName = Left(FileName, Len(FileName)-4) + ".h"
    if FileExt = ".h" Then FileName = Left(FileName, Len(FileName)-2) + ".cpp"

    Documents.SaveAll
    Documents.Open FilePath

    Reply
  • Open Include File is Now a Feature

    Posted by Legacy on 10/06/2001 12:00am

    Originally posted by: No Longer Necessary in VC 6.0

    Just right click on the include line of the file you want to open and choose, 'Open Document'.

    Reply
  • ciulpk byby

    Posted by Legacy on 07/31/2001 12:00am

    Originally posted by: asad

    shoodas, paprasciau su goto line

    Reply
  • Not Clear !!!

    Posted by Legacy on 05/21/1999 12:00am

    Originally posted by: Sriram

    Where should i write this macro & how does it get executed?????

    Reply
  • Doesn't VC have this built-in?

    Posted by Legacy on 05/11/1999 12:00am

    Originally posted by: Yonat

    When I left-click on a filename (without this macro), one of the pop-up menu items is "Open Documnet <filename>". What does this macro do that is different?

    Reply
  • it's very useful!

    Posted by Legacy on 12/19/1998 12:00am

    Originally posted by: james qiao

    I find it's quite handy,and I can switch to .cpp or .h file very quickly.

    Reply
  • Ctrl+Shift+G (File, FileGoTo) works also with VC5

    Posted by Legacy on 10/15/1998 12:00am

    Originally posted by: Gert Rijs

    Ctrl+Shift+G (File, FileGoTo) works also with VC5
    you don't need to select anything, just make sure the cursor is on a filename.
    VC will search all the standard places for the file

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • The first phase of API management was about realizing the business value of APIs. This next wave of API management enables the hyper-connected enterprise to drive and scale their businesses as API models become more complex and sophisticated. Today, real world product launches begin with an API program and strategy in mind. This API-first approach to development will only continue to increase, driven by an increasingly interconnected web of devices, organizations, and people. To support this rapid growth, …

  • Packaged application development teams frequently operate with limited testing environments due to time and labor constraints. By virtualizing the entire application stack, packaged application development teams can deliver business results faster, at higher quality, and with lower risk.

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds