I'm wondering if there is a way of programmatically opening an Outlook Shortcut from my addin.
I've created the shortcut as follows
Sub AddShortcut()
Dim myOlBar As Outlook.OutlookBarPane
Dim myolGroup As Outlook.OutlookBarGroup
Dim myOlShortcuts As Outlook.OutlookBarShortcuts
myOlBar = Application.ActiveExplorer.panes.Item("OutlookBar")
myolGroup = myOlBar.Contents.Groups.Item(1)
myOlShortcuts = myolGroup.Shortcuts
myOlShortcuts.Add("http://microsoft.com/", _
"MSHomepage", 1)
End Sub
I'm guessing I need to use InvokeMember in some way
myOlShortcuts("MSHomepage").GetType().InvokeMember(..)
But when I use GetType().GetMethods() I can't see any Click members or something similar. Any help is very much appreciated.
If you want to trigger the Outlook integrated web browser, you should grab the OutlookBarShortcut.Target. If the Target is of type string, then use the following (substituting your Target for the address Text)...
Office.CommandBarComboBox address = (Office.CommandBarComboBox)Application.ActiveExplorer().CommandBars.FindControl(26, 1740);
address.Text = "http://www.stackoverflow.com";
otherwise the type is Folder and you should assign Explorer.CurrentFolder. The only downside with this approach is that CommandBars have been deprecated with Outlook 2010 and this solution likely won't work in the next version of Office.
Another alternative is to use Web Folder behavior as discussed in this SO post. You could create a hidden Folder used just for the purposes of navigation.
Related
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.
I have a VSTO on MS Project. I use VB.NET. What I need is when I press the button I created on the ribbon, it will perform some codes which will update the info of some task, however, I would need to close the MS Project automatically. I tried application.FileCloseEx(), but it only closes the file, the MS Project is still loaded. I need similar to clicking the x button of the window.
Thanks,
Gilbert
If your MS Project application object is represented by "appMSProject" then it's as simple as:
appMSProject.Quit
OR say in a macro running under Project:
Application.Quit
Here's how I do it in VBA from Excel or Access. As far as I can tell the objects & methods are the same in VB.NET. Bottom line is that I create an instance of the MS Project object which starts the app & opens a file, execute some work, close the file, then destroy the MS Project object by setting it to Nothing. That has the effect of closing the app. You can also use "appMSProject.Quit" followed by setting it to Nothing. Frankly the 2nd option looks more orderly & easier to understand in code. Anyway, here's a sample of the way I do it:
Dim appMSProject As MSProject.Application
Dim prjPrj As MSProject.Project
Dim strPrjFile As String
strPrjFile = "C:\where_is_my_file\file_name.mpp"
Set appMSProject = New MSProject.Application
appMSProject.FileOpenEx Name:=strPrjFile
Set prjPrj = appMSProject.ActiveProject
'''Do something in here with the prjPrj
'Close the file, in my case w/o saving
appMSProject.FileCloseEx pjDoNotSave
'Destroy the objects
Set prjPrj = Nothing
Set appMSProject = Nothing
FYI - In this example I'm doing background work so I don't show the app. I also use "early binding".
Here's an MSDN example that does show the app with more info on early -vs- late binding - https://msdn.microsoft.com/en-us/library/office/ff865152.aspx
Currently at the office we have Outlook 2003. We will be migrating to Outlook 2013.
In Outlook 2003 we have a commandbar that as example saves a mail item to a user specified folder or moves the item to the desired team.
In a userform the end-user can set his settings to his desired folder or select the team he is currently on. In this settings form there are multiple input field the user can fillout.
Whenever he clicks a button on the commandbar, outlook checks his settings to see on what team he is on, his desired save folder is, etc.
This userdefined settings are stored and called on by it's tags
(Application.ActiveExplorer.CommandBars("Toolbar").Controls.Item(1).tag)
As far i found on the internet Outlook 2013 does not support commandbars. I can instal the commandBar, but as soon as you restart outlook the bar is gone and the settings are gone.
Is there a way to save/store the settings made by the end-user in a userform so the scripts saves the mail item based on his settings to the correct folder or team?
I've tried to find a solution but haven't found it yet, or do not know where to look.
Hope you can guide me into the right direction to look for a solution.
(note: I know a little bit of VBA, can read and write it, but found it hard to explain how it works. If i left out some critical information in the question please let me know.)
Outlook doesn't allow to customize the Ribbon UI using VBA. The only thing you can do is to assign a macro to QAT button (or add controls manually in Outlook).
You need to develop an add-in to be able to customize the Ribbon UI (aka Fluent UI). See Walkthrough: Creating a Custom Tab by Using the Ribbon Designer for more information.
Read more about the Fluent UI in the following series of articles in MSDN:
Customizing the 2007 Office Fluent Ribbon for Developers (Part 1 of 3)
Customizing the 2007 Office Fluent Ribbon for Developers (Part 2 of 3)
Customizing the 2007 Office Fluent Ribbon for Developers (Part 3 of 3)
Is there a way to save/store the settings made by the end-user in a userform so the scripts saves the mail item based on his settings to the correct folder or team?
Using the Tag property is not the best way to store the user settings. Of course, you can standard ways for storing settings on the PC - files (XML, text or your own binary format), windows registry and etc.
But the Outlook object model provides hidden items for that. The GetStorage method of the Folder class returns a StorageItem object on the parent Folder to store data for an Outlook solution. See Storing Data for Solutions for more information.
As promised a few code samples wich i used to store and get the settings.
Maybe not the best way to do it, but it solved my problem to store the settings and maybe it could help someone else.
First of all I made a little check to see if the settings are already there.
Function Hidden_Settings_Aanwezig() As Boolean
Dim oNs As Outlook.Namespace
Dim oFL As Outlook.folder
Dim oItem As Outlook.StorageItem
On Error GoTo OL_Error
Set oNs = Application.GetNamespace("MAPI")
Set oFld = oNs.GetDefaultFolder(olFolderInbox)
Set oItem = oFld.GetStorage("Hidden Settings", olIdentifyBySubject)
If oItem.Size <> 0 Then
Hidden_Settings_Aanwezig = True
Else
Hidden_Settings_Aanwezig = False
End If
Exit Function
OL_Error:
MsgBox (Err.Description)
Err.Clear
End Function
If not, the following code creates the settings based on tekstboxes and checkboxes on a userform with the following code
Function Maak_Settings_Hidden()
Dim oNs As Outlook.Namespace
Dim oFld As Outlook.folder
Dim oSItem As Outlook.StorageItem
On Error GoTo OL_Error
Set oFld = Application.Session.GetDefaultFolder(olFolderInbox)
Set oSItem = oFld.GetStorage("Hidden Settings", olIdentifyBySubject)
'repeat the next to lines for every setting you want to store
oSItem.UserProperties.Add "Export Folder", olText
oSItem.UserProperties("Export Folder").Value = TextBox1.Text
oSItem.Save
Exit Function
OL_Error:
MsgBox (Err.Description)
Err.Clear
End Function
The functions above are called on with the following code:
If Hidden_Settings_Aanwezig = True Then
Call Get_Hidden_Settings_Startup
Else
Maak_Settings_Hidden
End If
To use one of the settings i use the following code.
In the main sub I use the following line:
DestFolder = Get_Hidden_Settings("Export Folder")
To call on this function:
Function Get_Hidden_Settings(Setting) As String
Dim oNs As Outlook.Namespace
Dim oFL As Outlook.folder
Dim oItem As Outlook.StorageItem
On Error GoTo OL_Error
Set oNs = Application.GetNamespace("MAPI")
Set oFld = oNs.GetDefaultFolder(olFolderInbox)
Set oItem = oFld.GetStorage("Hidden Settings", olIdentifyBySubject)
If oItem.Size <> 0 Then
Get_Hidden_Settings = oItem.UserProperties(Setting)
End If
Exit Function
OL_Error:
MsgBox (Err.Description)
Err.Clear
End Function
If I understand your problem correctly, what I would do is the following:
1) Export your VBA stuff into a *.bas files (for modules) and *.frx (for user forms) This is done in the VBA editor, File --> Export. You do this for each item (module and user form). Save these files e.g. on a memory stick, or whereever it suits you.
2) Import these files in Outlook 2013 into the VBA editor (same way, but --> File --> Import of course) e.g. by loading them from your memory stick.
This should make your VBA code available in your new Outlook 2013 environment.
3) Your command bars will not be available. But you can easily create something else: In the Office 2013 (etc.) products, you can add stuff to the "Ribbon". E.g. you can create a new tab called "My self-made tools", and you can place buttons there that call your VBA procedures. There you will find buttons for "Create new..."
To do so: --> File --> Optiobs --> Customize Ribbon --> Macros
Note: In a standard installation of Office 2013 (etc.) you will not have access to the VBA editor. To make the editor available, go through --> File --> Options --> Customize Ribbon and set a tick mark in the field for "Develooper tools". This will make a tab of that name appear in the "Ribbon".
I have a Word template (suggestion from) which includes an autonew macro to insert a reference number at a book mark and an action button (Submit)which saves the resulting document with the reference number as part of the file name and closes Word. This works perfectly well when opening the template via Windows Explorer.
We also have a PowerPoint show with action settings hyperlinking to various documents. The link will open the above template OK but does not insert the reference number. Also when the 'submit' button is hit, the file saves as another template with the reference number included.
I am not sure if the issue is Word or PowerPoint-related. The code for the Word template is
Sub AutoNew()
REF = System.PrivateProfileString("L:\Local\Lab\Section - Support Services\Health and Safety\H&S Suggestions\Settings.Txt", _
"MacroSettings", "REF")
If REF = "" Then
REF = 1
Else
REF = REF + 1
End If
System.PrivateProfileString("L:\Local\Lab\Section - Support Services\Health and Safety\H&S Suggestions\Settings.Txt", "MacroSettings", _
"REF") = REF
ActiveDocument.Bookmarks("REF").Range.InsertBefore Format(REF, "000#")
End Sub
Private Sub CommandButton1_Click()
REF = System.PrivateProfileString("L:\Local\Lab\Section - Support Services\Health and Safety\H&S Suggestions\Settings.Txt", _
"MacroSettings", "REF")
ActiveDocument.SaveAs FileName:="L:\Local\Lab\Section - Support Services\Health and Safety\H&S Suggestions\Suggestion " & Format(REF, "000#.doc")
Application.Quit
End Sub
Any help or pointers would be appreciated as if it works I'd like to use for various other templates.
From the description, it's kind of hard to get an accurate idea of what's happening, but it SOUNDS like the the AUTONEW just might not get run in that particular combination.
You could verify this by using some logging or MSGBOX calls to see exactly what macros are being run, when.
Check the docs on Autonew here
http://support.microsoft.com/kb/211659
Sounds like it won't run if the macro is saved in Normal, which doesn't sound like the case here but it's worth noting.
You might also consider using the AutoOpen macro and checking other elements to make sure this is a brand new doc instead of one that's already been saved (like checking the content of the Document.Fullname property).
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