Error Handling with Excel VBA controlling Word and PDF - excel

TL DR version: what errors should I specifically look for in a error handler with an excel vba running a word template and saving as word doco and pdf.
For the first time, I am actually giving a macro spreadsheet to someone excel literate, but macro illiterate.
This spreadsheet basically opens a bunch of word templates, changes some bookmarks, saves as a word document and as a PDF.
I cannot afford for this spreadsheet to fail, which brings me to error handling.
I haven't really pushed into error handling too much previously. But I have come up with a list of possible errors the 3rd party could run into
Invalid Inputs (various)
Cannot find word templates
Cannot find bookmarks
Cannot Save As Word (various reasons)
Cannot Save As PDF (various reasons)
Not having Microsoft Word 16.0 object library
Is there any other errors I should be catching?
how do I deal with the Not having Microsoft Word 16.0 object library?

Sorry to be writing this - I know it's not the answer your looking for, but my experience has been that it is not possible to create an exhaustive list of errors that might occur... especially user errors.
Nevertheless... here are some more to add to your list:
Check if template or other source document is already opened by someone else
Check if directory exists (if different to the one being pulled from)
Try to lock down as much as possible for user... let them only access that which they absolutely need to

Related

Power BI unable to read excel worksheet with multiple tabs

I have an excel worksheet with 10 tabs.
For each tab, the data is structured as follows:
All tabs follow this same basic structure.
In Power BI, when I go to "Get Data", and then choose the .xlsx file, I get the following error:
Unable to connect
We encountered an error while trying to connect.
Details: "The input couldn't be recognized as a valid Excel document."
This is very frustrating and I don't know how such a simple task cannot be accomplished in Power BI.
Thank you.
Such alert could appear when you try to use Power BI connector on Excel file. It's understandable if the source file is corrupted and can't be opened in Excel. However, it looks strange if Excel opens the file in question and shows nothing wrong.
Based on our experience above is usually means what something is wrong with XML scheme of the Excel workbook.
Mushup trace (Data->New Query->Query Options->Diagnostics->Enable tracing) could give some additional information, but often not enough to find the reason.
We had two main scenarios
XML scheme is not complete
Usually if Excel file was generated by third-party tool. Such tool could generate quite limited XML scheme which is enough to open the file in Excel and to work with it, but not enough for Power BI connector. As an example, trace log shows
[DataFormat.Error] The input couldn't be recognized as a valid Excel document.\r\nStackTrace:\n…
…
[DataFormat.Error] We couldn't find a part named '/xl/sharedStrings.xml' in the Excel package.\r\nStackTrace:\n…
Such case is easy to fix – it's enough to open the file in Excel and save it (without any changes) – Excel is clever enough to fix the scheme. For the routine regular tasks we use poweshell script which does exactly the same in background.
There is the link within Excel file which is not recognizable as valid
Usually if Excel file is synced/kept with some cloud storage. One of the variants, wrong link could appear with copy/paste from another such file. That could be active link in one of the cells; or the link within conditional formatting formula; or even the link which actually isn't used by Excel but kept somewhere inside the scheme. For example, in one of the files I found in Data->Consolidate->All references the link like
'\drive.tresorit.com#7235\Tresors….[file.xlsx]Sheet'!$AC$6:$AC$357
on the file which was deleted long ago and isn't used, but for some strange reason the link was kept within the scheme.
Unfortunately for such case trace log doesn't give enough information to localize the issue, it looks like
[DataFormat.Error] The input couldn't be recognized as a valid Excel document.\r\nStackTrace:\n…
…
nExceptionType: System.UriFormatException, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089\r\nMessage: Invalid URI: The hostname could not be parsed.\r\nStackTrace:\n
Perhaps I have not enough knowledge for more straight forward localization of the problem, but the only way is to exclude Excel file parts one by one and check if the issue disappeared. Another way could be to unzip Excel file and check if wookbook.xml or sheetNN.xml have something suspicious inside.

Is it possible to store reusable VBA code in a library and call from Outlook?

I have code written in Outlook 2010 VBA that I want to share with co-workers (an Outlook rule runs that calls the code, which saves the current e-mail to a network folder as a text file). What I would like to do is save the VBA code in a library somewhere on the network, and have Outlook call it there. This way, others can call the same code and there is one set of code that all instances of Outlook can point to. If you can point me to info on setting this up I would appreciate it!
I do a similar thing in VBscript programs, using executeglobal to basically run a file containing the functions (acts like an include file) and am looking for how to do it in VBA.
Well it seems its not as easy as I was hoping. After perusing the links supplied by MP24 (Thanks MP24!), I learned the Outlook OTM file is not really intended to be shared. It errored for a colleague when I saved it to a network drive and she tried to use it via tools/references. To share code, one can export it as a .bas file, and another user can import it. This will add it to their own code if some exists already. The proper way seems to be to write an add-in, but that is beyond the scope of what I need for what I am doing. So, if a colleague wants to use my code I'll export and they can import it. The trouble is if I change something they will have to delete it and re-import. Oh well.

Need a office 2007 Macro for the below situation?

Here is the situation: We have a mobile app which sends the data entered/captured by user through email to any email client selected by the user. Data/attachments consists of one .csv(excel file) and .jpg file(may be 1to3 pics).
Basically we have automated the manual report filing (before automation, folks use manual report filing using normal/manual paper filing technique).Now i want to create a good looking report in ms word or in excel(if later is not possible) from what i have sent(attachments)to the email.
Few links to give you guys a hint about the target doc file and excel file with attachment.
Excel file: http://i1117.photobucket.com/albums/k596/hitmanx07/Excel.png
Doc file: http://i1117.photobucket.com/albums/k596/hitmanx07/Doc.png
Need a automated solution so that every time user downloads the data from email he/she could possibly change the downloaded excel file into a doc(see attached).
Hope i'm clear
mrana...
Here is the solution for above:
Ms-office Excel's Macros is the solution for this issue. Basically Macros work in such a manner that what steps user performs on excel to solve the issue have been recorded by macros and then user can perform same task within seconds which if done manually could take so many time by use of that recorded macros.
So basically its an automation technique.
Please use this link to see how to deal with all these things.
http://www.csus.edu/training/handouts/workshops/Excel07pivot&macro.pdf
gracius.

PowerPoint - How can I figure out if a presentation has a password programmatically?

I'm writing an application that processes PowerPoint presentations and I'm looking into ways of detecting whether or not a PowerPoint presentation has a password if it does then stop processing it.
It seems to me that there is no way to check if the presentation is password-protected. You have to open the document first to make properties accessible. You have to provide the password when you are opening the document.
There is a Password Property you can check.
I've found a workaround on Expert Exchange:
The problem is the following: Visual
Basic is a single threaded
application, this means that you cant
put a certain procedure on hold and
proceed with another (in the same
application). When powerpoint has an
"on open"-password set, the
CreateObject procedure inside your
application is put on hold untill
powerpoint releases it, this is when
the user types in a password. There is
no way around this that I know off,
but you can make a workaround:
Make 2 applications, 1 application is
the application you already have, the
second one is a "powerpoint unlocker".
You run the second program just before
you open the protected powerpoint
presentation in application 1. You can
do that with the shell command. The
"powerpoint unlocker" can be as
advanced as you want it to be, you can
for example provide command line
parameters to specify which
presentation must be unlocked with
what password. Then you use the
findwindow api to get the window
handle of the locked presentation.
Once you have that, you use the
sendmessage api to input the password.
After this the "powerpoint unlocker"
unloads and the first application can
resume with its excution.
I hope this helps!
Osmodean
Consider looking at this -
http://blogs.msdn.com/b/openspecification/archive/2009/07/17/overview-of-protected-office-open-xml-documents.aspx
For a PPTX document, you can examine the first 8 bytes to look at the header (should be [d0cf 11e0 a1b1 1ae1] for an encrypted file), and know if it is an encrypted PPTX or not.
However, for files created with Office 2003 (default extension .ppt), the header is the same (MS-CFB header). So, if somebody creates an office 2003 document and then renames it to a PPTX, your code will consider it as an encrypted document (whereas, it wouldn't necessarily be one).
If you are working with documents already opened in Powerpoint, you can use the SaveCopyAs function to first save the document to disk in the pptx format (use the default option in the second parameter), and then examine the header to check if it's an encrypted file.
Consider looking at this -
https://msdn.microsoft.com/ko-kr/library/dd948895(v=office.12).aspx
For a PPT(office 2003) document, you can examine the unsigned integer(0xF3D1C4DF bits) to identify whether the file is encrypted.

Excel Sheet Project References

I am programatically opening excel workbooks under a folder tree to check for some project references using the following code -
workbook = app.Workbooks.Open(fileName,false,true,Missing,Missing,.....);
foreach(Reference r in workbook.VBProject.References)
{
//check for a specific reference here
}
This works fine but my folder structure is very deep and I have over 20,000 spreadsheets stored in them. Sometimes depending on the size of the excel file, the call to Workbooks.Open() takes a long time (over 5 minutes per call on some files). Is there a faster more efficient way to do this?
Thanks for the help
It seems like whenever you have to hit the Excel object model, you're going to take a performance hit. I agree with the previous poster, that if you want to speed up performance, you'll need to read the Excel files directly.
As a side note, since Excel 2007 files (*.xlsm, *.xltm) are essentially *.zip files, you would need to find and access the vbaProject.bin file directly. A quick look points to the path as (I changed the extension so I could browse the file):
..\Book1.zip\xl\vbaProject.bin
Obviously you could dig through that bin file manually and find particular references (as suggested by the previous poster), but if you're looking to loop through all of the references in a project, you'll need to use the API calls IStream/IStorage. There's a great article about reverse engineering the Office BIN files here: http://www.codeproject.com/KB/cs/office2007bin.aspx. To access references in vbaProject.bin, look for the section titled "Reading or updating vbaProject.bin parts". There is also a sample C# code project that demonstrates how to read an OLE container. I just took a peak at the code sample, so I can't attest to it's effectiveness, but it certainly seems in order.
Hope that helps!
I don't think you can increase the Workbooks.Open performance. However if your main intention is just to check if a particular reference is used by the Spreadsheet or not, then consider opening the Excel file in binary mode and searching for the dll string.(The path of the dll providing the functionality, which can be seen in the Location part of the References Window).
This would be very crude way, but if the Workbook.Open performance is really a bottleneck then you can definitely give it a try.

Resources