Reputation: 6142
Today I surfed some random geek-stuff articles on wikipedia to get my daily dose of useless knowledge. I stumbled accross quines, which are programs that print their own source-code. I found that a great way to make my brain hurt, so I began working on a quine in VBA. I had two good reasons:
Here is my masterpiece:
Sub q()
c = "Sub q();c = #;Debug.Print Replace(Replace(c, Chr(59), vbNewLine), Chr(35), Chr(34) & c & Chr(34));End Sub"
Debug.Print Replace(Replace(c, Chr(59), vbNewLine), Chr(35), Chr(34) & c & Chr(34))
End Sub
My challenge: Can you make it even shorter (and preferably more awkward)?
Upvotes: 7
Views: 646
Reputation: 4129
Depending on your definition of "printing itself", this could work (32 bytes):
Sub q()
SendKeys "^p~q~"
End Sub
It sends the keys :
CTRL+p
: To print the current active code module (named Quine in my case).
ENTER
(~) : To press OK in the dialog box.
q+ENTER
: To submit the name of the file.It will use your default printer which is Microsoft Print to PDF
in my case, so I get this:
Big thanks to Chris Rae for the inspiration found here: https://chrisrae.com/vba/routines/printmyself.html
Upvotes: 1
Reputation: 578
Some further modifications of @Dorian I's response gets the bytecount for the VBA quine to come down to 77 bytes
c=Chr(34):q="c=Chr(34):q=:?Replace(q,Chr(7),c+q+c)":?Replace(q,Chr(7),c+q+c)
The key tricks that allow for this to happen are the use of character 7, , over character 35,
#
, and using string addition rather than string concatenation.
Upvotes: 2
Reputation: 46
I don't know if anybody reads this thread anomore but here's an even shorter one, based on the quine of das_weezul. It's independent of "Option Explicit" (unlike das_weezul's) and it's independent of the Office App you are working in (i.e. Excel, Access, Word etc.) - unlike Alex K's. Use it in the immediate window (Ctrl+G):
c="c=#:?replace(c,chr(35),chr(34) &c &chr(34))":?replace(c,chr(35),chr(34) &c &chr(34))
Upvotes: 3
Reputation: 175766
How about
Sub q() '//in mdl1
Debug.Print Workbooks(1).VBProject.VBComponents(5).CodeModule.Lines(1, 3)
End Sub
Upvotes: 7