I am having trouble with getting the HTML Application (MeowcatSoftware Launcher Demo.HTA on GitHub) to open a target application, for example, MultiToolv0.2.exe. Is there a way to open the target applications such as the MultiTool using VBScript besides using Wscript.shell objects, which has been causing most of my problems?
I have tried the following, which didn't work:
Sub RunProgram
Set objShell = CreateObject(“Wscript.Shell”)
objShell.Run “notepad.exe c:\script\test.txt”
End Sub
(From 'Hey, Scripting Guy" Blog Post)
I played around a little with it but couldn't figure out how to achieve my goal. The blog post also mentioned using a Windows Shell object instead of Wscript.shell, but it looks like from the example that the Windows Shell object method was for opening a file using another program, and I just want it to simply open the target application. How do I open a program using VBScript within an HTA Application?
In vbscript this should work : You should using quote (") not like you posted in your question (“) and (”)
Call RunProgram()
Sub RunProgram
Set objShell = CreateObject("Wscript.Shell")
objShell.Run "notepad.exe c:\script\test.txt"
End Sub
Related
Edit: See my comment below for a partial solution.
Edit 2: I found an adequate solution for closing the VBA editor but just want to see if anyone knows how to make it fully invisible the whole time. What I have found out works for my needs but I will leave this thread open for anyone who wants to elaborate on another method or expand on mine.
Original Post: I have a function that creates tables, queries, and forms. For the forms, it copies a template form and calls a function that replaces the forms VBA code dynamically. The below function I created works great, however, if I do not have the class object form open in the editor, I get the Run-Time error '2516': "Microsoft Access cannot find the module 'SPCInputFormVBA.', where SPCInputFormVBA is a variable for the class objects name. To further elaborate on the behavior, if I have the editor closed, as long as the module or class object is open in the editor, it will still work. I would like to be able to activate the corresponding Form_xyz object in the editor without the editor opening so that I can use this function to do a bunch of stuff to the to it. I am using a template form because there are a lot of things nested in it and has a lot going on. The form I can change the record source and various other things in it just fine, but the VBA portion is elusive to me so far. I thought opening the object would be easy but I am having a lot of trouble and cannot find a way to describe my issue that leads to me finding a solution.
Public Function InputFormVBA(SPCInputFormVBA)
DoCmd.OpenModule (SPCInputFormVBA)
Dim i As Integer
With Application.Modules(SPCInputFormVBA)
For i = 1 To .CountOfLines
If InStr(.Lines(i, 1), "TempTable") > 0 Then
' .ReplaceLine i, " If DCount( ""serial"", """ & tblName & """, _"
End If
'If Instr(.Lines(i, 1), "
Next i
End With
End Function
The closest I have come is trying various things with this function in order to help me understand how Access will react to different potential solutions I find online:
Sub PrintOpenModuleNames()
Dim i As Integer
Dim modOpenModules As Modules
Set modOpenModules = Application.Modules
For i = 0 To modOpenModules.Count - 1
Debug.Print modOpenModules(i).Name
'DoCmd.OpenModule (modOpenModules(i).Name)
Next
End Sub
The 'DoCmd.OpenModule (modOpenModules(i).Name) is commented out in this example and will open things, but it only sees things that are open already which doesn't help me. I do understand that there are different types of Modules, but I am not sure how to distinguish and the documentation online explains a general difference but doesn't reveal any way to reach out to the Class Object unless it is open in the editor already. Hoping someone can help or even correct my terminology if it is off and steer me to an existing solution elsewhere on the site.
You can use the VBE object model to access your form's code module without opening it in the VB Editor. And with that approach, the VB Editor window does not need to be visible or open. If the VB Editor window is not open, accessing the module this way will not open it. So I think that satisfies the main objective of your question.
It's possible to use VBE with late binding but, since you're not familiar with it, you'll likely want to use early binding instead. If so, add Microsoft Visual Basic for Applications Extensibility to your project's references.
Here is a very minimal procedure which only shows you how to reference a code module by name and print 2 of the module's properties.
Public Sub InputFormVBA(ByVal SPCInputFormVBA As String)
Dim objModule As CodeModule
Set objModule = Application.VBE.VBProjects(1).VBComponents(SPCInputFormVBA).CodeModule
With objModule
Debug.Print .CountOfDeclarationLines
Debug.Print .CountOfLines
End With
End Sub
If your database contains only one VBA project, VBProjects(1) will reference it. But if the db contains more than one VBA project, you may need to give VBProjects() a different number. I presume you'll figure that out pretty darn quick. :-)
A CodeModule object has methods you should find useful, including:
DeleteLines; Find; InsertLines; and ReplaceLine. However I don't really know what you want to do with the module's code, so will just leave it at that.
I am trying to develop a simple tool in VB.NET that, within a loop, navigates to a website and save the page as an HTML document.
I can set up the loop easily as the pages are numbered sequentially.
www.example.com/pages/1.html
www.example.com/pages/2.html
www.example.com/pages/3.html
www.example.com/pages/4.html
Where I am having trouble is with finding a method to save the actual page. I was going to utilize a series of SendKeys to Alt, File, Saves As, Enter, etc.. but I figured there had to be some sort of object/method that could be used to do this more straight forward.
I've enabled the COM Internet Controls Reference and declared and new SHDoc.Vw.InternetExplorer and am able to programatically open the browser in a new window and navigate to the desired page(s). I've search online for a solution to the Save issue but have been unsuccessful. Does anyone have any ideas?
Dim baseUrl As String = "http://www.example.com/pages/{0}.html"
Dim basePath As String = "C:\some\path{0}.html"
Using ws As New System.Net.WebClient()
ForEach i As Integer In Enumerable.Range(1,4)
wc.DownloadFile(String.Format(baseUrl, i), string.Format(basePath, i))
Next i
End Using
If you have a lot of these want to get a little fancy, you can even use the DownloadFileAsync() method to queue up several of thses at once.
Edit: The answer to this question can be found within the comments of the accepted answer.
I am attempting to open an Access database from a button click within my excel file. I currently have this code:
Private Sub bttnToAccess_Click()
Dim db As Access.Application
Set db = New Access.Application
db.Application.Visible = True
db.OpenCurrentDatabase "C:\Users\wcarrico\Desktop\wcarrico-CapstoneFinalSubmission.accdb"
End Sub
This seems to work briefly and then Access shuts down almost immediately. If it matters, the Access file has an AutoExec macro that runs through a few tests itself on open.
Don't try to open the Access application then; just create a connection object using one of the Data Access technologies:
- OLE-DB or
- ODBC.
Google "ODBC Connection strings" or "OLE-DB Connection Strings" to get details depending on your particular configuration (and Access filetype).
Probably ADODB is the easiest current library to use for your data access.
Update:
Try Importing the data from Access then using the Data -> From Access wizard. Yu can always use the Macro recoding facility to automatically generate some VBA code for you, that will create some infrastructure for you; I use this regularly when exploring new portions of the VBA object model.
Update - Final resolution of problem, from comments below
That may be because the variable goes out of scope; move the declaration of db outside the function, to module level
The code started Access by creating an application instance assigned to an object variable. At the end of the procedure, the variable went out of scope so Access shut down.
You accepted an answer to use a module-level variable for the Access application instance. In that case, Access remains running after the procedure ends. However if the user exits Excel, Access will close down too.
If the goal is to start Access and leave it running until the user decides to close it, just start Access directly without assigning the application instance to an object variable (Set db = New Access.Application). That db variable would be useful if your Excel code needed it for other purposes. However, it's actually only used to open the db file.
You can use the Run method of WScript.Shell to open your db file in an Access session.
Private Sub bttnToAccess_Click()
Const cstrDbFile As String = "C:\Users\wcarrico\Desktop\wcarrico-CapstoneFinalSubmission.accdb"
Dim objShell As Object
Set objShell = CreateObject("WScript.Shell")
objShell.Run cstrDbFile
Set objShell = Nothing
End Sub
I know this is an old thread, but you will get this error in Excel VBA if you are trying to open an Access database, but you do not have two specific References clicked. (Tools, References on the VBA Editor screen). You need to click 'Microsoft Access 15.0 Object Library' and 'Microsoft ActiveX Data Objects 6.1 Library'.
Remove the New declaration then it works
Actually it is pretty straightforward:
Private Sub bttnToAccess_Click()
db = DBEngine.OpenDatabase("C:\Users\wcarrico\Desktop\wcarrico-CapstoneFinalSubmission.accdb")
End Sub
For this to work you need to declare db as Database at the Module level.
Dim db As Database 'Requires reference to the Microsoft
'Access Database Engine Object Library
Using MS Access, I need to open a PDF file, move the last page of the file to the first, and save it. But the error (see below) keeps getting in my way. (I'm good with VBA, but this is my first attempt at manipulating PDF with VBA).
Note: I'm using AcroRd32.DLL
Option Compare Database
Option Explicit
Sub x()
Dim PDFdoc As New AcroPDDoc
PDFdoc.Open "C:\Reports\MRIR\mrir.pdf" 'activex comp. can't create object
PDFdoc.MovePage 0, PDFdoc.GetNumPages
PDFdoc.Save 1, "C:\reports\MRIR\Switched.pdf"
End Sub
If you just call the document with a shell method, then the following will work according to the following Adobe help file:
http://www.adobe.com/devnet/acrobat/pdfs/PDFOpenParameters.pdf
When opening a PDF document from a
command shell, you can pass the
parameters to the open command using
the /A switch using the following
syntax:
<path to Acrobat> /A "<open parameter>=OpenActions" "<path to PDF file>"
For example:
Acrobat.exe /A "page=4=OpenActions" "C:\example.pdf"
Thanks all, but since I don't have Adobe Acrobat on my local machine (just the reader), I found a 3rd party freeware program (PDFTK) that can do this kind of basic page manipulation from the command-line.
(Kudos to David Walker for giving a detailed answer, even though I couldn't use it in the end.)
This does NOT work:
Sub X()
Dim A As Access.Application
Set A = CreateObject("Access.Application")
'Do Stuff
End Sub
However, this DOES work:
Sub X()
Dim A As Object
Set A = CreateObject("Access.Application")
'Do Stuff
End Sub
I know they do virtually the same thing, but can anyone tell me how to make an access.application object? I should add that I have Crystal Reports 11 and on my last upgrade, it may have 'unregistered' some VBA DLLs.
(Update 2009-06-29)
In response to the first 2 questions, I am using MS Access VBA to control some other Access & Excel files. Since this will only ever run on my local machine, I can guarantee that Access will always be installed. I have also referenced the "Microsoft Access 11.0 Object Library" (MSACC.OLB).
I know there's ways around this, i.e. use early binding when coding, and switch to late binding when running it, I just don't understand why the early binding method doesn't work at all on my machine (Of course, the code works fine on another machine with Access).
If you are writing this in Access there is no need to do that as the Application object is already there for you. If you are writing this in Excel or Word then you need to add a reference to the Access Library. Go to Tools/References and look for Microsoft Access XX Object Library
Hello,The code that you say is not working is legal syntax. What error are you getting? When does it occur? Do you know the line of code it happens at?
Just as a side note, this is legal syntax as well: Dim accApp As Access.Application
Set accApp = New Access.Application
But to be clear, the CreateObject Syntax is legal and not the source of the problem.
Try Detect And Repair from the Help menu in MS Access. Worked perfect for me.