I have an update routine that involves various different procedures that runs during the night. The procedures are all stored in an excel file which calls them in the order needed using commandline.
Now I need to add an Access Database File that is to be included. I can open the Access Database and the file I want, but only in "Read only" mode. It also doesn't recognize the Macro I am calling either.
My commandline looks like this:
MSACCESS.EXE /ro "W:\Mandate.accdb" /x AutoRunProcess
Problem 1:How do you open an access file in regular mode? I have researched the topic a bit and have looked at Microsofts documentation, but found no information about how to open a database so you can run updates and save it again. The examples provided are only for opening in "read-only" mode source.
Problem 2: Macro not found Though my current code opens the correct access database file, it says it can't find the macro. However, the macro runs fine if I start it manually.
Any pointers or help appreciated.
Problem 1:
To not open the database read-only, don't specify the /ro switch. :)
Problem 2:
A general suggestion is to avoid macros as far as possible. Generally, a database should have one macro, that is AutoExec.
Although with a quick test the /x switch worked for me, even if the database has a AutoExec macro. AutoExec runs first, then the /x macro.
An alternative is the /cmd switch. You pass a string that you read in your AutoExec function with the Command() function.
Select Case Command()
Case "AutoRunProcess": Call MyProcess
Case "SomethingElse": Call AnotherFunction
Case "": ' nothing was passed in /cmd
Case Else: MsgBox "Error in command-line: " & Command()
End Select
Related
I'm encountering an issue trying to get some macros and VBA scripts working on a new coworker's computer. I've been using the same code for years on my machine and several others without issue, but when my new analyst tries to run it, it throws an error stating it couldn't save the file. He can manually save the file without issue, but running the script throws the error. The really weird part is that it lists the file path, but replaces the filename with a seemingly random hex byte.
On another file, if he opens it, closes with or without saving, then I open the file and try to save it using a macro, it fails saying "cannot access file...". If I copy and paste the file, open and run the macro again, it saves over the offending file no problem.
We've checked permissions and settings and haven't found anything that was different between the two systems. We're both on VMs. His hardware allowance is less than mine but otherwise they're both IT-managed and identical. I'm at my wits' end... Any advice on what may be the source of my grief would be helpful, even if not a solution.
A couple of things I'd check before trying to step through debugger mode.
Coworker does not have write access to the directory (I think you said you checked already)
The new users' Trust Center Settings are not correct
File > Options > Trust Center > Trust Center Settings... > Macro Settings > Ensure the box is checked to trust the VBA model and that macros are not disabled
The new user does not have the same libraries referenced.
Alt + F11 > Tools > References... > Check if coworkers' libs match yours
Is he using a new/different version of Excel that you? A lot of older code got bojangled when Office 365 hit the scene.
After checking settings, I would ask the following:
What error is thrown when coworker attempts to run the macro?
When does the error get thrown? It could be that you have code that is attempting to edit the file (or has another file stuck in an edit) at the same time you're trying to save it.
I would see why the file name is getting corrupted. It sounds like coworkers' machine is looking to a bit of memory or memory address that is somehow getting forgotten. Like the code is referencing a variable that is out of scope.
User Access Control has been enabled on his VM that is somehow preventing him from saving the file correctly.
Your code does not properly Quit the Excel application, so its still running in the background on coworkers' machine. Have them open/close without saving a file and check Task Manager to see if Excel is still up to confirm
I need to automate the act of printing .xlsx file.
I have already seen some answers to this task saying that it is possible by creating a VBA script, as well as some examples. That is not about what my question revolves around.
Thought, I know that it is also possible to right-click on a .xlsx file and click "Print", which does the exact task that I want. It opens Excel, prints the file to the default printer, then closes Excel. (Windows 7, by the way)
So I'm thinking that the work has already been done here.
What process is launched when clicking this "Print" option? Can it be launched via command line, or "clicked" by a python script or something? And if not, why? How can something so easy to click be impossible to automate? I assume a process of some sort must be launched in some way.
Found it!
This task can be easily launched using python.
import os
os.startfile('C:/path/to/the/file.xlsx','print')
This code will launch the same print task. From there, it is pretty trivial for a python developer to automate the task in his scripts.
However, if you do not know much about Python and do not want to learn it now, an easy (or lazy?) way to add it in any automation script would be to save the two lines of code above in a whatever.py file, and launch it via command line (with Python installed, of course).
The context menu print command for Office documents utilizes Dynamic Data Exchange (DDE) and does not directly run a command that can be replicated from the command line.
You can view the content of the commands in the registry. Browse to HKEY_CLASSES_ROOT\.xlsx and look at the (Default) data column. On my machine, "Excel.Sheet.12" is the type of a .xslx file. Then browse to HKEY_CLASSES_ROOT\Excel.Sheet.12\shell\ to see the commands registered for that file type. On my machine, the Print (Default) is "C:\Program Files\Microsoft Office\Office16\EXCEL.EXE" /dde and the "command" is zn=BV5!!!!4!!!!MKKSkEXCELFiles>]-z5hw$l[8QeZZR4_X=$ /dde, none of which is directly useful or accessible for running from a command line.
It will require another program to allow you to access the interface, but there are programs that allow you to make use of DDE from the command line. I recommend Freddy Vulto's Class Exec. More information and a few other similar utilities can be found here.
Is there a way to make Excel ignore certain command line arguments? I am launching via a command similar to:
Excel "C:\Temp\TestingCLAs.xlsx" /e service=http://localhost:61023/api/PortlandDev/2/ReportSource?Order='100146309'^&Line='0010'
The service string gets read by some VBA in the Excel file and is used to update a query. However, when I launch Excel the command line argument interpreter see's two things:
Open "C:\Temp\TestingCLASs.xlsx"
Open service=http://....
I would like the interpreter to ignore the second argument, however I get the following error when opening:
"Sorry, we couldn't find
service=http://localhost:61023/api/PortlandDev/2/ReportSource?Order='100146309'&Line='0010'.xlsx.
Is it possible it was moved, renamed or deleted?
I know this isn't due to the VBA within the excel file because it happens even when testing with a blank excel file.
I found this reference article: https://blogs.msdn.microsoft.com/twistylittlepassagesallalike/2011/04/23/everyone-quotes-command-line-arguments-the-wrong-way/ and tried to escape the URL with a '^' character but, got the same result.
Also looking at the error above, a .xlsx is tacked on to the end of the second URL above when it wasn't specified in the initial call.
Is there a way to make the command interpreter not try to execute the URL?
Any ideas are very much appreciated. Thanks
Not sure why it works but passing a "/p" switch makes it so that the command interpreter does not try to automatically open the URL. For example, the following can be consumed by my internal VBA without the command interpreter trying to create a new workbook from the URL.
Excel "C:\Temp\Template.xlsm" /e /p http://localhost:61023/api/PortlandDev/2/ReportSource?Order='100142574'&Line='0010'
I have a vba macro to open Excel files, including Read-only files.
I use the following code to do this:
Workbooks.Open((FileName:=MyFileName, UpdateLinks:=0, IgnoreReadOnlyRecommended:=True)
Up until June 2013 this never failed. As of now this no longer works. Now when the macro encounters a read-only file the file appears in a separate window and a Save As prompt also appears (never happened before).
If I click Cancel I get a Path not found error. If I click Save the file gets saved to its original location. This is completely new. There is no Save As command in this part of my code.
It appears to be something new associated with Microsoft’s File > Protect Workbook > Mark as final stuff. Anyone seeing this? I can’t find a way to prevent it. I simply want to be able to open the read only file without further prompts.
I haven't used VBA with excel much, but is it possible that the macro security settings within excel were reset so that it no longer trusts macros or VBA script attempting to use it.
Thanks for the input. I did find a work-around. In newer versions of Excel there is a new ReadOnly designation: Final, i.e. File > Protect workbook > Mark as Final. This is the same as ReadOnly, just by another name, and it does not show up as an option in WorkBooks.Open(FileName:= ...). If I set the Workbook.Final property = False, then I can open the file w/o the SaveAs prompt.
If wbName.Final = True Then
wbName.Final = False
End If
I'm invoking a macro within an Excel document via Powershell.
To invoke the macro, I have to run a named macro and call it in run. However, when the macro is invoked and completes successfully, a MessageBox will appear. As far as I know, this messagebox is the only way to find out if the process has completed successfully.
I have no control over the ability to remove the message box. The powershell script must wait for the macro to finish.
From the document: The documentation for this does not give an option for this situation, or so it appears.
Alternative Options I can work with: (But I'm not sure how to get to the point where the messagebox would be dismissed)
The Excel document can have code inserted within it via Powershell
Options can be changed within the document memory space
Is it possible to run a macro asynchronously and to check back on the execution of the macro?
There are a few convoluted ways to go about doing this.
1) Invoke another instance of the Excel application and run the Macro using that. Then how would you know whether it's done? You pass a global variable by reference to it. And use the OnTime functionality to keep checking every few seconds if its done or not.
An example for calling another excel instance is given here: Stop VBA-Script from "freezing" while sending MDX-Query
2) You can store your script as a .VBS file. Then you call the shell to run the VB script and again check some passed-by-reference variable.
3) Use a hidden worksheet as a buffer, which gets written upon in a particular location once the asynchronous code finishes running. Again, you need some clever OnTime programming to automatically run a polling service... and more importantly, to stop running it!