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

	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


  • 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
       End If
    End Sub

  • 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.Open FilePath

  • 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'.

  • ciulpk byby

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

    Originally posted by: asad

    shoodas, paprasciau su goto line

  • 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?????

  • 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?

  • 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.

  • 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

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

Top White Papers and Webcasts

  • Enterprises are increasingly looking to platform as a service (PaaS) to lower their costs and speed their time to market for new applications. Developing, deploying, and managing applications in the cloud eliminates the time and expense of managing a physical infrastructure to support them. PaaS offerings must deliver additional long-term benefits, such as a lower total cost of ownership (TCO), rapid scalability, and ease of integration, all while providing robust security and availability. This report …

  • Moving from an on-premises environment to Office 365 does not remove the need to plan for disruptions or reduce the business risk requirements for protecting email services. If anything, some risks increase with a move to the cloud. Read how to ease the transition every business faces if considering or already migrating to cloud email. This white paper discusses: Setting expectations when migrating to Office 365 Understanding the implications of relying solely on Exchange Online security Necessary archiving …

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date