Last active
September 29, 2021 05:44
-
-
Save 18520339/afd5cc6ccf314e1b75098a68a98bf117 to your computer and use it in GitHub Desktop.
Enable VBA Macros for Excel automatically
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Microsoft.Win32; | |
using System.Diagnostics; | |
using System.IO; | |
namespace ConsoleApp { | |
class Program { | |
static void Main(string[] args) { | |
// https://www.kunal-chowdhury.com/2016/06/how-to-retrieve-office-version.html | |
// Enable for default Office version | |
string regOutlook32Bit = @"SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\OUTLOOK.EXE"; | |
string regOutlook64Bit = @"SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\App Paths\OUTLOOK.EXE"; | |
string outlookPath = Registry.LocalMachine.OpenSubKey(regOutlook32Bit).GetValue("", "").ToString(); | |
if (string.IsNullOrEmpty(outlookPath)) | |
outlookPath = Registry.LocalMachine.OpenSubKey(regOutlook64Bit).GetValue("", "").ToString(); | |
if (!string.IsNullOrEmpty(outlookPath) && File.Exists(outlookPath)) { | |
FileVersionInfo outlookFileInfo = FileVersionInfo.GetVersionInfo(outlookPath); | |
string officeVersion = outlookFileInfo.FileVersion.Split('.')[0] + ".0"; | |
string subKey = $@"SOFTWARE\Microsoft\Office\{officeVersion}\Excel\Security"; | |
RegistryKey key = Registry.CurrentUser.CreateSubKey(subKey); | |
if (officeVersion == "11.0") key.SetValue("Level", 1, RegistryValueKind.DWord); | |
else key.SetValue("VBAWarnings", 1, RegistryValueKind.DWord); | |
key.Close(); | |
} | |
// Enable for all Office versions | |
//int[] versions = new int[5] { 11, 12, 14, 15, 16 }; | |
//try { | |
// foreach (int ver in versions) { | |
// string subKey = $@"SOFTWARE\Microsoft\Office\{ver}.0\Excel\Security"; | |
// RegistryKey key = Registry.CurrentUser.CreateSubKey(subKey); | |
// if (ver == 11) key.SetValue("Level", 1, RegistryValueKind.DWord); | |
// else key.SetValue("VBAWarnings", 1, RegistryValueKind.DWord); | |
// key.Close(); | |
// } | |
//} catch (Exception ex) { } | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment