Unable to get open property of workbook class VBS - vbscript

I'm trying to make a script that will remove the password protection for a excel (.xls) file but i keep getting the following error when I try to open my workbook:
"Unable to get the Open property of the Workbook class"
My code to open the file is:
sfPath = objArgs(0)
spassword = objArgs(1)
set objExcelFile = CreateObject("Excel.Application")
set objWorkbook = objExcelFile.Workbooks.Open(sfPath, spassword)
Any help would be much appreciated.

Did you read the docs.
You code is passing a string instead of a number specifing to update links or not. Password is the 5th parameter.
expression.Open(FileName, UpdateLinks, ReadOnly, Format, Password, WriteResPassword, IgnoreReadOnlyRecommended, Origin, Delimiter, Editable, Notify, Converter, AddToMru, Local, CorruptLoad, OpenConflictDocument)

Related

How does this work - How does the vbs script know it should open the file?

I needed a vbs script that opens a .xlsm file, runs a macro in it and closes the file again.
Online I found a script that works perfectly.
Trouble is, I don't understand how.
This is the script :
Sub MacroExcel()
Dim ExcelApp
Dim ExcelFile
Set ExcelApp = CreateObject("Excel.Application")
Set ExcelFile = ExcelApp.Workbooks.Open("D:\File1.xlsm")
ExcelApp.Visible = True
ExcelApp.Run "Macro1"
ExcelApp.Quit
Set ExcelFile = Nothing
Set ExcelApp = Nothing
End Sub
How does this open the file in the first place? As I understand, this line :
Set ExcelFile = ExcelApp.Workbooks.Open("D:\File1.xlsm")
assigns a method to the variable ExcelFile. But then, ExcelFile is not used in the code anymore.
How does the script know that the file should actually be opened ?
As I understand, this line :
Set ExcelFile = ExcelApp.Workbooks.Open("D:\File1.xlsm")
assigns a
method to the variable ExcelFile
That is where you understand it wrong. It doesn't assign a method to the variable ExcelFile, it assigns the result of executing the method ExcelApp.Workbooks.Open("D:\File1.xlsm")
You see, when you ask Excel to open a file, it not only opens the actual file in Excel, it also returns an object of type Workbook
It's this workbook object that is stored in the variable ExcelFile
Now you are entirely correct that in this snippet the actual workbook object is not used. So instead of assigning the result to a variable, they could have also executed the method and ignored the resulting object like this.
ExcelApp.Workbooks.Open "D:\File1.xlsm"

VBScript: Download JSON File From Webpage and Read Contents to Variable

I'm trying to write a VB script that will log into a secure website and download a series of reports.
The following gets me into the website, but after login the whole site is written in javascript.
Dim oIE
Set oIE = CreateObject("InternetExplorer.Application")
With oIE
.Visible = True
.Navigate SecureWebsite
Do While .Busy Or Not .readyState = 4: WScript.Sleep 100: Loop
Do Until .document.readyState = "complete": WScript.Sleep 100: Loop
Do While TypeName(.document.getElementById("username")) = "Null": WScript.Sleep 100: Loop
End With
Set Helem = oIE.document.getElementByID("username")
Helem.Value = "myusername"
Set Helem = oIE.document.getElementByID("password")
Helem.Value = "mypassword"
Call oIE.Document.all.loginForm.submit
I've found a link that I can use with parameters to search for the reports I need. When I follow the link, Internet Explorer returns a JSON file that I can open/download. The JSON file contains a Report ID that I can use as a parameter in another link to download the file that I need.
Is there any way using the InternetExplorer object to read the text contents of the JSON file into a variable so that I can parse the Report ID out of it? All the examples I've found use the MSXML2.XMLHTTP object, but that disconnects it from the sign-on I've achieved in the InternetExplorer object.
I ended up doing this in C#. The website had redirects and SSO so I couldn't get a direct WebClient Get/Post, but I compromised by logging in using a WebBrowser object and then passing the cookies to a HttpWebRequest object, per this excellent guide:
https://www.codeproject.com/Tips/659004/Download-of-file-with-open-save-dialog-box

opend ms word, past the data from access and save in vb6

I want to open new ms word file and copy the data from access (DAO connection) and past the same in document and save in particular path when I click the command button. Please confirm if it is possible.
You can use Word object to do this. Please refer the code below.
Dim oWord As Object
Dim oDoc As Object
Set oWord = CreateObject("word.application")
Set oDoc = oWord.Documents.Add
oWord.Selection.Paste
oWord.Visible = True
Refer this link for more details

"Method or Data Member Not Found" When Trying to Execute Database Code

I have a test project I'm using to familiarise myself with VB6. Just a listbox, a button to get info, and a button to clear info:
Code:
Option Explicit
Private Sub btnGet_Click()
lstResults.DataSource = GetMenuItems
End Sub
Private Sub btnClear_Click()
lstResults.Clear
End Sub
Public Function GetMenuItems() As ADODB.Recordset
Dim rs As ADODB.Recordset
Dim conn As New ADODB.Connection
conn.ConnectionString = "File Name=C:\connString.udl"
Dim cmd As New ADODB.Command
Set cmd.ActiveConnection = conn
cmd.CommandType = adCmdStoredProc
cmd.CommandText = "dbo.GetMenuItems"
Set rs = cmd.Execute()
GetMenuItems = rs
End Function
The following error appears when I click the Get Items button (btnGet):
Compile Error: Method or Data Member Not Found
At first I thought it might be something to do with the event/button, that some sort of binding between them wasn't present. But just putting in something like "MsgBox("Hello")" works fine. Yet it doesn't even seems to get to the line where the GetMenuItems function is called before throwing the error.
This being my first whirl with VB, I'm a little stumped.
EDIT - I've had a look at the UDL file I was using too. Tested that and its connecting ok on its own.
In Sub btnGet_Click, use
Set lstResults.DataSource = GetMenuItems
Assigning object references without using Set is hardly ever the right thing to do. For what it's worth, omitting Set references the left-hand side's default property; this was part of VB6 (OK, VB4, when classes were introduced) as a help to VB3 programmers, before there were such things as objects. Whatever kind of object lstResults.DataSource returns likely does not have a default property, leading to the "Method or data member not found" error.
You've got a private sub btnGet_Click() calling a public function GetMenuItems(), which may cause problems.
Also I'm not sure you can use a udl as the connection string. Instead, open the UDL (you may need to change the file extension to .txt temporarily), take the connection string out, and use that in place of the file name.
Also, check the stored procedure exists dbo.GetMenuItems

ms access linked image relitive path

I have an Image object.
I have the Picture type set to linked, so I can change the picture if I want.
I have the Picture property set to the picture name.
I would think that access would use relitive addressing and simple looking in the current directory for the image. But it does not and I get an error telling me it cannot find the picture.
Anyone have a solution? (Other than setting the Picture type to embedded or using the full file address?)
Thanks!
Update:
Tried this:
Private Sub Form_Load()
Dim file As String
file = CurrentDb().Name
file = Replace(file, ".mdb", ".bmp")
Me.Image46.Picture = file
End Sub
It works, except I still get the error message. I click O.K. and it works. Just need the error message to go away.
SOLUTION: Use the above code (or the code posted in the answer below) and then set the 'picture type' to "embedded" and then delete the 'picture' field so that it says "(none)".
Save and run.
It should work.
THANKS!
You could set the property on the forms OnLoad event like this
Me.imgMy_image.picture=getDBPath & “mypicture.bmp”
Here is the getDBPath function
Public Function GetDBPath() As String
Dim strFullPath As String
Dim I As Integer
strFullPath = CurrentDb().Name
For I = Len(strFullPath) To 1 Step -1
If Mid(strFullPath, I, 1) = "\" Then
GetDBPath = Left(strFullPath, I)
Exit For
End If
Next
End Function
Before anyone comments yes I know in access 2000 and above you can use currentproject.path but I’m stuck in the land that time forgot so need that custom function, it still works with later versions of access
Current folder depends of the way you open database in Access. At least, if you open it thru "File-Open", current folder changes to the folder of MDB file. But if you open via double-clicking MDB in explorer, it does not.

Resources