serhat
serhat

Reputation: 51

Running Macros without opening excel

I wonder how would you assign VBA codes written on Excel VBA to a sort of procedure/programme or maybe dos related filepath, which you can directly without opening excel. In other word, i want to have a desktop icon i can stir up a vba code i assigned to.

Upvotes: 4

Views: 70164

Answers (5)

Julien Kronegg
Julien Kronegg

Reputation: 5262

You can do it easily.

Add the following content in a VBS file (e.g. example.vbs). This is only a text file that you can write using Notepad:

'Code should be placed in a .vbs file
Set objExcel = CreateObject("Excel.Application")
objExcel.Application.Run "'C:\path\to\my\excel\file\myExcelMacroFile.xlsm'!MyModule.MyFunctionName"
objExcel.DisplayAlerts = False
objExcel.Application.Quit
Set objExcel = Nothing

Then you can double clic on the VBS file to execute it.

Source: http://wellsr.com/vba/2015/excel/run-macro-without-opening-excel-using-vbscript/

Upvotes: 8

Serdar
Serdar

Reputation: 1516

write a cmd or ps1 that opens an excel and in that excel's startup run your macro... and then when finished close it.

this can be a solution but you are probably doing something which is unnecessary in correct planned environments.

Upvotes: 0

Robert Ilbrink
Robert Ilbrink

Reputation: 7953

Though the learning curve may be a bit steep, but you could consider using AutoHotKey. This allows you to create your own scripts and if so desired turn them into (rather large) .exe files. AutoHotKey is free!

Upvotes: 0

Robert Co
Robert Co

Reputation: 1715

If the VBA in the Excel macro doesn't reference Excel objects, you can just copy the code into a text file and change the extension to .VBS. However, VB script doesn't like it when you use types, just delete the "AS something" from your Dim statement.

I do this often to get the benefit of Intellisense, which I wouldn't have using Notepad.

If my assumption is correct, then you probably want to change your tags to VB Scripting instead of Excel to get appropriate help.

Upvotes: 3

KekuSemau
KekuSemau

Reputation: 6856

If I understand that right, you can simply write VBS code in a text file and rename it to .vbs (make sure file endings are visible in Windows). On doubleclick the file is executed by the Windows Scripting Host. VBS lacks some functionality of VBA but you can do a lot with CreateObject/GetObject.

Upvotes: 2

Related Questions