I'm working on an Excel workbook that scans an Outlook folder, pulls the email data and saves the attachment. A later bit of code then reads the data in each attachment and prints it to a master sheet.
I really want to remove the need to save an attachment and just temporarily open it, take what I need from it and then close it.
I've done a fair bit of research and can't find anything that doesn't involve saving it. It's also worth calling out Outlook VBA is out of the question due to security restrictions so it needs to be done from Excel.
Is it possible to do what I want, and do you have any examples/references I can check out?
Thanks in advance
No. To display/open/read the data you need to save it. Even when you preview it inside Outlook, the file is saved at temp location.
You can always save the file (preferably in User's Temp folder) and after processing the file you can delete it from VBA.
To Delete the a file from VBA , use Kill Command. Make sure the file is closed and then
Kill FullFileName
Example: Kill "C:\Temp\abc.xlsx"
Related
I'm using Word 2019 and have created macros I want to send to other organisations.
However, when I transfer the document (.docm) or the template (.dotm) the recipient doesn't get the macros (although everything else goes across). On the Developer ribbon, there is no entry for the macro itself or the VBA code.
I know I can export the macro to a *.bas file which the recipient can then import but I'd like to do it directly.
There is no problem with transferring Excel macros but I can't get Word to work.
Can anybody help?
This is most likely to have 2 possible reasons:
Did you really choose "Word Macro-Enabled Document" as file format? It could be that you saved a normal "Word Document" with the file extension .docm. Note that the file extension does not defined the file format.
If this happend you have actually a Word document (without macros) but with the wrong file extension .docm. Make sure you choose the correct format not only the correct extension.
You can easily test this:
Save your file as macro enabled file docm.
Close Word completely.
Re-Open your file. If the macro is still there your file format is correct.
If you send your file via email it can be that your recepient does not allow to recieve macro enabled files, and his email server is stripping off the macro from the Word file.
If this is the case, then you cannot do much to prevent this. It is in the responsibility of the recepient. The only way to get through this kind of filter is to pack your file into a password protected zip archive so the email server cannot access the Word file. But not that doing so can violate the recepients policies. Also if the reciepient's company did it right they won't allow password protected zip archives via email because the could tunnel through their email filters.
Best solution in this case it to provide the file via some cloud share (Google Drive, Dropbox, One Drive, what ever, …) and send the recipient a link only.
That is because of your AV blocking macros to be saved in your PC. Macros have been used in the past by threat actors and Microsoft has applied a protection to them.
When you edit a document with macros, word creates a hidden file which is the macro you are running, now when you try to save that file, it gets caught by AV. Since AV thinks it is an evil activity. It gets saved on your system because your directory is now a Trusted Location and it is okay.
A workaround is to install Windows 7 with Microsoft Office 2007 to get what you want since Windows 7 has little to no security.
Thanks
So I have a robot which is accessing an Excel file an writing some information to it from a collection.
The thing is it's been working fine so far but the other day there was an Outage and the internet connection was failing.
So bot kept running and adding the data to the excel. But the save was failing since the file is stored on a shared hard disk, so you need internet to access it.
Bot thought it has saved but it wasn't. Can I add VBA code to the original BluePrism 'Close Workbook' to include an output as check Success? to guarantee I know it has indeed saved the file.
Can I add VBA code to the original BluePrism 'Close Workbook' to include an output as check Success? to guarantee I know it has indeed saved the file.
You can, but the ideal approach is to try and minimize the amount of edits required to the stock VBOs as much as possible to avoid accruing technical debt. In fact, the check you're describing is possible with just the stock actions provided in the MS Excel VBO.
Instead of using the "Close Workbook" action's "Save Data" parameter to save the workbook, insert an action stage calling the MS Excel VBO's "Save Workbook As" action. You can pass the same path you've provided to the "File path" input of the "Open Workbook" action to overwrite (performing the same function as the "Save Workbook" action).
The difference is that if there's an issue saving to the specified path, the action itself will throw an Internal exception, which you can Recover and Resume from as your business logic dictates:
In an Outlook Add-In, objects seem to be kept in memory. As a result when the Add-In is active in Outlook, .eml files can only be opened once. When trying a second time, I get:
We can't open [filename]. It's possible the file is already open, or you don't have permission to open it ...
When the Add-In is disabled, .eml files can be opened multiple times without any issue.
In Add-In code, I placed a Marshal.FinalReleaseComObject(obj) at multiple places but it did not help. The code is fairly long and spread of over multiple files. It might be that I missed a release.
Is there any way to easily troubleshoot the situation and identify the root cause.
Try to comment out most of your code and bring it back a few lines at a time until the problem comes back.
As a sledgehammer solution, try to call GC.Collect() after you are done.
I understand that an "on error" code can be used if an error occurs. However, I wonder if it is possible to void all that had been processed by that macro? Perhaps recalling the last memory?
For example, I have a code that runs to transfer many different tasks. Is there a code that can be used to undo what it has done other than manually going to those sheets or files and delete them manually?
Thank you in advance.
Save a copy of the workbook to a backup as it starts the code, on error rename the current workbook to something else, restore the backup over the original then close the renamed one you are in.
Open the file again and it will be exactly as it was before you ran the macro.
NOTE, THIS WILL BE SAVING YOUR FILE WHEN IT RUNS, Make sure you have a backup somewhere else when doing your testing.
I'm creating a program in VB.net that does the following:
At a high level I receive a file in email, put the attachment in a monitored folder, import the text file to excel, format the excel, and then email the excel file to a list of recipients.
Here is my plan:
Completed: Outlook VBA to monitor all incoming email for specific message. Once message is received drop attached .txt file in a specific network folder.
Completed: (VB.net) Monitor folder, when text file is added begin processing
Not Complete: (VB.net) Import text file to Excel
Not Complete: (VB.net) Format Excel Text file. (add in a row of data, format column headers with color/size, add some blank columns, add data validation to some of the blank columns that allow drop down selections)
Completed: (VB.net) Save file.
Completed: (VB.net) Send file to list of recipients.
Obviously the items above that are not complete are the bulk of the work, but I wanted to get some advice on what some of you think would be the best way to approach something like this. The import and formatting of the file are causing me some problems because I just can't decide what would be the most efficient way to do this.
What I've thought of so far:
The way stated above. Import to excel -> format
Having a template excel that contains all of the formatting already done for me and attempting to transition the data to this document (no clue if/how I can do this). Is it even feasible? Have the template already created and then import the text file to a new excel file, then transition that data to the excel template?
Something I thought about, in terms of formatting the document, was to record a macro of me doing all of the formatting that I'm going to need and then attempt to convert that macro into my vb.net code, but I'm not sure if that will work. I will need to verify that the text file comes in the EXACT format every time correct?
I really appreciate any advice/suggestions that anyone is willing to give.
You will want to use http://epplus.codeplex.com/
It allows you to create an Excel file from scratch, without having to start Excel itself. Automating Excel will make the process slow and it lacks robustness (Excel process can hang or not close properly).
In addition, using a .Net library allows you to run it on a server or so where no Excel is installed. (Next step would be to inspect the mailbox via POP, IMAP or the Exchange API, so that part doesn't have to be run on a client machine either)
http://msdn.microsoft.com/en-us/library/kh3965hw(v=vs.100).aspx
You can also just use the Interops from MS to interact with Excel, Outlook, Word, etc. They're not difficult at all to use. I'm not familiar with CodePlex, so that may be a better route or an easier one. I just wanted to provide you with an alternative.
With Microsoft Office 2010 Interops you can not generate Office files from .net applications anymore.
You can manipulate data from existing Excel files so you need templates(your 4th point). Then Excel allows you to query some databases. You may be able to simulate one with your folder, otherwise I suggest to convert your .txt files into some databases. (3rd point)
If you do use an older version, you can crate your Excel files by loading them into an instance of Excel and manipulating them as you wish.
By the way I supposed your attached files would have some sort of format.
If you want to manipulate Excel files, I can recommand you the NPOI library found on CodePlex. It has several advantages over OLE-automation:
NPOI is not dependent on a specific Excel version.
Excel (or any other Office component) need not to be installed.
It is faster.
It works with both .XLS and .XLSX files.
We are using a third party software called excel writer. May not be what you are looking for becauseit needs to be license, but it is very fast and the clients does not have to wait for a chart or a data output. Because we have that tool, have not try anything else.