i am trying create a bar chart using the vb script.
my script is
Set objExcel = CreateObject("Excel.Application")
Set objReadWorkbook = objExcel.Workbooks.Open("D:\Excel_Macro_Proj\Create_Barchart.xlsx")
Set oExcelReadWorkSheet = objReadWorkbook.Worksheets(1)
objReadWorkbook.Sheets("Sheet1").Activate
objExcel.Visible = True
Sub GraphCreate ()
ActiveSheet.Shapes.AddChart2(201, xlColumnClustered).Select
ActiveSheet.Range("D11:D14,F11:F14,H11:H14")
End Sub
GraphCreate
objReadWorkbook.SaveAs("D:\Excel_Macro_Proj\barchart_create1.xls"),-4143
objExcel.Quit
when i run the above macro i am getting an error as,
"object required ActiveSheet". i am not getting why this error. any one please help me on this.
vbscript cant recognize ActiveSheet object directly, it available under Excel.Application object..in this case objExcel.ActiveSheet and objExcel.ActiveChart
Try this code
Set objExcel = CreateObject("Excel.Application")
Set objReadWorkbook = objExcel.Workbooks.Open("D:\Excel_Macro_Proj\Create_Barchart.xlsx")
Set oExcelReadWorkSheet = objReadWorkbook.Worksheets(1)
objReadWorkbook.Sheets("Sheet1").Activate
objExcel.Visible = True
Sub GraphCreate ()
objExcel.ActiveSheet.Shapes.AddChart2(201, xlColumnClustered).Select
set xlRange = objExcel.ActiveSheet.Range("D11:D14,F11:F14,H11:H14")
objExcel.ActiveChart.SetSourceData(xlRange)
End Sub
GraphCreate
objReadWorkbook.SaveAs("D:\Excel_Macro_Proj\barchart_create1.xls"),-4143
objExcel.Quit
Related
The vbscript below is intended to trigger a macro named "Test" in the file listed in the script. It follows examples I've seen on this site, but it does not run the macro. Probably a syntax issue. Maybe related to the desktop where the file is. Any help is appreciated.
On Error Resume Next
ExcelMacroExample
Sub ExcelMacroExample()
Dim xlApp
Dim xlBook
Set xlApp = CreateObject("Excel.Application")
Set xlBook = xlApp.Workbooks.Open("C:\Users\smithj01\Desktop\Alteryx Demo\'Command Line Test.xlsm'", 0, True)
xlApp.Run "Test"
xlApp.Quit
Set xlBook = Nothing
Set xlApp = Nothing
Set objFSO = Nothing
End Sub
It is my first vbs experience.
I try to keep my Problem short.
This one works, when I run it with my .bat:
Option Explicit
On Error Resume Next
ExcelMacroExample
Sub ExcelMacroExample()
Dim xlApp
Dim xlBook
Set xlApp = CreateObject("Excel.Application")
Set xlBook = xlApp.Workbooks.Open("C:\.....\RunScript.xlsm", 0,
True)
xlApp.Run "Auto_Open"
xlApp.Quit
Set xlBook = Nothing
Set xlApp = Nothing
End Sub
And this one works (shows me my corect current directory with my file):
Dim oFSO
Set oFSO = CreateObject("Scripting.FileSystemObject")
sScriptDir = oFSO.GetParentFolderName(WScript.ScriptFullName)
Wscript.Echo sScriptDir & "\RunScript.xlsm"
But if I combine them, it does not work:
Option Explicit
On Error Resume Next
ExcelMacroExample
Sub ExcelMacroExample()
Dim xlApp
Dim xlBook
Dim oFSO
Set oFSO = CreateObject("Scripting.FileSystemObject")
sScriptDir = oFSO.GetParentFolderName(WScript.ScriptFullName)
Set fileDirectory = sScriptDir & "\RunScript.xlsm"
Set xlApp = CreateObject("Excel.Application")
Set xlBook = xlApp.Workbooks.Open(fileDirectory, 0,
True)
xlApp.Run "Auto_Open"
xlApp.Quit
Set xlBook = Nothing
Set xlApp = Nothing
End Sub
Like I said, thank you Dave. That works.
However I found another issue, now with vbA inside the RunScript.xlsm
This code works with the vbS before:
Sub Auto_Open()
Application.DisplayAlerts = False
ActiveWorkbook.RefreshAll
ActiveWorkbook.Save
ActiveWorkbook.SaveAs Filename:= _
"C:\...\MyCSV.csv" _
, FileFormat:=xlCSV, CreateBackup:=False
Application.DisplayAlerts = True
ThisWorkbook.Saved = True
Application.Quit
End Sub
But if I change here the path, it just works when I run the RunScript.xlsm, but not when I run my vbS:
Sub Auto_Open()
Application.DisplayAlerts = False
ActiveWorkbook.RefreshAll
ActiveWorkbook.Save
Dim relativePath As String
relativePath = Application.ActiveWorkbook.path & "\MyCSV.csv"
ActiveWorkbook.SaveAs Filename:=relativePath, FileFormat:=xlCSV, CreateBackup:=False
Application.DisplayAlerts = True
ThisWorkbook.Saved = True
Application.Quit
End Sub
I think it is beacause of the ActiveWorkbook, I tried already ThisWorkbook, and I tried it without Application.
I am trying to open automatically an excel document on a Mac OS X, but it doesn't work. My code is:
Sub Button81_Click()
Dim objWord
Dim objDoc
Set objWord = CreateObject("Word.Application")
Set objDoc = objWord.Documents.Open("/Users/ricardo/Miniman/miniman_uti.docx")
objWord.Visible = True
End Sub
Will the path be wrong? For this path "/Users/ricardo/Miniman/miniman_uti.docx" it opens excel files. Why not word files?
Can someone please help me?
Does this work for you?
sub Test()
dim objdoc as object
with CreateObject("word.application")
set objdoc = .documents.open("path")
end with
end sub
Code to safely open a word doc from a file.
Handles the case where you already have word open.
Dim w As Object
' If word is already open get ahold of the running instance
' Otherwise create a new instance
On Error Resume Next
Set w = GetObject(, "Word.Application")
If w Is Nothing Then Set w = CreateObject("Word.Application")
On Error GoTo 0
' Close all open files and shutdown Word
' Loop through any open documents and close them
Do Until w.Documents.Count = 0
w.Documents(1).Close
Loop
w.Quit False
Set w = Nothing
' Now that all instances of word are closed, open the template
Dim wdApp As Object
Dim wdDoc As Object
Set wdApp = CreateObject("Word.application")
wdApp.Visible = True
wdApp.DisplayAlerts = False
Set wdDoc = wdApp.Documents.Open(Filename:="MYPATH")
I try to create object in UFT:
Dim xlApp
Dim xlBook
Dim xlSheet
Set xlApp = CreateObject("Excel.Application")
Set xlBook = xlApp.Workbooks.Open("C:\1.xls")
Set xlSheet = xlBook.Sheets(1)
Creating fails with Syntax error in
The test run cannot continue due to a syntax error.
Syntax error
Line (242): Set xlApp = CreateObject("Excel.Application")
Does anybody know how to repair it?
Thanks for the help
According to your comment, you are using somthing like:
Class MyClass
Dim xlApp
Dim xlBook
Dim xlSheet
Set xlApp = CreateObject("Excel.Application")
Set xlBook = xlApp.Workbooks.Open("C:\1.xls")
Set xlSheet = xlBook.Sheets(1)
End Class
Then, you get a syntax error in the xlApp assignment.
And right so, because the assignment is invalid in that scope (which is no callable scope at all).
First of all, set option explicit on.
Then, make sure you define all instance variables with Dim.
Also, create a constructor, or as in the following sample, a callable Sub, which initializes the instance variables, like this:
Option Explicit
Class MyClass
Dim xlApp
Dim xlBook
Dim xlSheet
Public Function SetParam ()
Set xlApp = CreateObject("Excel.Application")
Set xlBook = xlApp.Workbooks.Open("C:\1.xls")
Set xlSheet = xlBook.Sheets(1)
End Function
End Class
This would not generate a syntax error, and might be closer to what you intended.
This is because inside a Class...End Class construct, you cannot have anything else but definitions. No statements. And assignments are statements. (Initialization using "=" in a variable definition is not supported by VBScript.)
We want to upgrade our VB6 code to use Outlook 2010, but we're getting the following error:
Active x cannot create object
This is our current code:
Public Sub SendEmail()
Set emailOutlookApp = CreateObject("Outlook.Application.12")
Set emailNameSpace = emailOutlookApp.GetNamespace("MAPI")
Set emailFolder = emailNameSpace.GetDefaultFolder(olFolderInbox)
Set emailItem = emailOutlookApp.CreateItem(olMailItem)
Set EmailRecipient = emailItem.Recipients
EmailRecipient.Add (EmailAddress)
EmailRecipient.Add (EmailAddress2)
emailItem.Importance = olImportanceHigh
emailItem.Subject = "My Subject"
emailItem.Body = "The Body"
'-----Send the Email-----'
emailItem.Save
emailItem.Send
'-----Clear out the memory space held by variables-----'
Set emailNameSpace = Nothing
Set emailFolder = Nothing
Set emailItem = Nothing
Set emailOutlookApp = Nothing
Exit Sub
I'm not sure if "Outlook.Application.12" is correct. But I can't find a definitive answer for this.
For Outlook 2010, this is definitly corect Outlook.Application.14.
But, I don't know what about office 2007.
I think it's Outlook.Application.12 and for lower versions it is simply "Outlook.Application".
Here's the code I switched to for 2010:
Private Sub EmailBlahbutton_Click()
Dim mOutlookApp As Object
Dim OutMail As Object
Dim Intro As String
On Error GoTo ErrorHandler
Set mOutlookApp = GetObject("", "Outlook.application")
Set OutMail = mOutlookApp.CreateItem(0)
With Application
.EnableEvents = False
.ScreenUpdating = False
End With
'These are the ranges being emailed.
ActiveSheet.Range(blahblahblah).Select
'Intro is the first line of the email
Intro = "BLAHBLAHBLHA"
'Set the To and Subject lines. Send the message.
With OutMail
.To = "blahblah#blah.com"
.Subject = "More BLAH here"
.HTMLBody = Intro & RangetoHTML(Selection)
.Send
End With
With Application
.EnableEvents = True
.ScreenUpdating = True
End With
ActiveSheet.Range("A1").Select
ActiveWindow.ScrollColumn = ActiveCell.Column
ActiveWindow.ScrollRow = ActiveCell.Row
Set OutMail = Nothing
Set mOutlookApp = Nothing
Exit Sub
ErrorHandler:
Set mOutlookApp = CreateObject("Outlook.application")
Resume Next
End Sub
Function RangetoHTML(rng As Range)
' Changed by Ron de Bruin 28-Oct-2006
' Working in Office 2000-2010
Dim fso As Object
Dim ts As Object
Dim TempFile As String
Dim TempWB As Workbook
TempFile = Environ$("temp") & "/" & Format(Now, "dd-mm-yy h-mm-ss") & ".htm"
'Copy the range and create a new workbook to past the data in
rng.Copy
Set TempWB = Workbooks.Add(1)
With TempWB.Sheets(1)
.Cells(1).PasteSpecial Paste:=8
.Cells(1).PasteSpecial xlPasteValues, , False, False
.Cells(1).PasteSpecial xlPasteFormats, , False, False
.Cells(1).Select
Application.CutCopyMode = False
On Error Resume Next
.DrawingObjects.Visible = True
.DrawingObjects.Delete
On Error GoTo 0
End With
'Publish the sheet to a htm file
With TempWB.PublishObjects.Add( _
SourceType:=xlSourceRange, _
Filename:=TempFile, _
Sheet:=TempWB.Sheets(1).Name, _
Source:=TempWB.Sheets(1).UsedRange.address, _
HtmlType:=xlHtmlStatic)
.Publish (True)
End With
'Read all data from the htm file into RangetoHTML
Set fso = CreateObject("Scripting.FileSystemObject")
Set ts = fso.GetFile(TempFile).OpenAsTextStream(1, -2)
RangetoHTML = ts.ReadAll
ts.Close
RangetoHTML = Replace(RangetoHTML, "align=center x:publishsource=", _
"align=left x:publishsource=")
'Close TempWB
TempWB.Close savechanges:=False
'Delete the htm file we used in this function
Kill TempFile
Set ts = Nothing
Set fso = Nothing
Set TempWB = Nothing
End Function
Why do you explicitly specify the version? Why not simply
Set emailOutlookApp = CreateObject("Outlook.Application")
Try "Outlook.Application.14". Not sure if this is related though: 2007 to 2010 upgrade issue
I realize it's not the exact issue, but it may lead you down the right path.