C# Outlook Addin - Open msg Files - c#

Ok i try to explain the Problem.
I've wrote an Outlook Extension to match Mails, Task and so on to an ERP System.
It works just fine but i have 1 Problem:
I save all Outlook Elements whitch could be mapped to ERP Customers, Suplliers and so on in the Document Database of the ERP System. So Every Project/Team member could open the latest Version of the Outlook Item.
The first time a user Opens the Item every Thing is fine, the second time they couldn't open the File because it is already open in Outlook.
see social.msdn.microsoft.com
I know that Outlook "saves" a temporay item for every msg File but i could not release the object.
And i don't want to Quit Outlook i just want to release the msg File after closing it so the User can reopen it WITHOUT CLOSING Outlook.
But i haven't found a way to release it without calling Quit.
Or is there an Option to redisplay the the currently opend Item? At the Moment i have no idea how to reference those already opened msg files.
Building an Dictionary of those Item is quite heavy i think.
#SuperBiasedMan - Sure, i tried 2 ways so far:
// 1. Just open File in corresponding Viewer
System.Diagnostics.Process.Start([PathToMsgFile]);
// 2. Using SharedItem Method to get an item Object
Microsoft.Office.Interop.Outlook.Application app = new Microsoft.Office.Interop.Outlook.Application();
var item = app.Session.OpenSharedItem([PathToMsgFile]) as Microsoft.Office.Interop.Outlook.MailItem;
item.Display();
With both the Item is Displayed as expected, when the User Closes this File and try to reopen it (without restarting Outlook)the user get an error Message:
"Cannot open file: [PathToMsgFile] .msg. The file may not exist, you may not have permission to open it, or it may be open in another program. Right-click the folder that contains the file, and then click Properties to check your permissions for the folder."
Outlook keeps the Item open.
The only way i found to release it is to call:
app.Quit();
The Discussions (see Link above) conclusion is that Outlook "saves" an temporary Outlook Element when opening Msg files. I search an possibility to close/delete/release or whatever this temporary Object or to reopen it without restarting Outlook
#Dmitry: The file comes from a remote machine

Related

"Best" way to get an Outlook MailItem from .msg file

To pass from a .msg file to its related Outlook MailItem I found and tried these two ways:
Outlook.Application oApp; // --> Outlook Application
Outlook.MailItem oItem; // --> Outlook MailItem
string file= #"C:\PWS\myMail.msg";
oApp= (Outlook.Application)new Outlook.Application();
// way #1
oItem= (Outlook.MailItem)oApp.CreateItemFromTemplate(file);
// or way #2
oItem= (Outlook.MailItem)oApp.Session.OpenSharedItem(file);
What is the difference between these two ways? I need to open the .msg and then use the resulting MailItem (to get some properties as 'SenderEmailAddress' or the email attachments)... what should I use? At the moment they are the same to me...
The third way is to use run .msg file programmatically. A default application (outlook) should be opened in that case. For example:
string file= #"C:\PWS\myMail.msg";
Process.Run(file);
Be aware, you can't run multiple instance of Outlook. So, the message will be opened in the existing Outlook instance (if any).
Both methods (#1 and #2) allow to open the saved message in Outlook. But they have minor differences:
The CreateItemFromTemplate method of the Application class creates a new Microsoft Outlook item from an Outlook template (.oft) and returns the new item. I'd also like to draw your attention to the fact that new items will always open in compose mode, as opposed to read mode, regardless of the mode in which the items were saved to disk.
The OpenSharedItem method of the Namespace class opens a shared item from a specified path or URL. See How to: Import Saved Items using OpenSharedItem for more information.
It is up to you which way to choose based on the information listed above...

C# DSO Framer Control Exception has been thrown by the target of an invocation

Thank you for taking the time to read my question.
I am developing a small application to do the following:
Read each .xlsm Report in a specific directory
Grab specific field based on different Report types
Store the fields in a custom class object
Store the custom class object in a List<>
After reading all the .xlsm Reports, create a new Tally Report from a template
Output the data from each custom class object in the List<> to the new Tally Report
This is working great. But I added a new feature so that if an exception error occurred, I could prompt the user using a custom message box to either Edit or Remove the document or Ignore the exception and continue getting the remaining data.
When the user selects "Edit", it calls a class file that dynamically generates a new Form, add a DSO Framer Control to the new Form, and then opens a specific file from the parameter passed in.
The first time I receive my message box and select "Edit", it will open the new form correctly and display the .xlsm report for me to edit. Once I click Close, it saves the changes and continues on processing. If I receive my message box a second time and select "Edit" (whether for the same .xlsm file or not) I received an Exception:
"Exception has been thrown by the target of an invocation."
I thought it may have been because the document had already been open using "Microsoft.Office.Interop.Excel", but the first .xlsm file will open even though it had been opened using "Microsoft.Office.Interop.Excel". I did notice that When using "Microsoft.Office.Interop.Excel" I still had an Excel process running in Task Manager after calling the Excel.Application.Quit() function and using Marshal.ReleaseComObject on the ExcelApp object.
If I complete close the application and reopen it, I can "Edit" again the first document that prompts my message box.
All I need is to be able to do when I select the "Edit" option is to pause the running code, display the file for the user to edit, and once they're done editing continue running the code again.
Please advise.
I just tried posting the code in here too and it said I used too many lines. Whats the best way to share all of my code?

Crystal report document load hangs

Whenever the program reaches the below code, the program hangs
protected void InitCrystalReport(String _reportUrl)
{
myReportDocument.Load(_reportUrl);
}
This situation only happens when I put the web on IIS (another server), but it doesn't exist when I run the application on Visual studio (Debug mode). I've also used process monitor to monitor the process to see if the file is access denied.
I've tried below on Web Server but none of them works:
Changing application pool to .NET Classic
Restart the printer spooler
Edit
I restarted the server and everything's OK now
I had this problem when i moved a new report to the test server. My fix was to set a specific printer through File.. Print.. to one i knew was on the server (in this case MS XPS Document Writer). Ensure you've used .dispose etc on the object when you're done.
Although this is old question already answered, I also encountered same problem and after wasting one whole day, I got suggestion from #PhilKemreen answer. Here is how I fixed it in my case:
Open report in Crystal Report Designer and Right Click
Select Design > Page Setup..., Page Setup Dialog box appears.
From the printer drop-down list, select Microsoft XPS Document Writer.
Click OK.
Then I deployed the new report file on server, it works.

Normal.dotm being used by a different process?

I have an application that:
Copies a file
Pastes the copy with a new name
Modifies it
Saves it
This has been working fine, but then today, I've been getting this error:
Normal was being edited by another Word session. If you save this
document with the original name, you will overwrite any changes made
in the other session. Do you want to save the document using the
original name anyway?
And this is the file location of "Normal.dotm". I've never seen this file and am not manually accessing this file in any of my code.
C:\Documents and Settings\MyUserName\Application
Data\Microsoft\Templates\Normal.dotm
I'm running old code that has worked fine in the past, and I reset my computer, so I don't think any of my processes are screwing this up. What could be causing this? It seems like something happened externally to cause this problem, but I could be wrong. I'm genuinely stumped.
You might get this save message when you edit the styles or margin settings of the document.
Refer this
http://office.microsoft.com/en-us/word-help/change-the-normal-template-normal-dotm-HA010030756.aspx
I suppress this message in my application because we set some settings for Word when the document opens up
foreach (Word.Template template in Globals.ThisAddIn.Application.Templates)
{
switch (template.Name.ToLower())
{
case "normal.dotm":
template.Saved = true;
break;
}
}
I have noticed that if you have outlook opened it locks the normal.dotm file so check if your outlook is editing your normal.dotm file

Is there a way in c# to know when the user has finished editing a file with an application?

This is what I'm trying to do :
Download a file (txt, doc, xls, whatever) from a server
Open the file with the appropriate application using System.Diagnostics.Process.Start(path to file)
Monitor for file changes using a FileSystemWatcher.
Each time the file is changed, upload the file back to the server
Continue monitoring until the user has finished editing the file
Delete the local copy of the file
Exit the application
I'm stuck at step 5. How can I know whether a user has finished working on a file ?
I cannot rely on the file being locked (notepad doesn't lock txt files for example).
I cannot rely on a process having exited or not (an example is Notepad++ for txt files : the file could be open in a tab. When you close the tab, you've finished editing the file, but the process is still running)
Any idea/points on how to do that in C# ?
You've excluded the two ways you could go about detecting the file being in use: file locking, and the process you start exiting.
The only alternative I an think of is to display a dialog to ask the user when they've finished editing.
Edit: For what it's worth - FileZilla has this type of behaviour. You can choose to edit a file on the remote server, it downloads the file, launches the default editor, and (in the background) shows a "If you've finished editing - Click OK" button.
This gives me the opportunity to cancel an edit, if I've mucked up the file and saved it.
This is really hard to do - we've tried various things but never found anything that was foolproof. If you know the program you have launched then, in theory, you can find the file handles it uses and see when it stops using the one you're interested in.....but if you rely on Windows to resolve the default application to launch even this becomes tricky.
We copy editable files into a temp folder named with the date and rely on users uploading them back when they have finished their edit session. We then clean up previous days folders on application startup.
You could check the date of last change of the file. This date gets set when you save changes to the file. Mind though that this field is not very reliable since one can set it to any value (with appropriate tools).

Categories