I would like to reference a new excel object created with shell - shell

I have Excel 2003 and 2010 on my machine so when I use set xlapp = new excel.application it creates a new Excel 2003 application. My work around is to create a new Excel 14 application using shell. It works but I need to open an existing excel file to reference the object later.
I would like to reference the object without opening an existing file. Is there any way to reference the new Excel app without using the file name. Below is code for an existing file. Please show me the correct way to do this.
Dim MyApp As Excel.Application
TaskID = Shell("C:\Program Files\Microsoft Office\Office14\EXCEL.EXE "C:\Book2.xlsx", vbNormalFocus)
Set MyApp = GetObject("C:\Book2.xlsx").Application

You can create it ans close Excel ...
strFileName = "c:\test.xls"
Set objExcel = CreateObject("Excel.Application")
objExcel.Visible = True ' You don't have to use this property....
Set objWorkbook = objExcel.Workbooks.Add()
objWorkbook.SaveAs(strFileName)
objExcel.Quit
Would that work for you? See here original solution: Create an Excel file using vbscripts

Related

Separate process is not creating in MS PowerPoint while creating in MS Word and Excel

We have designed Microsoft Addins for MS PowerPoint 2019 written in VB.net.
There I am trying to create separate process in MS PowerPoint to open a presentation/file using the given below snippet
Dim objPpt As PowerPoint.Application
Dim objDoc As PowerPoint.Presentation
objPpt = CreateObject("Powerpoint.Application")
objDoc = objPpt.Presentations.Open(processFile, False, , True)
objPpt.Visible = True
but it creates threads of Parent file and does not open a file as a separate process where in MS Word I use the given below snippet
Dim objWord As Word.Application
Dim objDoc As Word.Document
objWord = CreateObject("Word.Application")
objDoc = objWord.Documents.Open(processFile, , , True, , , , ,, , , , ,, ,)
objWord.Visible = True
And it opens the word document as a separate process.
So I need to create separate process for each file/presentation in MS PowerPoint
Can you help in resolving this issue ?
Here are some more information about on which environment I'm working :
Operating System : Microsoft Windows 10 Pro
Code Editor : Visual Studio 2019
Technology : Vb.net(.Net Framework 4.8)
MS Office Version : 2019(32 bit)
Word and Excel both allow multiple instances of themselves to be created.
PowerPoint does not (other than as the result of an obscure bug).
I would like to share the impact of trying multiple applications of PowerPoint. As per the Microsoft paragraph below it will be sharing the process so painting of Menu bar/Standard bar/Add-ins tabs must be sharing objects so you cannot perform operation independently. Performed operation will going to impact other PPT screen.
Refer Link: About PowerPoint Instance
Multiple instances of Word (Winword.exe), Excel (Excel.exe), and Microsoft Access (MSAccess.exe) can run simultaneously. Therefore, these servers are defined as Single Use (Multiple Instances) servers. Only one instance of PowerPoint (Powerpnt.exe) can run at any given time. Therefore, PowerPoint is a Multiuse (Single Instance) server.
Also refer screenshot:

vb6 installation on Win 8.1

For several years I have been running a compiled VB6 program on an church office computer. I had the vb6 program installed on my home computer (Win 7) This program ran without problems. The program opened Excel spreadsheets and allowed the user to manipulate the data on the spreadsheet.
My Windows 7 computer crashed. I have a Windows 8.1 computer and have loaded vb6 on this computer. The installation appears to be successful except when I run the same visual basic project I get a Runtime Error '-2147319779 Automation Error Library not registered.' A sample of my code is shown below:
Dim oExcel As Excel.Application
Set oExcel = Excel.Application
oExcel.Workbooks.Open ("C:\FPCE Financial\FY-2014\2014-01 Financials.xls")
Application.Visible = True
The error occurs on the "Set oExcel = Excel.Application" line.
Any help would be appreciated.
If you haven't set a reference to Excel you need to use "Late Binding" to create the Excel object. As #Noodles says - if you don't have Office installed, it won't work period.
Try changing this
Dim oExcel As Excel.Application
Set oExcel = Excel.Application
To this
Dim oExcel As Object
Set oExcel = CreateObject("Excel.Application")

How to call and execute VBScript commands and functions in an .exe?

This question may be a little stupid, but i'm curious if it is possible or not.
I have a file named library.xxx(contains vbscript code), which contains predefined functions. And i have an other file test.vbs(also contains vbscript code, just here i use the functions, which are defined in library.xxx). In test.vbs the library is "included", which means, i can use the functions from library.xxx. For example, there is a function called ProgramFiles, and if i call it in test.vbs, i will receive the Program Files folder location.
The problem is, that library.xxx is visible in this way. There is an application called ScriptCryptor. With this application, i can open my library.xxx and make an .exe of it, which would be better for me, since it is not clear text.
My problem is now, how could i execute the command which are called in test.vbs? I think i should read line by line the test.vbs file, and process it somehow. But how? How do i know if the line i read is a function or just a variable? Or both? And how to process them?
Is there some way to do that?
Hopefully it is understandable what i want.
Thanks!
By far the easiest way to accomplish this is to include "library.vbs" in to your "test.vbs" file.
For example:
Library.vbs:
Function ProgramFiles()
ProgramFiles = "C:\Foo"
End Function
test.vbs:
sub includeFile (fSpec)
dim fileSys, file, fileData
set fileSys = createObject ("Scripting.FileSystemObject")
set file = fileSys.openTextFile (fSpec)
fileData = file.readAll ()
file.close
executeGlobal fileData
set file = nothing
set fileSys = nothing
end sub
includeFile "library3.vbs"
wscript.echo ProgramFiles
Your question seems to indicate that you may already be doing this so if you are then I apologize.
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
If clear text truly is bothering you then from what I have seen there is no way to make the executable from ScryptCryptor to be made available to your vbscript.
Instead you could create a COM Library DLL to be used as an object in your test.vbs file.
The downside to this is that it will be necessary to learn a new language. Visual Studio Visual Basic certainly is different from Windows Shell Script Visual Basic but it would work for what you want.
Steps to take:
Download Visual Studio 2013 Express for Windows Desktop (or trial version of Ultimate or whatever you feel is appropriate for you)
Open Visual Studio as an Administrator
Create a new Project. Select a "Class Library" under the "Visual Basic" templates
Copy and paste the code below
<ComClass(OurLibrary.ClassId, OurLibrary.InterfaceId, OurLibrary.EventsId)>
Public Class OurLibrary
Private userNameValue As String
Public Const ClassId As String = "40491A82-D53A-46A6-B7E0-1CDF78A33AB6"
Public Const InterfaceId As String = "B49C996C-B039-471D-BF17-0DDA5B3CF517"
Public Const EventsId As String = "6245E3DD-DEB5-4B75-AC03-F4430BC18FDE"
Public Sub New()
MyBase.New()
End Sub
Public Sub mycopy(mySource As String, myDest As String)
My.Computer.FileSystem.CopyFile(mySource, myDest, True)
End Sub
End Class
Click on Project -> ClassLibrary1 Properties
Click on "Compile" and check the box for "Register for COM interop"
Click on Build -> Build Solution
You now have a library that your test.vbs can use:
Set myLib = CreateObject("ClassLibrary1.OurLibrary")
mySource = "C:\mytextfile1.txt"
myDest = "C:\mytextfile2.txt"
myLib.mycopy mySource, myDest
If your like me test.vbs needed to be called as C:\Windows\SysWOW64\cscript.exe test.vbs
For more information about creating COM classes in Visual Basic see here: Walkthrough: Creating COM Objects with Visual Basic

Presentations.Open requires PowerPoint 2007 to be open?

I have a macro runTest in a PowerPoint file test.pptm that I wish to call by using the following VB script. All works well when the script is called on a machine with Office 2010, but the script will only work with Office 2007 providing the PowerPoint application has been opened before calling the script. I'd be interested to hear whether anyone has experienced a similar problem, or to hear of any potential solutions.
Option Explicit
On Error Resume Next
RunProcess
Sub RunProcess()
Dim pptApp
Dim pptPresentation
Set pptApp = CreateObject("PowerPoint.Application")
Set pptPresentation = pptApp.Presentations.Open("C:\test.pptm", True)
pptApp.Run "test.pptm!runTest"
pptApp.Quit
Set pptPresentation = Nothing
Set pptApp = Nothing
End Sub
I resolved the problem by adding
pptApp.Visible = True
after creating pptApp and before opening the presentation.
In the event that it needs Powerpoint launched, I would just trigger the launch as part of the vbscript
Set WshShell = wscript.CreateObject("wscript.Shell")
WshShell.Run "C:\Program Files (x86)\Microsoft Office\Office12\POWERPNT.EXE"
You could build in some error handling to see which version is installed, and call the appropriate exe file

vb6 can't open a connection obj that used to work perfectly

This code is in an ActiveX .dll. For a year this connection worked perfectly, now it doesn't. Here's the code:
Dim objConnection As ADODB.Connection
Dim objRs As ADODB.Recordset
Dim theConnectionStr As String
Dim SanMarDB As Databas
Dim SanMarRs As Recordse
.
.
.
Set objConnection = New ADODB.Connection
theConnectionStr = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & gProgramFilesPath & ";" & _
"Extended Properties='text;HDR=YES;FMT=Delimited;IMEX=1'"
objConnection.Open theConnectionStr
Here is my list of references in the .dll:
Visual Basic for Applications
Visual Basic runtime objects and procedures
Visual Basic objects and procedures
Microsoft Scripting Runtime
Microsoft Excel 14.0 Object Library
Microsoft DAO 3.6 Object Library
Microsoft ActiveX Data Objects 6.1 Library
Microsoft Data Formatting Object Library 6.0 (SP6)
Microsoft Shell Controls and Automation
I get one of two error messages. Here's the first
Invalid setting in Excel key of the Engines section of the Windows Registry.
Google pointed me to some reasons for this error message and I've checked the registry setting and it appears correct, CSVDelimited, no 5-char entries, etc.
The second error message is:
Object variable or With block variable not set
The thing that makes this particularly strange is that I have another dll that has exactly the same references and code and it works perfectly. (only some filenames are different).
Any thoughts? Thanks for helping me out with this.
-t
Something to at least try:
theConnectionStr = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source='" & gProgramFilesPath & "';" & _
"Extended Properties='text;HDR=YES;FMT=Delimited;IMEX=1'"
I.e. use quotes (apostrophes above) around the value for Data Source in case of characters that disrupt connection string parsing.
Also, isn't IMEX only used with the Excel IISAM and not the Text and HTML IISAMs?
Mixing ADO and DAO in one program is odd, but hopefully unrelated.

Resources