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 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 definitions

‘if name = “ARNE.H” then s = “_ARNE_H”
s = 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) + “_H”

Application.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 #include “thisFile.h”

‘if name = “ARNE.CPP” then s = “ARNE”
s = 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)+”.CPP”
elseif (Ucase(Right(currentFileName,4))=”.CPP”) then
newFileName = Left(currentFileName,Len(currentFileName)-4)+”.H”
end if

‘MsgBox 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+”.h”
sTxt = “/*———————————————–” + 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 +”.cpp”
set newDoc = Application.Documents.Add(“Text”)
newDoc.Save (newName)
IncludeMyH()
end if

End Sub

More by Author

Must Read