das_weezul
das_weezul

Reputation: 6142

The missing quines: Visual Basic (for Applications)

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:

  1. I couldn't find a quine written in VBA
  2. VBA encourages you to write awkward code which makes your brain hurt

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

Answers (4)

DecimalTurn
DecimalTurn

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 :

  1. CTRL+p : To print the current active code module (named Quine in my case).

  2. ENTER (~) : To press OK in the dialog box.

enter image description here

  1. 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:

enter image description here

Big thanks to Chris Rae for the inspiration found here: https://chrisrae.com/vba/routines/printmyself.html

Upvotes: 1

Taylor Raine
Taylor Raine

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

Dorian I
Dorian I

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

Alex K.
Alex K.

Reputation: 175766

How about

Sub q() '//in mdl1
Debug.Print Workbooks(1).VBProject.VBComponents(5).CodeModule.Lines(1, 3)
End Sub

Upvotes: 7

Related Questions