Topics Search

Using Shell in VBA

Using Shell in VBA
Views: 149
I am writing code to open a .csv file in access and then save this as an .xls file. I can open the file using shell but haven't been able to manipulate excel. I've tried the AppActive method but it doesn't seem to change the focus to the excel file. Here is the code I'm struggling with.

csvfile = Shell("excel.exe" & " " & TargetFile, vbNormalFocus)

AppActivate csvfile
SendKeys "%F"
SendKeys "A"
SendKeys excelfile 'rename the file as an .xls
Sponsored Links:

Similar posts...


multi lined shell command?

multi lined shell command? Icon
I have some VBA in a MS Access module that properly and successfully executes a shell command that executes a batch file in the following manner:Shell("C:\runthisfile.bat")I am trying to actually place the contents of the batch file into the module. The problem is that the batch file is many lines. How would I write that?

Shell("ftp.exe -s:%0)
Shell("open myftpserver")
probably 100 lines with each line being a separate shell command, but that doesn't work and neither does
Shell(" all lines")Is it possible to essentially shell many lines at the same time thereby making it possible to take say 100 lines resident in a batch file and just placing them all in the VBA module with shell command(s) instead of calling out the batch file that resides outside of the ms access file? It seems that I can only do single line shell commands?

reg.exe through command prompt and Shell in VBA cause different behavior

reg.exe through command prompt and Shell in VBA cause different behavior Icon
I'm running Access 2010 32bit on Win7 x64 and was mostly aware of what it means in relation to registry and how it behaves with redirection, but was not aware it will cause discrepancy inhow included Windows libraries perform.

Here's description. Windows (from XP onward I think) include one brilliant little command line app reg.exe which enables tons of tricky operations with Registry in very simple manner. For example, I need to copy a key with subkeys and values to new location (from HKLM to HKCU).

This little app does it in a single line command! and can of course be used from VBA through Shell command.

However, same line run from command prompt and through Shell function copies different keys. The one through command prompt copies asked key, the one through Shell function copies redirected key (from Wow6432Node, instead from HKLM\Software).

Of course, I don't want that! I know everything will be fine on x32 Windows, but on x64 Windows it simply reads wrongkey.

And that's exactly what I tried to avoid by this approach! Is there a way to run reg.exe from VBA as if it's run from command prompt, or force it not to redirect?

lost ability to "shell" out when database moved

lost ability to "shell" out when database moved Icon
I had to build a new intranet machine a while ago and just found out that at the end of my macros, I get the error: "The expression you entered has a function MS Office Access can't find" -- I believe it is related to the "run code" macro which simply does this: shell("c:\runtest.bat") --- It looks like I can't run anything with the "shell" command - as if the "shell" command doesn't exist in Access anymore ? What has happened

Telnet from VBA

Telnet from VBA Icon
Is there a way to interact with a shell from vba code ?
Like opening a telnet session, send userid, password, and other commands ?
if command returns ok then send next command ?

How to run a Macro or VBA code in another database

How to run a Macro or VBA code in another database Icon
I am designing 2 databases (frontend and backend) and writing all my macros and vba in the backend database. In the frontend database I basically have a form for user to select options (button commands).

I want to link this command buttons from Frontend database to run VBA codes/macros in the backend database.

So far I tried 2 options and none of them are working

1) Shell command - I used the following command found on internet
Call Shell ("filepath \x Macroname, 1)
I get run time error 76

(It allows ONLY to run queries and NOT Macros)

Dim db As DAO.Database
Set db = DBEngine.Workspaces(0).OpenDatabase("filepath")
'db.Execute "query1"
db.Execute "query2"
db.Execute "query3"

This runs query 1 and query 2 but give RUN TIME ERROR 6 (overflow for query3)

How do I open another database using VBA?

How do I open another database using VBA? Icon
so this relates to some access updating questions I've been asking. I went to that AutoFEUpdater site and read through it, and it all seemed a bit complicated and required too much user interaction. I'm dealing with non-pc literate people here and I'd like to make the whole thing program driven.

I saw an alternate solution on the website that involves opening a separate updating database, using that database to automatically replace the old file with the new file, then reopening the database. The updater program works fine, but I'm having a hell of a time trying to figure out how to get the program to open using a command button.

I've been doing research and it seems that the "shell" command is supposed to be used, but none of the places I've looked show how to actually use it. They only talk about the problems they have. What is the syntax for using the shell command? Am I supposed to have a specific reference set up in my VBA?

I'm so close, just can't open the damned updater

Trying to excute a batch file from VBA code

Trying to excute a batch file from VBA code Icon
I am using MS Access 2007 and I am trying, from my VBA code, to execute (or run) a Batch file. Now the batch foile works, but not from my code.
I used to be able to just do this --> Call Shell("S:\Accounting\AP\Tex Files\File List Generator.cmd ", 1)
ANd it would runit. But using this I get a message telling me that it is an invalid procedure call. So upon looking around on the net I found tha ton place said that with MS Access 2007 you need to do this:
Call Shell(Environ$("COMSPEC") & " /c S:\Accounting\AP\1099 ITEMS\W9 Folder\Corrected 2011 W9\File List Generator.cmd ", vbNormalFocus)
But while it does not error out it dooes noting eitheer. What the batch file does, when it works, is produces a list of all the file names in a folder

DB Crashes on open of VBA

DB Crashes on open of VBA Icon
I have a DB that worked fine yesterday. I came in this AM and it crashes every time I try to open any module or VBA code. I have tried Compact & Repair as well as migrating to a new shell DB.

This is pretty crucial and I am at a loss as to where to begin debugging. It's not when I'm running anything. Strictly when I try to open modules/code for developing.

Widget compendium?

Widget compendium? Icon
Where might I go to see a collection of really great Access VBA event procedures, macros and widgets? I read a great Linux book called, "Wicked Cool Shell Scripts" and I thought, I bet there is a similar resource out there of Access event procedures and VBA code snippets. Where should I lo


TransferText Icon
I need to move comma-delimited text files into Access.

I want to loop through a directory using VBA and create new tables in Access for each text file.

Right now, my VBA shell looks like this,

When I use Access's import wizard and step through (Delimited >> Delimiter = Comma, Qualifier = " >> Finish), my file imports without errors.

I save an import specification with those settings (Delimited, Delimiter = Comma, Qualifier = ") and re-import the file to verify the specification. Using the wizard, (Advanced, Specification = "Import" >> OK >> Finish). My file imports without errors.

But when I run the shell above, my file imports with 279 Type Conversion errors.

This occurs whether the syntax that follows TransferText is acImport or acImportDelim.