How to password-protect a VbaProjectPart using OpenXml SDK - vba

I have a scenario where users download their Word document from our local server and when they save that document, instead of saving it locally, the document is sent back to the server using a macro (which is "injected" by the server every time a user downloads a document that doesn't contains the latest version of the macro).
Now that we are going to send this solution to our final tests prior deploying it, we needed to protect that macro, I know I can password protect a macro using Microsoft Office by simply right clicking in the macro project inside the document and selecting "block from viewing" and inserting a password.
However, since the macro must be handled by the server, I need to find a way to protect that macro when I generate it, currently the macro is generated using OpenXml SDK using a VbaProjectPart. I couldn't find anything on the web.

Instead of trying to sign and protect the macro programmatically using Open Xml SDK I decided to download the document, sign it and protect it using word then I used "Open Xml SDK Productivity Tools" to "reverse engineer" the DOCM created and then I copied the binary "VbaProjectPart" created by the tool.
It's not the best way to do it since I need to have a separate documentation for this process and need to do the whole process when the macro changes, however, since the code isn't expected to change anymore I decided to go that way.
Hope this helps anyone who wants to know how to include password protected macros inside an Open Xml document.

Related

Word macros disappear on transfer

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

Upload Excel document to shared folder on Google Drive with Excel VBA?

I have found different scripts for Java, C#, and Access VBA for uploading a file to Google Drive. But the only thing I have found with Excel VBA, is a script saving the file in your local Google Drive folder and then wait for the application to sync it.
Is it possible to somehow upload the Excel file directly to a folder that has been shared with me? If yes, how?
I actually managed to do this.
First, you need to create credentials. Follow this guide: https://updraftplus.com/support/configuring-google-drive-api-access-in-updraftplus/
The credentials should be OAuth Client ID
Application Type: Other
Secondly, I downloaded this Access VBA code found here https://stackoverflow.com/a/34627231/1042624
Afterwards, in the Access document, there will be a settings box. You need to fill in the information about your account and the credentials from step 1 here. Then you can use the upload button, and it will work.
However, if you want to use this in Excel and without the form boxes, then you have to step through the code and find where the settings form boxes´ info are being used. It takes quite a while, and the code is too long to post here. But now you at least have the information on how to do it.
NB: I still did not crack the 2-step verification that I have enabled.

Deploying a VBA-macro

I created a VBA-macro which will be used by some word-documents within my company. The macro detects tags and removes chapters from a document. This document is created by another program. So the macro should be separately distributable.
Is it possible to generate an executable which adds the macro to the user running the executable?
Is there another way to package macros and install them on a user's computer?
Thanks
The easiest way to deploy Macros is via a template. Create your Macro and save the file as .dotm (macro-enabled template). I think you will get a suggestion where to save your .dotm-file.
Any colleague who wants to use your template simply has to put it in that directory (I think it's C:\Users\[UserName]\AppData\Roaming\Microsoft\Templates). After that, he should be able to use the macros while working on any word document.
There are basically five ways to do this:
1) Send everyone a text file with your macro that they can paste into their own Normal template. This is fine for very simple macros that are unlikely to have any name conflicts with macros users create themselves, but it does require basic knowledge of the VB editor.
2) Send everyone a .bas file that you create by exporting a module that contains your macro(s). This gives you a little more control and avoids copy/paste errors. Still requires basic understanding of the VB editor (or decent instructions from you).
3) Package your macros in a template (.dotm file) that lives in the Templates folder. Users can apply that template to any document they're creating and gain access to your macro(s). No VB knowledge required; this is done through the standard Word New File process. Also allows you to include styles or other things if you want.
4) Package your macros as a global template (.dotm file) that lives in the Startup folder. Users will have access to your macro(s) in every file they work on, no need to apply your template. This is good if what you are doing is central to your team's workflow and doesn't require that you include styles with your macro. You can also build in UI elements. (There can be issues with this approach in Word 2011; users may not have immediate access to the global template but it is easy enough to get back.)
Both 3 and 4 do require that the user initially place the .dotm file in the right place. You can help them with this (one approach is to use another Word doc as a "setup" file that, when run, places the template in the presumed correct folder). Obviously that requires more work on your part so how far you'd want to go with that depends on you and your business needs.
5) Additionally, if you have control over the creation of the document itself (rather than just the macro) you can embed a macro in the document. You can place the macro itself in the document's ThisDocument module (find your document in the Project Explorer and then open Microsoft Word Objects). Then save the document as .docm (macro-enabled document). Users should be instructed to enable macros when they open the document (different versions of Word use slightly different interfaces for prompting the user about this, but it's always pretty obvious).
Over the last 7 years I have been deploying my Word VBA in a different way. The software is a Word add-in that makes it easier for teachers to provide feedback on assignments. It is distributed as a 30 day trial and if the user buys it they are given a key which enables them to use eMarking Assistant for a year. You can test the deployment system at http://eMarkingAssistant.com
The deployment and licensing mechanism is given below:
save the vba in a macro enabled document i.e. a .docm file
in Windows rename the file to be a .doc file
use Orlando's excellent "VBA decompiler and compacter" from http://orlando.mvps.org/VBADecompilerMore.asp to remove compiled code and references to specific Office versions from the .doc and compact the document
ask the user to download the .doc file
ask the user to open the .doc file and ensure that macros are enabled
let the user trial the software in the document
if they want to use the software in any document they click an "install" button in the document to copy the vba code to to a .dotm file in their Word startup folder (so it is loaded automagically)
if they want to buy a subscription to use the software, they pay using paypal and I send them a key which unlocks the software until the end of the subscription
Advantages of this process are:
a single document can be used on all versions of Office for Windows from Office 97 to Office 2016 (32 bit and 64 bit).
the install and uninstall all happen within Office so the suer does not need to admin rights over their computer
users do not need to install the software until they have used it in the document
users do not need to use another program to unzip or install the software
Peter Evans

property promotion in infopath and sharepoint 2010

I have a SharePoint 2010 document library with >7000 xml documents created from infopath forms(Infopath 2007). Now i would like to promote few fields in the form to the SharePoint document library. I could do this only for documents created from the upgraded form, but not for all previously created documents. Although re submitting of old forms will work,It is not possible for me to update all 7000 records to promote the values to the column. Is there a much easier solution, considering the fact that this changes need to be implemented on a production environment too.
Note:The promoted columns will be used to generate graphical report. Any solution acceptable.
I've been through this before, and there's no good answer. If I understand correctly, you have existing InfoPath forms in a doc library, and you now want to promote fields to the document library, but you don't want to open each form one by one, correct?
Note, you most likely don't have to open then re-submit each form, you just have to open the form, and close it. Once you do that, the promoted fields will them show up.
So... what I've done before is this: First, get notepad++ (this allows you to open multiple files in tabs). Secondly, access the the doc library via WebDav (that is, go to the library in SharePoint, then go to the Library tab, and click Open with Explorer). Thirdly, open a large batch of files at once using notepad++ (select files, right-click, open with notepad++). It will take a moment for all the files to load in notepad++ in tabs. Once they are all open in notepad++, hit ctrl-w as fast as you can (which closes each file). Rinse and repeat.
It's not pretty, and I'm sure there's a better way to do this (programatically, perhaps), but this should work. At least you won't have to open each form one by one.
You can do this by relinking the document by powershell or through advanced settings.

How to read changes in excel file into vb.net application

I have an excel file which is auto generated by third party software..and its readonly file and its data constantly keep changing online.
now i want to use or read that data in my vb.net application whenever its changes take place in excel file automatically.
please help me..
Cody's right. No question, but I'm guessing you're looking to do something along these lines.
First, you'll need to pull down a copy of that XLS file (sounds like it's online at a website somewhere). To do that, you'll need to perform a pretty normal HTTP get or use whatever API the website provides to pull the file down.
Save the file locally, then open it and read it using EXCEL AUTOMATION. Just google excel automation for loads of examples of opening a worksheet file, reading cell values, and closing the file.
There's other options too such as the OpenXML api from Microsoft to allow you to read the contents of XLSX files directly without using excel automation, so that may be a possibility. not enough in the original post to know which would work best for you.