• Home
  • App Store
    • International Users
    • Indian Users
  • Subscribe
  • About Us
  • Productivity
    • Excel
      • VBA
      • Tiny Tips
    • Android
    • WordPress
  • Social Media
  • Misc
    • Cricket
      • IPL Fixtures
      • VIVO IPL 2016 New Face
      • T20 World Cup
      • Cricket World Cup
    • India Elections
      • How Not To Prepare Yourelf For Polling
Menu
  • Home
  • App Store
    • International Users
    • Indian Users
  • Subscribe
  • About Us
  • Productivity
    • Excel
      • VBA
      • Tiny Tips
    • Android
    • WordPress
  • Social Media
  • Misc
    • Cricket
      • IPL Fixtures
      • VIVO IPL 2016 New Face
      • T20 World Cup
      • Cricket World Cup
    • India Elections
      • How Not To Prepare Yourelf For Polling
  • Latest News
    Export Outlook Emails To Excel – VBA Code – 5,700+ Free Downloads
  • Latest News
    Excel To VCF Converter 1.1 – Excel To Vcard – 68,000 Downloads
  • Latest News
    Extract Web Page with Tables – Parse HTML Table – Data Mining Tool
  • Latest News
    Send Mass Email from Excel VBA 1.3 – Multiple Recipients – In Outlook
  • Latest News
    Web Scraping – Extract Data from Website – To Excel VBA
  • Latest News
    VCF To Excel Converter – Open VCF File – Free 28,000+ Downloads
  • Latest News
    Print Screen Automation – Take Screenshot, Save to File and Attach in Excel
  • Latest News
    Excel VBA – Import Data from Another Workbook – Macro to Query Excel
  • Latest News
    How to Telnet from Excel & Autorun Commands
  • Latest News
    Excel VBA – FTP Transfer – Files Download Upload
  • Latest News
    Microsoft Office – Communicator API – Verify Availability Status
  • Latest News
    Excel IP Address Lookup – Bulk GeoIP Lookup
  • Latest News
    Extract URL Links from Sitemap.xml file using Excel
  • Latest News
    Trace Lost Phone – Even Without Internet – Using SMS
  • Latest News
    Universal VCF to Excel and Excel to Vcard Converter
  • Latest News
    Download Facebook Videos – How To Get YouTube Video Downloader?
  • Latest News
    Block Game Requests On Facebook & Candy Crush Invites
  • Latest News
    How to Get stock prices in Excel? – Best Live Stock Ticker
  • Latest News
    How to Get Historical Stock Quotes In Excel – Google Finance
  • Latest News
    JSON Parser – Convert JSON To CSV – Excel VBA Macro
  • Latest News
    How To Screen Record – in IOS, iPhone, Android, Windows, Mac ?
  • Latest News
    How To Remove Duplicate Values in Excel, Google Sheets?
  • Latest News
    Best Bitcoin Trading Platforms – Cryptocurrency Market
  • Latest News
    Lock System at Scheduled Time with Excel
  • Latest News
    How to control Facebook Addiction? & Whatsapp Addcition
  • Latest News
    2018 – Hack Whatsapp Friends – Status Photos
  • Latest News
    Making SORT More Simple and Faster in Excel with Advanced Options
  • Latest News
    WordPress – Recent Posts Widget with Thumbnail – Without Plugins
  • Latest News
    Compare Two Excel Sheets – Highlight Differences – Macro VBA Code
  • Latest News
    Display Related Posts with Thumbnails in WordPress without Plugins
  • Latest News
    Post to Facebook Timeline through Email
  • Latest News
    100 CryptoCurrency Price – Ticker in Excel – Using VBA
  • Latest News
    Engineering Unit Converter Download – Excel Table – Feet To Meters
  • Latest News
    Android ICS File Viewer – Calendar file View in Exce;
  • Latest News
    Download – Backup Facebook Twitter Gplus Linkedin – Data To PC
  • Latest News
    How to remove #n/a in Excel? – Fixing Vlookup #n/a
  • Latest News
    #NoForwardsDay – New WhatsApp Texting Pattern that Would Make You Go Crazy
  • Latest News
    Sudoku Excel Download – Puzzle Solver – Free Game Help
  • Latest News
    Top 4 Whatsapp Tips and Tricks You Didn’t know Existed
  • Latest News
    Mortgage Calculator – Loan EMI Scheduler
  • Latest News
    Learn How To Code JavaScript in VBA
  • Latest News
    How To Open VCF File – Online VCF Viewers – 4 Easy Ways
  • Latest News
    How To Block Unwanted Text Messages in Iphone?
  • Latest News
    How To Scrap A Website Keywords? – Keyword Scraping Excel
  • Latest News
    Login to Facebook, Twitter, Google+ in IE from Excel
  • Latest News
    Excel Instant Messenger – Microsoft Office Communicator API Library
  • Latest News
    Skyrocket Website Traffic With Reddit
  • Latest News
    Excel Games Free Download – 2048 Tile Game
  • Latest News
    How to Speed Up Google Chrome
  • Latest News
    VBA Code – Create Index of Worksheets – with Links to each Sheet

Excel VBA to Shutdown Computer or Logoff User

  • Excel VBA Shutdown Computer

    Note: Save all your unsaved Documents before Testing this Code

    There is no escaping with this code. This VBA Shutdown, log off or Reboot the computer, sometimes, even without asking you for any confirmation to save the unsaved work. Always save your “in progress” work and then hit run.

    I myself ended up in rebooting the system with this code multiple times, before I publish this code. Good that I saved all my MS Word, Excel and Notepad documents that I was working on.

    Where can we use this?

    • Save Power: Many times, we leave office just by locking the computer. The electricity is still used up to keep the desktops up and running. You can use this code to set a time limit (example: 10.00 pm or 11.00 pm) in excel and if that time limit is reached, this code can be used to shutdown workstations.
    • Limit Working Hours: Are you so addicted to sit for long time with computer at home. Set a time limit to shutdown and spend some time with Family.

    Well, I have given 2 examples. Now use your imagination further to find where this code can be used. If you come up with any interesting usage with this code, post your comments or article link at comments section.

    Lets get to the actual Excel VBA PC shutdown code. The same code can be used in VB also.

    Visual Basic
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    'Excel VBA code to Shutdown Computer
    'For More Info search for these Windows Keywords
    'EWX_LogOff
    'EWX_SHUTDOWN
    'EWX_REBOOT
    'EWX_FORCE
    'EWX_POWEROFF
            
    'ExitWindowsEx Windows API Function to perform the following
    'in Computers running with Windows Operating System
    '(1) Logs off        - Code: 0
    '(2) Shuts down      - Code: 1
    '(3) Reboot          - Code: 2
    '(4) Forced Process  - Code: 4
    '(4) Power Off       - Code: 8
    '(5) Force if Hung   - Code: 10
     
    ''''API Function Declaration
    Public Declare Function ExitWindowsEx Lib "user32" _
        (ByVal dwOptions As Long, _
        ByVal dwReserved As Long) As Long
     
    ''''Call the below Functions to Perform Log off or Shutdown or Reboot
    Private Sub Excel_VBA_Log_Off_Computer()
        ExitWindowsEx 4, 0
    End Sub
     
    Private Sub Excel_VBA_ShutDown_Computer()
        ExitWindowsEx 1, 0
    End Sub
     
    Private Sub Excel_VBA_Reboot_Computer()
        ExitWindowsEx 2, 0
    End Sub


    Note: Add the force flag code to normal process code to force complete an operation.

    For example: If you want a normal shutdown use ‘1’. If it is force Shutdown then use ‘5’ i.e., Shutdown Code(1) + Force Complete Code(4).

    Steps to Execute the VBA Shutdown Code

    • Create a new workbook and press Alt F11 to view VB Editor.
    • Copy and paste the above code.
    • Save this workbook as Macro Enables workbook and also, save all other unsaved documents.
    • Execute the code by pressing Alt F5. System will start to Log off.

    MSDN References:

    http://msdn.microsoft.com/en-us/library/windows/desktop/aa376868(v=vs.85).aspx

    Related Topics:

    1. How to set Timer and lock a computer with Excel?
    2. How to set timer in Excel to execute any code?

    Author: Kumarapush

    Kumarapush has experience in IT industry for 12+ Years including 10 Years in MNC. Has vast expertise in Microsoft Excel automation using VBA Macro. Excel to CSV converter is one of his famous tool that has been downloaded & used by 68,000 users around the globe.

    View all posts by Kumarapush | Website


    Previous Post: Lock System at Scheduled Time with Excel
    Next Post: Find Synonym for Selected Text using MS Word VBA


Post Categories
Popular Posts
Export Outlook Emails To Excel...
Excel To VCF Converter 1.1 –...
Extract Web Page with Tables –...
Send Mass Email from Excel VBA...
VCF To Excel Converter –...
Web Scraping – Extract Data...
Print Screen Automation –...
Excel VBA – Import Data from...
How to Telnet from Excel &...
Excel VBA – FTP Transfer...
Microsoft Office – Communicator...
Excel IP Address Lookup –...
Recent Posts
Enable Insert button in Excel greyed disabled show
Excel Insert Button is Disabled...
Excel VBA Find String in Range Return Row Change Background color Remove Duplicates
Excel VBA - Find String in Range...
Excel VBA Convert Amount in Numbers to Words US Dollar Million Indian Rupee Lakhs Crores finance
Excel VBA Convert - Amount (Numbers)...
Excel VBA Code Get Names of All open workbook path Loop through
Excel VBA - Loop Through All Open...
Name Numerology Lucky Number Calculator Baby names Business name
Name Numerology Calculator - Lucky...
Similar Pages
  • Excel VBA Code Get Names of All open workbook path Loop through
    Excel VBA – Loop Through All...
  • Name Numerology Lucky Number Calculator Baby names Business name
    Name Numerology Calculator –...
  • How To type Subscript in Excel Superscript strikethrough underline font
    How To Type Subscript in Excel –...
  • Excel VBA Merge Cells Unmerge Range Selection objects
    Excel VBA – Merge Cells –...
  • Excel VBA Convert Column Letter Alphabet to Number
    Excel VBA – Convert Column...
  • Excel VBA Convert Column Number to Letter Formula
    Excel VBA – Convert Column...
Popular Tags
  • Email Marketing Block Apps in FaceBook Task Manager print screen VCF HTML Table Import Data Candy Crush Outlook Record Macro android app development SEO Convert URL to IP HTML/ Javascript QR Code Generation VCARD Timer in Excel Countif Google+ bitcoin vba color index vba color codes Data Mining Android social media Twitter bulk email Google Adsense Tips Excel VBA Calendar in Excel Excel Macro WhatsApp Facebook mass email outlook vba Wordpress
FREE Tech Updates

Home

  • Home
  • App Store
    • International Users
    • Indian Users
  • Subscribe
  • About Us
  • Productivity
    • Excel
      • VBA
      • Tiny Tips
    • Android
    • WordPress
  • Social Media
  • Misc
    • Cricket
      • IPL Fixtures
      • VIVO IPL 2016 New Face
      • T20 World Cup
      • Cricket World Cup
    • India Elections
      • How Not To Prepare Yourelf For Polling

Trending Topics

    • Learn How to Access WhatsApp in Web
    • Compare Data In Two Excel Sheets
    • Find IP Address Location – GeoIP Lookup
    • Lock a Folder In Windows Using This Simple Excel Macro Trick
    • Download Free Tool to Convert VCF Contacts to Excel

Join Us

Copyright © 2018 OfficeTricks.com