Make File Pair
Posted
by Per Nilsson
on February 23rd, 1999
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

Comments
My way of doing the same
Posted by Legacy on 08/01/2003 12:00amOriginally posted by: Graham Reeds
Reply-Auto add to project -Creation in project folder
Posted by Legacy on 07/02/2003 12:00amOriginally posted by: Oztan Harmanci
ReplyHow do i get dependent file for Project in VC++
Posted by Legacy on 01/21/2000 12:00amOriginally posted by: ramesh
i have requirement of getting dependent files to perticular project through program in VC++(like apllication package WIZARD uses)
thank you
ReplyGreat! Now, how do you auto add files to current project??
Posted by Legacy on 06/09/1999 12:00amOriginally posted by: Chuck Baker
Thanks! I have been looking all over for a macro that
creates the .h and .cpp files.
Now, how do you get the macro to automatically insert these
two new files INTO the current Project? Just like if you
did an ADD FILES TO PROJECT...
I really need this functionality to complete my macros!
Reply