VBS Script and ClearContents -- Code Throws An Error

G

garibaldi

Need VBS script to clear the contents for range all worksheets in workbook.
Must run as VBS script. Having issue with line

"currentWorkSheet .Range( cellRange ).ClearContents"; error generated

excelPath = "<Enter a UNC path to a workbook with 1+ worksheets>"
cellRange = "A1:Z100"

WScript.Echo "Reading Data from " & excelPath
WScript.Echo "cellRange = " & cellRange

Set oExcel = CreateObject("excel.application")
oExcel.Workbooks.open( excelPath )

workSheetCount = oExcel.Worksheets.Count

WScript.Echo "We have " & workSheetCount & " worksheets"

For counter = 1 to workSheetCount
WScript.Echo "Reading data from worksheet " & counter & vbCRLF

Set currentWorkSheet = oExcel.ActiveWorkbook.Worksheets( counter )

WScript.Echo "Call to Range method next" & vbCrLf

currentWorkSheet .Range( cellRange ).ClearContents ' <-- Fails

Set currentWorkSheet = Nothing
Next

oExcel.Workbooks(1).Close
oExcel.Quit

Set currentWorkSheet = Nothing
Set oExcel = Nothing
 
C

carlo

I changed it a little:
(Created Exc_WB, and there was a space between
currentWorksheet and .Range)
'-----------------------------------------------------------------------------
excelPath = "D:\test.xls"
cellRange = "A1:Z100"

WScript.Echo "Reading Data from " & excelPath
WScript.Echo "cellRange = " & cellRange

Set oExcel = CreateObject("excel.application")
Set Exc_Wb = oExcel.Workbooks.open( excelPath )

workSheetCount = Exc_Wb.worksheets.Count

WScript.Echo "We have " & workSheetCount & " worksheets"

For counter = 1 to workSheetCount
WScript.Echo "Reading data from worksheet " & counter &
vbCRLF

Set currentWorkSheet = Exc_Wb.Worksheets( counter )

WScript.Echo "Call to Range method next" & vbCrLf

currentWorkSheet.Range( cellRange ).ClearContents ' <-- Fails
Next

Exc_Wb.Close(True)
oExcel.Quit

WScript.Echo "finished"
'-----------------------------------------------------------------------------

test worked

hth

Carlo
 
B

Bob Phillips

This worked fine for me

excelPath = "<Enter a UNC path to a workbook with 1+ worksheets>"
cellRange = "A1:Z100"

WScript.Echo "Reading Data from " & excelPath
WScript.Echo "cellRange = " & cellRange

Set oExcel = CreateObject("excel.application")

Set oWB = oExcel.Workbooks.Open(excelPath)

workSheetCount = oWB.Worksheets.Count

WScript.Echo "We have " & workSheetCount & " worksheets"

For counter = 1 To workSheetCount
WScript.Echo "Reading data from worksheet " & counter & vbCrLf

Set currentWorkSheet = oWB.Worksheets(counter)

WScript.Echo "Call to Range method next" & vbCrLf

currentWorkSheet.Range(cellRange).ClearContents ' <-- Fails
Next

oWB.Save
oWB.Close
oExcel.Quit
Set currentWorkSheet = Nothing
Set oWB = Nothing
Set oExcel = Nothing


--
---
HTH

Bob

(there's no email, no snail mail, but somewhere should be gmail in my addy)
 
J

Jialiang Ge [MSFT]

Hello,

Carlo is right. The error is caused by the space between "currentWorkSheet"
and ".Range". Please remove the space and try it again. It also works well
on my side.

Please let us know if you have any other concerns, or need anything else.

Sincerely,
Jialiang Ge ([email protected], remove ¡®online.¡¯)
Microsoft Online Community Support

==================================================
For MSDN subscribers whose posts are left unanswered, please check this
document: http://blogs.msdn.com/msdnts/pages/postingAlias.aspx

Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications. If you are using Outlook Express/Windows Mail, please make sure
you clear the check box ¡°Tools/Options/Read: Get 300 headers at a time¡±
to see your reply promptly.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided ¡°AS IS¡± with no warranties, and confers no
rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top