Link to home
Start Free TrialLog in
Avatar of Mark01
Mark01Flag for United States of America

asked on

Access VBA Object variable Error

I am trying to run the code shown below in Access, but I get an “Object variable or With block not set” error. Please help me resolve the error.

Code:
Dim myApp As Object
myApp = CreateObject("InDesign.Application.CS6")

Dim myFileSystemObject As Object
myFileSystemObject = CreateObject("Scripting.FileSystemObject")

Dim myFile As String
myFile = myFileSystemObject.GetFile("C:\Temp\1.jsx")

I also tried, but I get the same error:
Dim myFile As Long

The following code runs without any errors in VB.Net:
Dim myApp As Object
        myApp = CreateObject("InDesign.Application.CS6")

       Dim myFileSystemObject = CreateObject("Scripting.FileSystemObject")

        Dim myFile = myFileSystemObject.GetFile("C:\Temp\1.jsx")
        myApp.DoScript(myFile, InDesign.idScriptLanguage.idJavascript)
        myApp = Nothing
Avatar of John Tsioumpris
John Tsioumpris
Flag of Greece image

Does this line works ?
myApp = CreateObject("InDesign.Application.CS6")

Open in new window

or you should change it to
myApp = CreateObject("InDesign.Application")

Open in new window

This:

Dim myFile As String
myFile = myFileSystemObject.GetFile("C:\Temp\1.jsx")

is incorrect.   GetFile returns a file object, not a string.

Jim.
SOLUTION
Avatar of Jim Dettman (EE MVE)
Jim Dettman (EE MVE)
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Probably the error is before
Dim myApp As Object
set myApp = CreateObject("InDesign.Application.CS6")

Dim myFileSystemObject As Object
set myFileSystemObject = CreateObject("Scripting.FileSystemObject")

'Dim myFile As String ' This will get the path of the file
Dim myFile As Object
set myFile = myFileSystemObject.GetFile("C:\Temp\1.jsx")

Open in new window

Avatar of Mark01

ASKER

The last line is highlighted in red (syntax error) with the following:

Dim myApp As Object
myApp = CreateObject("InDesign.Application.CS6")
Dim myFileSystemObject As Object
myFileSystemObject = CreateObject("Scripting.FileSystemObject")
myApp.DoScript(myFile, InDesign.idScriptLanguage.idJavascript)

and

Dim myApp As Object
myApp = CreateObject("InDesign.Application")
Dim myFileSystemObject As Object
myFileSystemObject = CreateObject("Scripting.FileSystemObject")
myApp.DoScript(myFile, InDesign.idScriptLanguage.idJavascript)
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Mark01

ASKER

I get a runtime error “‘Object doesn’t support this property or method” with the following:

Dim myApp As Object
set myApp = CreateObject("InDesign.Application.CS6")

Dim myFileSystemObject As Object
set myFileSystemObject = CreateObject("Scripting.FileSystemObject")

'Dim myFile As String ' This will get the path of the file
Dim myFile As Object
set myFile = myFileSystemObject.GetFile("C:\Temp\1.jsx")
myFileSystemObject = CreateObject("Scripting.FileSystemObject")
You have already created last object
Avatar of Norie
Norie

Which line of code is causing the error?
Avatar of Mark01

ASKER

I get a syntax error on the last line (highlighted in red) with the following:

Dim myApp As Object
Dim myFileSystemObject As Object

    Set myApp = CreateObject("InDesign.Application")
    Set myFileSystemObject = CreateObject("Scripting.FileSystemObject")

    myApp.DoScript (myFile, InDesign.idScriptLanguage.idJavascript)
What is it that your trying to accomplish with this code?

Jim.
Avatar of Mark01

ASKER

@Jim Dettman: I am trying to run the 1.jsx script.
Avatar of Mark01

ASKER

With the following, there are no errors and InDesign starts, but the "1.jsx"script will not run.

Dim myApp As Object
Dim myFileSystemObject As Object
Set myApp = CreateObject("InDesign.Application.CS6")
Set myFileSystemObject = CreateObject("Scripting.FileSystemObject")
Dim myFile As Object
Set myFile = myFileSystemObject.GetFile("C:\Temp\1.jsx")
Set myApp = Nothing
Dim myApp As Object
Dim myFileSystemObject As Object
Dim myFile As Object
Dim myFilePath As String
Dim myFileText as String

' Start application
Set myApp = CreateObject("InDesign.Application.CS6")

' Need to get contents of script file.
Set myFileSystemObject = CreateObject("Scripting.FileSystemObject")

myFilePath = "C:\Temp\1.jsx"

Set myFile = myFileSystemObject.OpenTextFile(myFilePath, ForReading)
myFileText = myFile.ReadAll
myFile.Close
Set myFile = Nothing

 myApp.DoScript (myFileText , InDesign.idScriptLanguage.idJavascript)

Set myFileSystemObject  = nothing
Set myApp  = nothing

Should be close.   You want to read the file contents, then execute.   DoScript's first parameter is a string containing the script.

Jim.
Avatar of Mark01

ASKER

With the following, there are no errors and InDesign starts, but the "1.jsx"script will not run.

There is a syntax error on:
myApp.DoScript (myFile, InDesign.idScriptLanguage.idJavascript)

Code:
Dim myApp As Object
Dim myFileSystemObject As Object
Set myApp = CreateObject("InDesign.Application.CS6")
Set myFileSystemObject = CreateObject("Scripting.FileSystemObject")
Dim myFile As Object
Set myFile = myFileSystemObject.GetFile("C:\Temp\1.jsx")
myApp.DoScript (myFile, InDesign.idScriptLanguage.idJavascript)
Set myApp = Nothing
Avatar of Mark01

ASKER

My previous post erroneously stated that there are no errors and InDesign starts.

With the following there is a syntax  error on:
myApp.DoScript (myFile, InDesign.idScriptLanguage.idJavascript)

Code:
Dim myApp As Object
Dim myFileSystemObject As Object
Dim myFile As Object
Dim myFilePath As String
Dim myFileText As String

' Start application
Set myApp = CreateObject("InDesign.Application.CS6")

' Need to get contents of script file.
Set myFileSystemObject = CreateObject("Scripting.FileSystemObject")

myFilePath = "C:\Temp\1.jsx"

Set myFile = myFileSystemObject.OpenTextFile(myFilePath, ForReading)
myFileText = myFile.ReadAll
myFile.Close
Set myFile = Nothing

 myApp.DoScript (myFileText , InDesign.idScriptLanguage.idJavascript)

Set myFileSystemObject = Nothing
Set myApp = Nothing
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
<<I don't see the need for the file system object, the DoScript method is expecting a string as the first parm referencing the JSX file to run.>>

  Referencing the file, or the actual script as a string?   I thought it was the later.

Jim.
Jim, seems to want the file path, not content...


»bp
Avatar of Mark01

ASKER

Bill solved the problem. Thank you, Bill, John, Jim, and Norie.
Glad that helped.


»bp