VBA Zip File error - vba

I am using code similar to the below to add some files to a zip folder:
Set oApp = CreateObject("Shell.Application")
oApp.Namespace(CVar(strDest)).CopyHere CVar(strSource)
This works fine most of the time, but sometimes I get an error:
"(Compressed (zipped) Folders Error) Cannot create output file". This error is raised asynchronously outside of my VBA code and as such I cannot trap it to take remedial action.
If I enter break mode and step back to:
oApp.Namespace(CVar(strDest)).CopyHere CVar(strSource)
then the file is added correctly.
I am guessing that there is some kind of lock either on the compressed folder or the source file that is causing this problem, but I am unsure as to how to check this. I should note that the files are being added are pdf files created from Microsoft Access 2007 and we are using the fully qualified paths and the code runs as follows creating up to 10 pdfs per zip file:
Create Zip
Run this loop:
For Each ReportToRun
CreatePdf ' using DoCmd.OutputTo acOutputReport, "rptHame", acFormatPDF, strReportName
AddToZip
Next
Any idea how to either fix this or trap the Cannot create output file error?
Thanks

I've very successfully used the open souurce Info Zip DLLs. See Compression DLLs, OCXs, etc for links and sample VB code.

Two thoughts, neither likely to fix the issue:
Have you tried explicitly providing FALSE as the next argument, i.e., the AutoStart argument? The help file say it defaults to FALSE, but maybe it's opening the PDFs after generating them, so that they are still open?
add a SLEEP of a couple of seconds (see Make code go to Sleep for the code).
alternatively, loop through all your reports and generate all of them, then start a new loop to copy each one into the zip file, rather than generate/copy for each report. Having a couple of seconds sleep between the two loops might not hurt (assuming that the problem is that the asynchronous PDF generation process is not fully complete at the point the copy is initiated).

Related

How do I embed an exe file in an MS Access Form and Run it at run time?

I'm using MS Access 2003 for Special and old problems with the .mdb project
I want to embed my files like .txt or .exe or .pdf and run them at runtime or write that on disk.
What have I tried :
enter image description here
I don't know what's the code to do what I need I couldn't find my solution on internet.
Private Sub Form_Load()
'Me.OLEUnbound2.Application.Run
Dim obj As Object
Set obj = Me.OLEBound1.Object
'obj.DoVerb (acPrimaryVerb)
End Sub
Update:
I wrote a C# program called BMH.exe, which I open and run through Access, but I want this file to be in my source in any way possible so that the user does not have to place this file next to the program or somewhere It has Windows,
I also don't want to do things like downloading from the site, creating an installation file, I just want to access this program in any possible way through the embedded file itself, which is in the form of OLE Object and from the Packager Shell class. Write the object to a specific address or run it directly from Access itself
You can save the file as attachment in attachement field. If it will not accept an .exe file switch the extension and switch it back when exporting, you can do all this with code. Alternatively you can store the file as binary in an Ole field.

Print button didn't save the PDF file at a shared path, and no error logged

I have an Excel macro that uses SAP for printing data in pdf format.
Steps:
it access the SAP transaction which provides a table with the necessary information
it press Print button (from the Menu Bar of SAP)
then Print window appears (from here it is selected Microsoft Print To PDF option and then it is pressed OK button)
Save As window appears (complete the path and filename: \S\BC....\outputName)
then Save button it is pressed
then no error happens
But if I access the path folder "\S\BC....", nothing happen, there is no file saved. BUT, if we look in "Date modified" property of the folder, it is modified in accordance with the last saving time.
Also, I mention that if I'm trying to write the pdf file on local folders, not network shared folders (example: a desktop folder: C:\Users\mariah\Test), macro works.
Also, I mention that user can Read&Write at the shared path \S\BC....\
Please help me to find the cause of this issue.
A VBA macro only does what you are allowed to do manually. Can you manually save the file in the shared path without the macro?
Is the common path really \S\BC...\ or \ \S\BC...\ ?
Regards,
ScriptMan
Solution proposed by the OP in this comment:
IT WORKS if I save the file on "C:\Users\UserName" and then cut it to shared path "S\BC...". So I've implemented code that saves the file in a path and then copy it to another path and then I deleted it from the first path (such that user never knows that I copy the file in other destination than desired destination).

Word file corrupts after SaveAs

I've build a program to generate other docm files. The program works just fine without saving the generated files through VBA. When I do save through VBA with a SaveAs statement, however, the generated file becomes corrupt. Trying to open the file gives the following message:
"Could not open the file, there is a problem with the contents."
The SaveAs statement is as follows:
qDoc.SaveAs (getQuizURL(quizname))
The quizUrl function has been thoroughly tested and operates properly.
Does anyone know what's causing this and how to solve it? I could save it all manually, but given that other people will be using this program I'd like it to make it as friendly as possible.
You are missing file format specification.
I am not sure about SaveAs either from your code.
I have got Office -2016 and it has a function called SaveAs2, but may be SaveAs is available in older versions.
Anyhow, change your code to specify the file format.
ThisDocument.SaveAs2 "C:\temp\Test.docm", WdSaveFormat.wdFormatXMLDocumentMacroEnabled
This will do the trick.

Programmatical and application-based editing of an Excel file

Good evening,
I have the following problem to solve:
I want to add to an Excel file the contents of a bunch of user-generated .txt files. These files are generated throughout the day and sent over FTP to a folder, which is being constantly monitored by the program to see if there are new additions.
If the program finds new .txt files in it, it opens the Excel file which is to be edited, adds the info and then closes the Excel, saving the changes.
At the same time, users have to open these Excel files to check the new updated info and deal with it accordingly.
The program's execution is somewhat like this:
Infinite loop checking if the folder is empty or contains new .txt files.
If the folder is not empty (hence there is info to be added), it checks whether the Excel file is open or not.
If the Excel file is closed:
Opens it programmatically and adds the info to it.
Saves the Excel file and quits.
Backups the .txt to another folder in case there was some sort of error.
If the Excel file is open:
Keep checking until it is closed.
To check if the folder is empty or not I use: System.IO.Directory.EnumerateFileSystemEntries(path).Any()
To check if the Excel file is open I use:
System.IO.FileInfo(path) and a FileStream which, inside a Try-Catch clause, opens the FileInfo in the following mode: info.Open(FileMode.Open, FileAccess.ReadWrite, FileShare.None)
For the opening, editing and saving/closing of the Excel file I use Microsoft.Office.Interop.Excel.Application, [..].Workbook and [..].Worksheet.
The main problem comes when a user opens (through the normal Office Application) the Excel file in the middle of the addition process. I was hoping to find some sort of lockage of said file so the user cannot interrupt the editing process.
Any ideas on how to combine both types of opening/editing the Excel file?
Thank you so much in advanced.
TL;DR: How can I prevent, programmatically, a user from opening an Excel file while a program is editing that very Excel? Or at least, open another instance of it so the process is not interrupted?
PS: If any further code should be needed, I'll gladly post it :)
I think you need a better solution. It sounds like you are maxing out the capabilities of Excel. You would be better off, if possible, to use a database.

separate files for VB code and Excel

I'm using Excel 2010. I have some code in background (VBA) that is growing up from time to time. I'm trying to find a way to separate the source code from the xls file, so I could compare the code changes. In other words, I want that the code will be in a textual file, and every time I'll open the Excel file, the source code for macros will be taken from this file.
Thanks in advance!
Take a look at this question on SO.
It has the mention of addin called SourceTools, that I have used & find it worthwhile.
Also, it comes with source code so it can be modified to point it to the source code control software (such as SVN) that is specific to your use.
Feel free to close this question as the link I gave has the same question as yours & answers what I suppose you are looking for.
Have a look at the various code cleaner apps/code available for VBA, such as:
http://www.appspro.com/Utilities/CodeCleaner.htm
Among other things, these export the modules/forms/classes to text files, delete them, then re-insert them into your projects.
With a few mods, that'll form the basis for what you're after.
Another possibility: I don't do much in Excel, but if its add-ins behave like those in PowerPoint, that might help also. In PPT, installed add-ins load automatically when PowerPoint starts, create any user interface needed and are available to use with any open files in the app. To update the code, you modify it, create a new add-in, put it wherever PPT is looking for it, and restart PPT. Voila ... code's updated for all PPT files.
First of all, thank you all for your answers. my solution was:
1. export all the modules to *.bas (one file per module).
2. add the modules code my calling:
Application.VBE.ActiveVBProject.VBComponents.Import (filename)
for each file..
3. after finishing:
Set VBComp = VBProj.VBComponents(moduleName)
If Err.Number = 0 Then 'no error
VBProj.VBComponents.Remove VBComp
that's remove the module so it won't be saved in the xls before quiting
I would recommend a manual process in such a scenario.
Suppose you want to take a backup of Module1, then right click on it and click on "Export File". You will get an "Export File" dialog box. Save it as, say Module1 - 22 Apr - 2012.bas at a relevant location. And you are done
How would this help?
1) The dates in the file name will tell you what date the backup was taken so you can actually keep track of the date when the macro was changed.
2) .Bas files can be opened with Notepad. This will help you in comparing the current VBA code with the relevant backup file.
3) If at any point of time you want to retrieve the backup from a particular date, simply delete the existing module (take a backup of it if you want) and then click on "Import File" and import it in your VBA.
HTH