Disable Macros from Safe Mode - vba

I wrote a macro that now is causing issues with MS Word. I can only open the document in safe mode and cannot figure out how to delete the macro from safe mode. I don't have access to any of the features in safe mode and the macro isn't even showing up. I tried changing the Trust Center settings but none of those options make a difference. Unfortunately I was very dumb and made it a macro on all documents so I can't open any documents without going into safe mode. help!
To be more specific, I believe the code that broke my MS Word was a OnError GoTo ErrorHandler where I tell it to skip to the next if there is an error. I am working on converting equations to a professional format and some of them don't have the right syntax for the conversion so for now I wanted it to ignore those equations and convert the next equation. That is the only change I have made recently that could be the problem.
An additional edit: the macro is coded to run on open. It was running fine before I added the ErrorHandler. I'm sure this is also a huge part of the problem, but a simple solution may not be the right fix since it automatically runs when I open the document. I can't access anything in safe mode.

If you are talking about the "Normal" document in the project explorer, resetting it is easy.
Simply navigate to %AppData%\Microsoft\Templates and either delete or rename the Normal.docm file. Once you reopen Word it will regenerate a brand new file automatically - one that contains no code.

You created an AutoOpen macro which only fires when a document is opened. Presumably this macro is stored in your Normal template.
When you first start Word the macro should not run as you are not opening a document. If Word doesn’t automatically create a new blank document you can safely do so. You will then be able to access and edit the macro you created.

Related

XLTX contains macros, but then they are not there - what gives?

I have an xltx that clearly contains macros when i open it, based on the error "ThisWorkbook.RefreshAll err..etc .etc., due to invalid sql connection".
(whether i open the xltx as New or Open).
Once I go to vba project, there is ZERO code of any kind.
I consider myself a VBA expert, but have to confess, I've never worked at ALL with template files. (they seem like a completely unnecessary thing to me).
is there some magic that makes vba code invisible in xltx ?
You need to use XLTM (Excel Macro-Enabled Template)- the difference is the same as between XLSX and XLSM - you can write whatever code you like, but if you don't save it in the macro-enabled format, it won't still be there when you re-open the workbook/create a new spreadsheet based on the template.
You should have had this error message when you saved (although if you just clicked "Yes" it'll have saved anyway, just without the macro code)
If you haven't closed down the template file you're working on, you should still be able to save it as XLTM and retain the code - otherwise it's gone, unless it can be recovered from an autosave:
There will not be any macros in an .XLTX. You can try renaming the file as .XLSM or .XLTM in file explorer to get them back. The message "ThisWorkbook.RefreshAll err..etc .etc., due to invalid sql connection"." also looks like a broken link to a datasource rather than a macro.

Lock or Freeze VBA Code to Prevent Editing

Is there any way I can lock or freeze the VBE editor to prevent others from accidentally editing the VBA code?
I don't want to lock down the whole workbook where people can run macros but can't see the code.
I want to let others see the code but don't want them accidentally deleting something and break the program (something even I am prone to doing to my code).
Just go to Visual Basic, right click your module, select VBA Project Properties..., under protection tab, you can lock your VBA code with password.
Hope this help. ^^
If you want the code to be visible and executable, then you can't stop people from making changes to it.
You can however, stop them from saving those changes, by opening the workbook as read-only.
You can mark a workbook as read-only in the File Properties from within File Explorer. Or, if your workbook is opened via Automation, then you can specify read-only in the open method.

Prevent Word macro in Normal.dotm for some templates

I have a Normal.dotm file that contains an AutoNew macro.
This macro is automatically executed each time a new document is created using any other template.
Is there any way I can prevent this automatic behavior for a specific template?
I have a Word VSTO add-in running, so I can hook into Word's events, but so far I havn't found a way to prevent this.
I do know that I can prevent macro execution when using templates programmatically, for example like this:
' Disable auto-macros before opening document
wordApplication.WordBasic.DisableAutoMacros(1)
' Open document
newWordDocument = wordApplication.Documents.Open(template.FullName, ConfirmConversions:=False, [ReadOnly]:=True, AddToRecentFiles:=False, Revert:=True)
' Re-enable auto-macros
wordApplication.WordBasic.DisableAutoMacros(0)
But this solution doesn't work when the user uses a Word template from Windows explorer or the Open-dialog in Word, since in those cases I can't execute code before it's too late already.
Or can I?
I hope someone has a trick for me :-)
-
Edit: While trying different solutions, I discovered something that might help others in similar situations, though unfortunately it doesn't help me.
It seems that if a template contains a module containing an AutoNew (or AutoOpen for that matter), that local macro is executed instead of the one in Normal.dotm.
Example:
Normal.dotm contains the following macro:
Sub AutoNew()
MsgBox "Normal.dotm"
End Sub
Test.dotm contains the following macro:
Sub AutoNew()
MsgBox "Test.dotm"
End Sub
When executing Test.dotm the message "Test.dotm" is displayed, while the message "Normal.dotm" is not displayed.
If the AutoNew macro is removed from the Test.dotm template, the message "Normal.dotm" is indeed displayed.
So it is possible to easily override the auto-macros.
The local versions of AutoNew and AutoOpen can even be empty subs that do nothing. It still works.
This is not possible in my case though, since the template I use is generated by code, and cannot contain macros (because adding macros to templates programmatically requires the user to manually activate the option "Trust access to the VBA project object model", and that's something I cannot ask my customers to do for all users. It's also a security risk.)
Based on the workaround described in the "Edit" part of the question - providing a template with "empty" Auto-macros - it's possible to use the Open XML SDK to create a template and add the VBA project to it in order to provide this functionality. This approach avoids the user needing to allow access to the VBA project on his installation. The only "macro security" that could be triggered is that for not allowing macros to run. But since the client uses macros, anyway, this should not be a major obstacle.
The simplest method is to create as much of the basic template as possible in the Word UI and use this as a starting point.
Since you're unfamiliar with the Open XML SDK, the next step would be to create one (or more) templates in the Word UI using the basic template as the starting point, saving under a different file name.
You can then use Open XML SDK Productivity Tool to see the code required to generate any one of these files, as well as, using the Compare tool, the code for converting the basic template to the derived version. This should give you a decent start with the SDK and it's object model. Once you get a feel for how the Open XML SDK works, if you're familiar with Word's object model, using that provided by the SDK is relatively straight-forward as an effort was made to make it correspond as closely as possible to the "COM" object model.
The VBA project can be added later, but you can also include it in the basic template. That would be the simplest approach.
Include this "starting point" basic template as part of your solution, installing it as part of the solution.
Within the AutoNew macro you can check the AttachedTemplate property. Only if it is a template where you want to apply the cleaning you can execute the respective macros.
Sub AutoNew()
If ActiveDocument.AttachedTemplate <> "Normal.dotm" Then
Exit Sub
End If
' rest of the macro
End Sub
If you don't control the Normal.dotm you can put an empty AutoNew macro in your own templates. As Word only executes the auto macro in the closest context, the macro in the Normal.dotm file would not be executed.
If you don't control the other templates either, you can tell your users to hold down the SHIFT key while creating a document. This prevents the execution of the auto macro.
Probably it is best, however, if you ask the owner of the other system to find another solution that does not rely on polluting the Normal.dotm file.

Macro not responding on different computer

I have a macro which works fine on my computer when I click its button but it does not work on different computers.
All the macros on that excel file are not working when clicking on the button.
However, I have a macro working before saving. That means that when I try to save, another macro works properly. (that macro pop up an input message, but is not relevant now). So it seems that macros are not working only when you call them by a click...
The version of Excel is the same one and I enabled the macro when asked.
It is not a problem of the code because even if I create a new simple macro on that file from the different PC (such as:
msgbox("hello")
) it is still not responding.
I had a problem with that excel file in the past and I had to manually delete all the *.exd extensions in my computer. However once I deleted those extensions, the file worked well.
I believe that the excel file is considered corrupted by other computers and they do not enable the macro. This is just my impression but I cannot get why the macro is working when saving and not working when clicking.
Do you have any idea to solve the problem?
I had issues like that myself both with macros and with Excel workbook doing some really funny stuff (like formulas not calculating properly, or functions not working as expected etc) and I did not find any other solution then copying the content of the file and pasting into a newly created file.
In each case this resolved my problem.
It suggests that my files somehow got corrupted and the only solution was to save the contents and delete the files.
Exporting the .frm and .frx files from VBEditor (Modules and Forms) worked fine for me.
Have you tried this yourself?
This may be too simple a suggestion, but have you checked the security settings in Excel? The other computers may be set to a higher security setting that disables all macros without notifying the user.
https://support.office.com/en-US/Article/Enable-or-disable-macros-in-Office-documents-7b4fdd2e-174f-47e2-9611-9efe4f860b12?ui=en-US

Word opens for real when trying to fill-in the blanks dynamically?

I'm currently using Word documents as templates where blanks have to be filled dynamically/programmatically in PwoerBuilder.
This has always worked fine until the company moves on Windows 7.
In short, the Word application is opened and made invisible.
Word.Application.Visible = false
Except that sometimes, and I don't know why, once the template is accessed, Word opens itself just as if I had double-clicked the template byself through the Explorer - but I didn't.
So, it asks whether I want to open it in read-only mode, since the application already has a handle on the file. And even if I click [Cancel] not to open the file, Word opens with no document, then the application crashes.
It reports PowerBuilder System Error 35.
Error Number 35.
Error text = Error calling external object function open at line 24 in function of_fusion of object n_cst_9999.
The external object that the application is trying to call a function against is Word.
oleobject lole_word
lole_word = create oleobject
lole_word = ConnectToNewObject("Word.Application")
lole_word.Documents.Open("templatefile.docx")
It may work for a few documents, and after a few, the problem comes up. This is the first time ever I meet with this issue.
I'll be glad to answer anyone's question who's trying to help.
Will, you may try setting DisplayAlerts and FeatureInstall properties on Word Application object.
That hid most of word alerts for us. (The code is from C# project and may not be exactly what you need)
Word.Application.DisplayAlerts = Word.WdAlertLevel.wdAlertsNone;
Word.Application.FeatureInstall = 0;
You may also try making a copy of the file before opening it to avoid accessing same .docx from different threads - if that may be the case.