Make File Pair | CodeGuru

Make File Pair

Macro that creates a .h / .cpp file pair. It will prompt for a file name and create the .h / .cpp files accordingly. The .h file will be given a small header and the usual #ifndef _FILE_H … sentry. The .cpp file will be given an #include “MyHFile.h”. The technique behind this code generation […]

Written By
CodeGuru Staff
CodeGuru Staff
Feb 23, 1999
1 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

Macro that creates a .h / .cpp file pair.

It will prompt for a file name and create the .h / .cpp files accordingly.

The .h file will be given a small header and the usual #ifndef _FILE_H … sentry.

The .cpp file will be given an #include “MyHFile.h”.



The technique behind this code generation is patended and trademarked in 123 countrys so dont you dare modifying it….heh…not really, do as you wish 🙂



The whole thing is split up into 3 separate macros (each can be run individually):


  • MakeHFileifdef
  • IncludeMyH
  • MakeFilePair
Sub MakeHFileifdef()DESCRIPTION: Generates #ifdef THISHFILE_H#endif definitionsif name =ARNE.Hthen s =_ARNE_Hs = Ucase(Application.ActiveDocument.Name)
	  p = Len(s)
	  for i = 1 to Len(s)
	    if Mid(s,i,1)=.then p = i-1
	  next
	  s =_+ Left(s,p) +_HApplication.ActiveDocument.Selection.StartOfDocument
 	  Application.ActiveDocument.Selection =#ifndef+ s + vbLf +#define+ s +vbLf
	  Application.ActiveDocument.Selection.EndOfDocument
 	  Application.ActiveDocument.Selection = vbLf +#endif //+ s + vbLf
End Sub
Sub IncludeMyH()DESCRIPTION: Inserts an #includethisFile.h”
      ‘if name =ARNE.CPPthen s =ARNEs = Application.ActiveDocument.Name
	  p = Len(s)
	  for i = 1 to Len(s)
	    if Mid(s,i,1)=.then p = i-1
	  next
	  s = Left(s,p)
	  Application.ActiveDocument.Selection =#include+Chr(34)+s+.h+Chr(34)+vbLf
End Sub
Sub GetFriendFile()DESCRIPTION: Opens the corresponding .h / .cpp file
	currentFileName = Application.ActiveDocument.FullName
	newFileName = “”
	‘MsgBox currentFileName
	if (Ucase(Right(currentFileName,2))=.H) then
	  newFileName = Left(currentFileName,Len(currentFileName)-2)+.CPPelseif	(Ucase(Right(currentFileName,4))=.CPP) then
	  newFileName = Left(currentFileName,Len(currentFileName)-4)+.Hend ifMsgBox newFileName
	if newFileName<>“” then	Application.Documents.Open newFileName
End Sub
Sub MakeFilePair()DESCRIPTION: Generates a .h / .cpp file pair
  dim newDoc
  s = InputBox(This macro generates a .h / .cpp file pair.+vbLf+vbLf+Enter filename (not including extension),Generate filepair)
  if s<>“” then
    newName = s+.hsTxt =/*———————————————–” + vbCrLf
	sTxt = sTxt+File name    :+ newName + vbCrLf
	sTxt = sTxt+Author       :+ vbCrLf
	sTxt = sTxt+”  “+ vbCrLf
	sTxt = sTxt+Description  :+ vbCrLf
	sTxt = sTxt+”  ———————————————–*/+ vbCrLf
	set newDoc = Application.Documents.Add(Text)
	newDoc.Save (newName)
	newDoc.Selection = vbCrLf+#include+Chr(34)+stdafx.h+Chr(34) + vbCfLf+ vbCrLf
	MakeHFileIfDef()
	newDoc.Selection.StartOfDocument
	newDoc.Selection = sTxt
	newName = s +.cppset newDoc = Application.Documents.Add(Text)
	newDoc.Save (newName)
	IncludeMyH()
  end if
End Sub

CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.