word add in cached and not able to remove it - vba

there was originally an add-in installed for word, under the path
D:\User\UserName\AppData\Microsoft\Word\Startup\addin.dot
it is working fine, and it add to Word menu with a name addin_2017. Since it is already 2021, we figured its better to update the menu name, so we changed the name to addin_v2 instead. However, when we place the updated addin.dot into the folder, both name showed up in the Word add-in menu. addin_2017, and addin_v2. I am wondering if there are anything cached somewhere.
I have also tried to use VBA code to remove all the add-in, but when I copy the file back to the STARTUP folder, there are two items in the menu still.

Looking back at some (very old) Wd2003 projects the method that I used was to have two separate routines for deleting and creating menus. These were called when the add-in was loaded and unloaded, e.g.
Public Sub AutoExec()
DeleteMenu
CreateMenu
End Sub
Public Sub AutoExit()
DeleteMenu
End Sub
Public Sub DeleteMenu()
Dim cbc As Office.CommandBarControl
For Each cbc In Application.CommandBars("Menu Bar")
If cbc.Caption = "AddinV2" Or cbc.Caption = "Addin2017" Then cbc.Delete
Next cbc
End Sub
It is obvious from your investigations that the rogue menu exists in the addin. One option to remove it is to:
Create a new template
Export all the code modules from the existing addin
Import the modules into the new template.
This should be seen as only a (very) temporary measure until you have created a new add-in with a custom ribbon tab.

Related

Need code help on calling a macro from a new VTSO addin for Word

I have created a new addin with a ribbon in MVS. On click of button1 I want to run a macro that is stored in a .dotm file in the Startup folder in Word. The .dotm file is called MyMacros and the macro is titled "TableMacro".
The module name in Word is titled NewMacros and the top rows of the macro in Word are:
Sub TableMacro()
`
` TableMacro
I am sure the macro is started with the code below but even this is guess:
Private Sub Button1_Click_1(sender As Obeject, e As RibbonControlEventArgs) Handles Button1.Click
`code to call TableMacro'
End Sub
I know how to write macros but I have no idea the code needed to trigger the macro stored in the MyMacros.dotm file.
To search all global templates, including the Building Block template, from a VSTO add-in, you can use this:
Dim wApp = Globals.ThisAddIn.Application
Dim i As Integer, Tmplt As Word.Template = Nothing
For i = 1 To wApp.Templates.Count
If wApp.Templates(i).Name = "MyMacros.dotm" Then
Tmplt = wApp.Templates(i)
wApp.Run(Tmplt.Name & "!TableMacro")
End If
Next
The value of performing it this way is you now have an object variable set to a specific global template and you can then get at AutoText, Styles, etc. and of course macros that are stored in that specific global template.
Your VSTO code has a Microsoft.Office.Interop.Word.Application object. Say you're storing that reference in a variable named hostApp, you could do this:
hostApp.Run("TableMacro")
That requires the .dotm file to be the "active" document. If the document isn't active and you have a reference to it (say, theDocument), I think this might work (untested):
hostApp.Run(theDocument.Name & "!TableMacro")
The object VB.NET uses is the same one VBA uses, so if Application.Run "MyMacros!TableMacro" works in VBA, it will work in VB.NET. I'd try to fiddle in VBA first to get the syntax right - you get instant feedback, vs needing to build and launch the host, load the add-in and test the thing with VSTO.
The following Run syntax worked for me from within a VSTO Add-in to run VBA code in a Template loaded as an add-in. It uses the module name plus the macro name.
Keep in mind that Run can only work with public subs...
Globals.ThisAddIn.Application.Run("Module1.TestPublicVarx")

Disable Save As but not Save in Word 2010

I am looking to disable Save As in a Word 2010 file but still allow save. In other words I want users to be able to update the existing file but not create copies. I realize that this is impossible to truly do for people who know workarounds but for the general user I have successfully done this in Excel but am pretty new to word VBA.
When I add the following to a brand new document everything works as intended:
Sub FileSaveAs()
MsgBox "Copies of this file cannot be created. Please save changes in the original document." & _
, , "Copy Cannot be Created"
End Sub
My document has other macros for various command buttons but none of them involve saving the document (under original name or save as). There is also a macro running on open but that is 1 line going to a bookmark. When I try to "save as" in this document I get the message box as intended. When I try to "save" though things get strange: I get the save as dialogue (problem 1). Whether I try to save either under same name or other name the dialogue behaves as it normally would except it doesn't save and the dialogue box opens again automatically essentially creating an endless loop until I hit cancel (problem 2). I also intermittently get a "disk is full" warning pop-up after trying to save which I can dismiss but appears a few minutes later as long as he file is open (perhaps related to autosave?)
Since the macro works in the test file I assumed this strange behavior must be something elsewhere in my code but my document with the other macros saves normally as long as I don't include the save as code above so now I'm totally confused. Before I put up the rest of my code which is lengthy and for the reasons stated above I would not think impact things, I figured I'd ask this:
1. Is there any place other than my other command button macros that could be causing this behavior?
2. Is there a better method people recommend to achieve my ultimate goal of disabling save as but not save?
Thanks in advance for any advice you can provide.
The Word application has a DocumentBeforeSave event. To enable application events I suggest to create a class module by the name of ThisApplication and paste the following code into it.
Option Explicit
Private WithEvents App As Application
Private Sub Class_Initialize()
Set App = Word.Application
End Sub
Private Sub App_DocumentBeforeSave(ByVal Doc As Document, _
SaveAsUI As Boolean, _
Cancel As Boolean)
If SaveAsUI Then
MsgBox "Please always use the ""Save"" command" & vbCr & _
"to save this file.", _
vbExclamation, "SaveAs is not allowed"
Cancel = True
End If
End Sub
Add the following code to your ThisDocument module.
Dim WdApp As ThisApplication
Private Sub Document_Open()
Set WdApp = New ThisApplication
End Sub
You may add the Set App = ... line to your existing Document_Open procedure. After the WdApp variable has been initialised all application events will be received by the ThisApplication class where the DocumentBeforeSave event procedure is programmed not to allow SaveAs.
Of course, this is a blanket refusal for all documents. Therefore you may wish to add code to the procedure to limit the restriction to certain documents only. The proc receives the entire document object with all its properties, including Name, Path, FullName and built-in as well as custom properties. You can identify the files you wish to be affected by any of these.
Note that the WdApp variable will be erased in case of a program crash. If this happens the application events will no longer fire. It may be useful to know that application events occur before document events. This is if you wish to use the application's DocumentOpen event as well as or instead of the document's Document_Open event.

Excel-2010 - CustomUI - Backstage : Errors when various files are opened in the same instance

Firstly, i'm very happy to join the community. I hope we could often exchange advice. I'm french so excuse me for the mistakes in the sentences.
I try to explain my problem :
I had the "good" idea to use the Backstage of my Excel file to create a small dashboard. It works very well.
The problem arises when this file is opened at the same time as another file, in the same instance of Excel. The second file is trying to access functions "Backstage_OnShow" and "Backstage_OnHide" of my workbook so I have a message "Impossible to run the macro 'Backstage_OnShow' (or 'Backstage_OnHide'). It is possible that the macro is not available in this workbook ..." <- This is a translation to the french error message.
How can I do to not have this message or rather to ensure that the backstage is specific to my file and not the instance of Excel?
I show with my code snippets. It will be more clear.
In my XML, I have this :
<customUI xmlns="http://schemas.microsoft.com/office/2009/07/customui" onLoad ="Ribbon_Load">
<ribbon startFromScratch="false"/>
<backstage onShow="Backstage_onShow" onHide="Backstage_onHide">
...
</backstage>
</customUI>
In my Excel file, i have this :
Public Sub Ribbon_Load(ribbon As IRibbonUI)
Set Ruban = ribbon
End Sub
Public Sub Backstage_onShow(ByVal contextObject As Object)
'Rafraichissement du ruban
Ruban.Invalidate
End Sub
Public Sub Backstage_onHide(ByVal contextObject As Object)
End Sub
All this is contained in an Excel file, which is normal.
In Excel 2010, Excel files open by default in the same instance, which does not bother me, but, when a "normal" file is opened in the same instance as my customized backstage file, the normal file tries, I do not know by what miracle, to access the function Backstage_onShow Backstage_onHide and as soon as I display its backstage. However, this file should not even knows these functions exist because they are not reported for him.
Thank you in advance.
Sincerely,
Patrice.
PS : this is a link if you want to show my file. It's a safe code snippets of course !
#David, i have a solution !!!!
Thanks you to have take your time to answer me !!!
I try to explain you.
So, if we think a few moment, we note the problem is we need a stated place to store our code snippets. This place is in the XLSTART !!! If we use the PERSONAL.XLSB to store that :
Public Sub Ribbon_Load(ribbon As IRibbonUI)
Set Ruban = ribbon
End Sub
Public Sub Backstage_onShow(ByVal contextObject As Object)
'Rafraichissement du ruban
Ruban.Invalidate
End Sub
Public Sub Backstage_onHide(ByVal contextObject As Object)
'ErreurSaisieTaux = 0
End Sub
After, we can modify the customui.xml like that :
<customUI xmlns="http://schemas.microsoft.com/office/2009/07/customui" onLoad ="PERSONAL.XLSB!Ribbon_Load">
<ribbon startFromScratch="false"/>
<backstage onShow="PERSONAL.XLSB!Backstage_onShow" onHide="PERSONAL.XLSB!Backstage_onHide">
...
</backstage>
</customUI>
I have tried and it works.
What do you think about this idea ? Do you see drawbacks with this method ?
Patrice.
The Backstage is part of the Application, and you have specified that certain macros should be called for the onShow and onHide events.
Excel will always assume that an unqualified macro, e.g., Backstage_onShow should be found within scope of the ActiveWorkbook. When this macro does not exist in the ActiveWorkbook, the error raises.
To resolve this, revise the XML so that the macro call qualifies a specific workbook:
<customUI xmlns="http://schemas.microsoft.com/office/2009/07/customui" onLoad ="Ribbon_Load">
<ribbon startFromScratch="false"/>
<backstage onShow="Exemple.xltm!Backstage_onShow" onHide="Exemple.xltm!Backstage_onHide">
...
</backstage>
</customUI>
Regarding Whether This Method Can Work on an XLTM File
Follow-up from comments:
When you use the above method on an XLSM file, the file which contains the Backstage_OnShow macro is open, and so calls to Exemple.xlsm!Backstage_OnShow work, because Exemple.xlsm is an open workbook file.
When you use this method on an XLTM file, to create a new file from template, the explicit XML with onShow ="Exemple.xltm!Backstage_OnShow will fail because the macro cannot be found. The macro cannot be found because Exemple.xltm is not a valid member of the Workbooks collection.
If, as you previously attempt, the XML does not fully qualify the macro (e.g., onShow="Backstage_OnShow), you will get the same error if you navigate to any open workbook which was not created by the XLTM, for the same reason: the macros cannot be found. In this case, the macro cannot be found in the scope of ActiveWorkbook.
It seems this simply may not work with an XLTM file (at least not without considerable effort)
You might be able to make it work if, instead of hijacking the Backstage you create a custom tab.
Alternatively, I think you would have to modify the Ribbon XML for each new instance of the file, and while it is possible to modify the Ribbon XML, I think it is not possible to do while the file is open.
Best solution I can think of would be to create an add-in or macro that does:
1) Prompt user for new filename
2) Create new file from template, save & close it
3) Modify the contents of the new file's Ribbon XML
4) Open the new file
You could see this for information about unpacking and modifying the XML:
http://www.jkp-ads.com/articles/Excel2007FileFormat02.asp)

Leaving a Project file open after retrieving it with GetObject

What is the right way to leave an MS Project file opened with GetObject() open and visible to the user after the end of a macro in a different app?
The information I found online suggests that setting the Application.UserControl property to True before the objects go out of scope should allow the user to continue using the opened file. However, for MS Project at least, the Application.UserControl property appears to be read-only. Is there a way to work around this?
A simplified example showing the problem:
Sub AddTasks()
Dim proj As Object
' Already have the file path from another part of the workflow
Set proj = GetObject("C:\projtest.mpp")
' perform some calculations and add new tasks to project
proj.Tasks.Add "additional task"
' Leave Project open and visible for the user
proj.Application.Visible = True
proj.Application.UserControl = True ' Gives "Type Mismatch" error
' without the UserControl line, runs ok, but Project closes after the end of the macro
End Sub
Instead of using GetObject, could you create an instance of the application and open the project file in the instance?
Sub AddTasks()
Dim msProj as Object
Set msProj = CreateObject("Project.Application")
msProj.FileOpen "C:\projtest.mpp"
'do stuff to project file here
msProj.Visible = True
End Sub
Something like the above (I can't test the above code because I don't have MSProject, but similar code works for MSWord)
For Project UserControl just indicates if the user started the application or not; it appears to be read-only because it is. I've not done what you're asking for with Project, although here is a similar example for Word trying to see and find running instances of Excel. Perhaps this helps a little:
can-vba-reach-across-instances-of-excel

Get me started programming and debugging Microsoft Office automation

I'm using Microsoft Office 2003 and creating a bunch of template documents to standardize some tasks. I asked this on Superuser.com and got no response so I'm thinking it's too program-y and hoping I'll have better luck here.
I need to automate a work flow that uses a bunch of Office (mostly Word) templates. What I want is to have "My Template Foo.dot" and "My Template Bar.dot", etc. in the "My Foo Bar Stuff" on a shared drive and have users double click on a template to create a new Foo or Bar.
What's I'd really like is for the user to double-click on the Foo template and be prompted for a couple of items related to their task (e.g., a project number) and have a script in the template change the name that Save will default to something like "Foo for Project 1234.doc".
I asked on Google Groups and got an answer that worked....for a while. Then my AutoNew macro stopped kicking in when I created a new document by double-clicking on the template. I have no idea why or how to debug it.
In Class Modules/This Application, I have:
Sub AutoNew()
Dim Project As String
Project = InputBox("Enter the Project Number")
ActiveDocument.SaveAs "Project " & Project & " Notes.doc"
End Sub
In Microsoft Word Objects/ThisDocument, I have:
Private Sub Document_New()
End Sub
I really have no idea why or where that came from.
In Tools/Macro Security... I have Security Level set to "Low".
I'm a software engineering with 25+ years of experience but a complete Office automation noob. Specific solutions and pointers to "this is how to automate Word" FAQs are welcome. Thanks.
Update: If I create a new template (New..., Blank Document, Save As "My New Template.dot"), and insert the AutoNew() macro, it works. So what's inhibiting it from working on my existing template?
Update 2: Removing the module and function from my old template and adding it back works, too.
You can attach a template to a saved document in ordre to access the macros contained in the template in question.
You can do this with the AttachedTemplate property of a Document object (i.e. ActiveDocument).
Please note that I did not try this myself.
Sub AutoNew()
Dim Project As String
Project = InputBox("Enter the Project Number")
ActiveDocument.SaveAs "Project " & Project & " Notes.doc"
ActiveDocument.AttachedTemplate = "\\path\to\templates\My Template Foo.dot"
End Sub
See MSDN - Word 2003 VBA Language Reference - AttachedTemplate Property
Hope that helps.
Check if the macro is still contained in your template. This sounds stupid but it happended to me, too, in Word 2003 under the following circumstances:
Create a template containing macro,
everything fine
Create a new document file based on
the macro, macro kicks in
Notice I could improve the template
here and there a bit, I do it in the
file created in 2) and SaveAs .DOT
Macro in .DOT GONE!
Why? Because the code stored in the .DOT doesn't go over to the doc file.