We are developing Outlook 2007 add-in. For testing outlook category renaming I've added the following code block
var session = Application.Session;
var categories = session.Categories;
var category1 = session.Categories[1];
//catefory1.Name is "Group1" before executing line below
category1.Name = "TEST!!!";
Marshal.ReleaseComObject(category1);
Marshal.ReleaseComObject(categories);
Marshal.ReleaseComObject(session);
to the end of add-in private void ThisAddIn_Startup(object sender, EventArgs e) method.
Category is renamed but if Outlook is closed, the above lines are commented, and outlook is started again - the category name is not "TEST!!!" as I expected. It is "Group1" as is was before renaming. Is it possible to rename outlook category "forever" by code? Microsoft.Office.Interop.Outlook.Category has no Save() or Update() or Persist() methods.
P.S. We are developing Outlook 2007 add-in using Visual Studio 2008, .net 3.5, C# 3.
The problem is reproduced with Outlook 2007 SP1 and SP2. Other outlook versions were not tested.
I have solved the problem (the problem itself seems to be Outlook 2007 bug) using a hack.
The following links helped me to create the hack (oops, not enough reputation to post more then 1 link):
http ://blogs.officezealot.com/legault/archive/2009/08/13/21577.aspx
http://www.officekb.com/Uwe/Forum.aspx/outlook-prog-addins/3142/Apply-Categories-to-other-users-Outlook-2007
http ://help.wugnet.com/office/set-master-category-list-Outlook-2007-ftopict1095935.html
http ://forums.slipstick.com/showthread.php?t=18189
http ://msdn.microsoft.com/en-us/library/ee203806%28EXCHG.80%29.aspx
The hack itself is show below:
using System;
using System.Text;
using System.Xml;
using System.IO;
using Microsoft.Office.Interop.Outlook;
namespace OutlookHack
{
public static class OutlookCategoryHelper
{
private const string CategoryListStorageItemIdentifier = "IPM.Configuration.CategoryList";
private const string CategoryListPropertySchemaName = #"http://schemas.microsoft.com/mapi/proptag/0x7C080102";
private const string CategoriesXmlElementNamespace = "CategoryList.xsd";
private const string XmlNamespaceAttribute = "xmlns";
private const string CategoryElement = "category";
private const string NameAttribute = "name";
public static void RenameCategory(string oldName, string newName, Application outlookApplication)
{
MAPIFolder calendarFolder = outlookApplication.Session.GetDefaultFolder(
OlDefaultFolders.olFolderCalendar);
StorageItem categoryListStorageItem = calendarFolder.GetStorage(
CategoryListStorageItemIdentifier, OlStorageIdentifierType.olIdentifyByMessageClass);
if (categoryListStorageItem != null)
{
PropertyAccessor categoryListPropertyAccessor = categoryListStorageItem.PropertyAccessor;
string schemaName = CategoryListPropertySchemaName;
try
{
// next statement raises Out of Memory error if property is too big
var xmlBytes = (byte[])categoryListPropertyAccessor.GetProperty(schemaName);
// the byte array has to be translated into a string and then the XML has to be parsed
var xmlReader = XmlReader.Create(new StringReader(Encoding.UTF8.GetString(xmlBytes)));
// xmlWriter will write new category list xml with renamed category
XmlWriterSettings settings = new XmlWriterSettings { Indent = true, IndentChars = ("\t") };
var stringWriter = new StringWriter();
var xmlWriter = XmlWriter.Create(stringWriter, settings);
xmlReader.Read(); // read xml declaration
xmlWriter.WriteNode(xmlReader, true);
xmlReader.Read(); // read categories
xmlWriter.WriteStartElement(xmlReader.Name, CategoriesXmlElementNamespace);
while (xmlReader.MoveToNextAttribute())
{
if (xmlReader.Name != XmlNamespaceAttribute) // skip namespace attr
{
xmlWriter.WriteAttributeString(xmlReader.Name, xmlReader.Value);
}
}
while (xmlReader.Read())
{
switch (xmlReader.NodeType)
{
case XmlNodeType.Element: // read category
xmlWriter.WriteStartElement(CategoryElement);
while (xmlReader.MoveToNextAttribute())
{
if ((xmlReader.Name == NameAttribute) && (xmlReader.Value == oldName))
{
xmlWriter.WriteAttributeString(NameAttribute, newName);
}
else
{
xmlWriter.WriteAttributeString(xmlReader.Name, xmlReader.Value);
}
}
xmlWriter.WriteEndElement();
break;
case XmlNodeType.EndElement: // categories ended
xmlWriter.WriteEndElement();
break;
}
}
xmlReader.Close();
xmlWriter.Close();
xmlBytes = Encoding.UTF8.GetBytes(stringWriter.ToString());
categoryListPropertyAccessor.SetProperty(schemaName, xmlBytes);
categoryListStorageItem.Save();
}
catch (OutOfMemoryException)
{
// if error is "out of memory error" then the XML blob was too big
}
}
}
}
}
This helper method must be called prior to category renaming, e.g.:
var session = Application.Session;
var categories = session.Categories;
var category1 = session.Categories[1];
//catefory1.Name is "Group1" before executing line below
OutlookCategoryHelper.RenameCategory(category1.Name, "TEST!!!", Application);
category1.Name = "TEST!!!";
Marshal.ReleaseComObject(category1);
Marshal.ReleaseComObject(categories);
Marshal.ReleaseComObject(session);
This is an Outlook bug introduces with Outlook 2007 SP2.
"Consider the following scenario. You have a custom application that can be run to create new categories in Outlook 2007.
You run the application to create a new category in Outlook 2007. Then, if you restart Outlook 2007, the category that you created is removed unexpectedly. This problem occurs after you install the Februarycumulative update or SP2."
There is a hotfix available since June 30, 2009:
http://support.microsoft.com/default.aspx/kb/970944/en
Regards,
Tim
Related
I'm working on a solution to localize legacy applications. I've written a Visual studio add-in using EnvDte that automates the process of setting the "Localizable" flag for every form in the solution to true, which is a critical step for extracting resources on form designers. I am now trying to deal with any text that is set programmatically, text that trigger the Globalization (CA13##) warnings.
designer.Visible = true;
var host = (IDesignerHost)designer.Object;
var provider = TypeDescriptor.GetProvider(host.RootComponent);
var typeDescriptor = provider.GetExtendedTypeDescriptor(host.RootComponent);
if (typeDescriptor == null)
continue;
var propCollection = typeDescriptor.GetProperties();
var propDesc = propCollection["Localizable"];
if (propDesc != null && host.RootComponent != null &&
(bool?)propDesc.GetValue(host.RootComponent) != true)
{
try
{
propDesc.SetValue(host.RootComponent, true);
}
catch (Exception ex)
{
// log the error
}
// save changes
}
I've been able to run it manually from the menu using: Analyze -> Run Code Analysis -> On Solution to get a list of issues, but I would like to automate this step with another add-in that runs and extracts the results.
Are there any resources that point to accessing the build warnings or the results of the code analysis?
Are there any solutions that already do this using EnvDte or Roslyn?
Ok, I've managed to glean enough information to put together the add-in. Put simply, you use _dte.ExecuteCommand()
Initialize the command:
// nothing to see here...
_package = package ?? throw new ArgumentNullException(nameof(package));
var commandService = ServiceProvider.GetService(typeof(IMenuCommandService)) as OleMenuCommandService;
if (commandService == null)
return;
_dte = (DTE2)ServiceProvider.GetService(typeof(DTE));
var menuCommandId = new CommandID(CommandSet, CommandId);
var menuItem = new MenuCommand(MenuItemCallback, menuCommandId);
commandService.AddCommand(menuItem);
_events = _dte.Events.BuildEvents;
// since static code analysis is a sort of build you need to hook into OnBuildDone
_events.OnBuildDone += OnBuildDone;
Trigger the analysis
private void MenuItemCallback(object sender, EventArgs e)
{
_dte.ExecuteCommand("Build.RunCodeAnalysisonSolution");
}
Extract errors in the OnBuildDone event
private void OnBuildDone(vsBuildScope scope, vsBuildAction action)
{
Dispatcher.CurrentDispatcher.InvokeAsync(new Action(() =>
{
_dte.ExecuteCommand("View.ErrorList", " ");
var errors = _dte.ToolWindows.ErrorList.ErrorItems;
for (int i = 1; i <= errors.Count; i++)
{
ErrorItem error = errors.Item(i);
var code = error.Collection.Item(1);
var item = new
{
error.Column,
error.Description,
error.ErrorLevel,
error.FileName,
error.Line,
error.Project
};
error.Navigate(); // you can navigate to the error if you wanted to.
}
});
}
I'm developing my first Visual Studio (2015 Community) Command Menu and I'm trying to get access to IEditorOperations to delete text, send backspace etc. but I'm not sure how to. I can do:
var Service = Provider.GetService(typeof(IEditorOperationsFactoryService)) as IEditorOperationsFactoryService;
Service.GetEditorOperations(???);
I'm not sure what to pass in the ??? since I don't have access to an ITextView instead what I have is a IVsTExtView via:
IVsTextView View;
IVsTextManager Manager = (IVsTextManager)ServiceProvider.GetService(typeof(SVsTextManager));
int MustHaveFocus = 1;
Manager.GetActiveView(MustHaveFocus, null, out View);
When creating the Command Menu, VS generates a template for me with a private ctor creating the command service, binding it to the command set id etc. An overridden Initialize method, and a bunch of properties.
Any ideas?
EDIT: After help from Sergey, I managed to get a bit further. But now I get a null when I try to get the IEditorOperationsFactoryService, all the other values are valid.
static IEditorOperations GetEditorService(IServiceProvider Provider, IVsTextView VsView)
{
IEditorOperations Result;
try
{
var Model = (IComponentModel)Provider.GetService(typeof(SComponentModel));
var Editor = (IEditorOperationsFactoryService)Provider.GetService(typeof(IEditorOperationsFactoryService)); // returns null
var Adaptor = Model.GetService<IVsEditorAdaptersFactoryService>();
IWpfTextView TextView = Adaptor.GetWpfTextView(VsView);
Result = Editor.GetEditorOperations(TextView);
}
catch (Exception e)
{
System.Windows.Forms.MessageBox.Show(e.ToString());
Result = null;
}
return (Result);
}
You can get IEditorOperationsFactoryService instance from variable named Model, like this:
var Model = (IComponentModel)this.ServiceProvider.GetService(typeof(SComponentModel));
var Editor = (IEditorOperationsFactoryService)Model.GetService<IEditorOperationsFactoryService>();
You can get IWpfTextView (that implements ITextView) from IVsTextView using:
IVsTextView textView = ...;
IWpfTextView v = GetEditorAdaptersFactoryService().GetWpfTextView(textView);
private Microsoft.VisualStudio.Editor.IVsEditorAdaptersFactoryService GetEditorAdaptersFactoryService()
{
Microsoft.VisualStudio.ComponentModelHost.IComponentModel componentModel =
(Microsoft.VisualStudio.ComponentModelHost.IComponentModel)serviceProvider.GetService(
typeof(Microsoft.VisualStudio.ComponentModelHost.SComponentModel));
return componentModel.GetService<Microsoft.VisualStudio.Editor.IVsEditorAdaptersFactoryService>();
}
Is there a Visual Studio 2010 plugin for the new "preview tab" feature in Visual Studio 2012?
I've tried to do it by myself, but I have no expierience in doing VS extensions nor using EnvDTE API.
I've followed Building and publishing an extension for Visual Studio 2010 to create a new Visual Studio 2010 extension.
Then I added a Tools menu item with the VSPackage Builder designer, and used this code to try to imitate the behaviour.
I am not able to:
Determine whenever a file is selected, so I have to do a loop.
Open a file in already existing window.
Change the window to be shown
at the right.
I leave the code here, just in case someone else is interested on creating an extension. Hope (s)he has a better knowledge of VS Extensibility.
[Guid(GuidList.guidPreviewDocumentTabPkgString)]
public class PreviewDocumentTabPackage : PreviewDocumentTabPackageBase
{
private DTE dte;
private Document currentTab;
protected override void Initialize()
{
base.Initialize();
this.dte = this.GetService(typeof(_DTE)) as DTE;
if (this.dte == null)
{
throw new ArgumentNullException("dte");
}
var applicationObject = (DTE2)GetGlobalService(typeof(SDTE));
var solutionExplorer = applicationObject.ToolWindows.SolutionExplorer;
System.Threading.Tasks.Task.Factory.StartNew(() =>
{
object currentItem = null;
while (true) // To be improved
{
// Get selected items
var items = solutionExplorer.SelectedItems as Array;
// Only do logic if there is one file selected, no preview for multiple files.
if (items != null &&
items.Length == 1)
{
var item = items.GetValue(0);
if (currentItem == null)
{
currentItem = item;
}
else
{
// Only show preview if the file is "new".
if (item != currentItem)
{
currentItem = item;
// Determine if is a c# file.
var realItem = (UIHierarchyItem)currentItem;
var itemName = realItem.Name;
if (itemName.EndsWith(".cs"))
{
// Get the file
var projectItem = (ProjectItem)realItem.Object;
var projectItemPath = projectItem.Properties.Item("FullPath")
.Value.ToString();
// No already opened file.
if (currentTab == null)
{
// Open the file and get the window.
this.currentTab = this.dte.Documents.Open(projectItemPath);
}
else
{
// Todo: Open the file in the this.currentTab window.
}
}
}
}
}
// Avoid flooding
System.Threading.Thread.Sleep(100);
}
});
}
}
I am trying to set styling in PDF generated from iTextSharp.
It seems the SetListSymbol is not working for me whenever I am trying to set Symbol to list items.
Below is the code that I used:
var elements = HTMLWorker.ParseToList(overviewReader,null);
foreach (var element in elements)
{
//element
var list = element as List;
if (list != null)
{
//list.Symbol.GetImage();
list.SetListSymbol("\u25A0");
list.IndentationLeft = 20f;
doc.Add(list);
}
else
{
doc.Add(element);
}
}
The HTMLWorker within iText and iTextSharp supports some very limited "stylesheets" via iTextSharp.text.html.simpleparser.StyleSheet. These stylesheets are loosely based on HTML/CSS properties but only the most basic (think HTML 3.2).
The three main things that you want to do are (1) load a font, (2) create a StyleSheet pointing to that font and (3) bind the StyleSheet to the HTMLWorker. I'm going to partially lift some code from my answer here.
iTextSharp doesn't automatically spider the entire system looking for fonts so you need to manually register them. (Actually, there is a method that you can call and tell iTextSharp to guess at loading fonts but this is much faster.)
Step #1, load the font, in this case Curlz
//Path to our font
string OurFont = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Fonts), "CURLZ___.TTF");
//Register the font with iTextSharp
iTextSharp.text.FontFactory.Register(OurFont);
Step #2, create a StyleSheet and point it to our font. I'll also set some other properties just to show them off.
//Create a new stylesheet
iTextSharp.text.html.simpleparser.StyleSheet ST = new iTextSharp.text.html.simpleparser.StyleSheet();
//Set the default body font to our registered font's internal name
ST.LoadTagStyle(iTextSharp.text.html.HtmlTags.LI, iTextSharp.text.html.HtmlTags.FACE, "Curlz MT");
ST.LoadTagStyle(iTextSharp.text.html.HtmlTags.LI, iTextSharp.text.html.HtmlTags.COLOR, "FF0000");
ST.LoadTagStyle(iTextSharp.text.html.HtmlTags.LI, iTextSharp.text.html.HtmlTags.SIZE, "50");
Step #3, bind the StyleSheet to our HTMLWorker
//Use our StyleSheet from above when parsing
var elements = HTMLWorker.ParseToList(overviewReader, ST);
Below is a full-working C# WinForms app targeting iTextSharp 5.2.0 that shows off all of the above.
using System;
using System.IO;
using System.Windows.Forms;
using iTextSharp.text;
using iTextSharp.text.html.simpleparser;
using iTextSharp.text.pdf;
namespace WindowsFormsApplication2 {
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e) {
//Path to our font
string FontArial = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Fonts), "CURLZ___.TTF");
//Register the font with iTextSharp
iTextSharp.text.FontFactory.Register(FontArial);
//Create a new stylesheet
iTextSharp.text.html.simpleparser.StyleSheet ST = new iTextSharp.text.html.simpleparser.StyleSheet();
//Set the default body font to our registered font's internal name
ST.LoadTagStyle(iTextSharp.text.html.HtmlTags.LI, iTextSharp.text.html.HtmlTags.FACE, "Curlz MT");
ST.LoadTagStyle(iTextSharp.text.html.HtmlTags.LI, iTextSharp.text.html.HtmlTags.COLOR, "FF0000");
ST.LoadTagStyle(iTextSharp.text.html.HtmlTags.LI, iTextSharp.text.html.HtmlTags.SIZE, "50");
//Sample HTML
var html = #"<ul><li>Test</li></ul>";
//File to output
var outputFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "Test.pdf");
//Basic PDF creation, nothing special here
using (FileStream fs = new FileStream(outputFile, FileMode.Create, FileAccess.Write, FileShare.None)) {
using (Document doc = new Document(PageSize.LETTER)) {
using (PdfWriter writer = PdfWriter.GetInstance(doc, fs)) {
doc.Open();
//Bind a reader to our HTML
using (StringReader overviewReader = new StringReader(html)) {
//Use our StyleSheet from above when parsing
var elements = HTMLWorker.ParseToList(overviewReader, ST);
//Loop through each element
foreach (var element in elements) {
//See if the element is a list item
var list = element as List;
if (list != null) {
//Set some properties
list.SetListSymbol("\u25A0");
list.IndentationLeft = 20f;
}
//Add the element to the document
doc.Add(element);
}
}
doc.Close();
}
}
}
this.Close();
}
}
}
List objects = HTMLWorker.ParseToList(new StringReader(format), null);
foreach (IElement element in objects)
{
List list = new List();
list.SetListSymbol("\u2022");
list.IndentationLeft = 20f;
list.Add(element);
if (list.Chunks.Count == 0)
{
doc1.Add(element);
}
else
{
doc1.Add(list);
}
}
Knowing the hwnd of the window, how do I read the contents of this? Before anyone ask me, I'm trying to get the text that was used in the Communicator window.
Below is the code I found on the Internet.
The code is not mine.
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
namespace EventFun
{
class EventHookUp
{
CommunicatorAPI.Messenger mCommunicator = null;
static void Main(string[] args)
{
EventHookUp hu = new EventHookUp();
hu.InitializeEventHocks();
Console.ReadKey();
}
public void InitializeEventHocks()
{
mCommunicator = new CommunicatorAPI.Messenger();
mCommunicator.OnIMWindowCreated += new CommunicatorAPI.DMessengerEvents_OnIMWindowCreatedEventHandler(mCommunicator_OnIMWindowCreated);
mCommunicator.OnIMWindowDestroyed += new CommunicatorAPI.DMessengerEvents_OnIMWindowDestroyedEventHandler(mCommunicator_OnIMWindowDestroyed);
}
void mCommunicator_OnIMWindowCreated(object pIMWindow)
{
CommunicatorAPI.IMessengerConversationWndAdvanced stpIMWindow = pIMWindow as CommunicatorAPI.IMessengerConversationWndAdvanced;
//stpIMWindow.History;
long Hwnd = (long)stpIMWindow.HWND;
Console.WriteLine("New IM Window Created : {0}", Hwnd);
CommunicatorAPI.IMessengerContacts contactList = (CommunicatorAPI.IMessengerContacts)stpIMWindow.Contacts;
StringBuilder sb = new StringBuilder();
foreach (CommunicatorAPI.IMessengerContact imc in contactList)
{
sb.Append(imc.FriendlyName);
sb.Append(Environment.NewLine);
}
Console.WriteLine(sb.ToString());
}
void mCommunicator_OnIMWindowDestroyed(object pIMWindow)
{
Console.WriteLine("IM Window Destroyed.");
}
}
}
It sounds like you are trying to get the conversation text history from the conversation window? If so, George Durzi has an excellent blog post on this.
As this blog post is not available, I used below method to retrieve the conversation history:
object obj = msgrAdv.StartConversation(
CONVERSATION_TYPE.CONVERSATION_TYPE_IM, // Type of conversation
sipUris, // object array of signin names for having multiple conversations or just a string
null,
"Test",
"1",
null);
imWindowHandle = long.Parse(obj.ToString());
if (imWindow == null) //If there is already an open window...
{
imWindow = (IMessengerConversationWndAdvanced)msgrAdv.InstantMessage(sipUris);
}
//else there was no open window, we have opened the window using "msgrAdv.StartConversation" so there is a imWindow associated which is implemented in communicator_OnIMWindowCreated.
//and then...
string history = imWindow.History;