Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Microsoft Office > Excel VBA > Excel VBA
|
Excel VBA Discuss using VBA for Excel programming.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Excel VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 8th, 2010, 01:29 PM
Registered User
 
Join Date: Mar 2010
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default updating an excel file with vb script

Hi there,

I would appreciate if you could see what I am doing wrong in the following vb script:

I am running the following from the command line as "Start script.vbs". This script suppose to adjust a column width and add a freezepanes and save the changes. Currently it is asking a dialoge box and to see if I would like to save this file. The file also stays open and only opens in read only view.

' script.vbs
Dim objXL
Set objXL = CreateObject("Excel.Application")
With objXL
.Workbooks.Open ("D:\files\2.xls")
.Application.Columns("B").Select
.Application.ActiveWindow.FreezePanes = True
.Application.Columns("A:AE").Select
.Application.Columns("A:AE").EntireColumn.AutoFit
.Application.Rows("3:20").EntireRow.AutoFit
.Application.Quit
End With
Set objXL = Nothing

Much appreciate your help
 
Old March 8th, 2010, 10:06 PM
Friend of Wrox
 
Join Date: Sep 2005
Posts: 812
Thanks: 1
Thanked 53 Times in 49 Posts
Default

Hi

You need to write some code toe save the file

Code:
Dim objXL
Set objXL = CreateObject("Excel.Application")
With objXL
    	.Workbooks.Open ("D:\files\2.xls")
        .Application.Columns("B").Select
        .Application.ActiveWindow.FreezePanes = True       
        .Application.Columns("A:AE").Select
        .Application.Columns("A:AE").EntireColumn.AutoFit
        .Application.Rows("3:20").EntireRow.AutoFit
	.Application.Quit
.Workbooks("2.xls").Save
.Workbooks("2.xls").Close (False)
   End With

Set objXL = Nothing
Cheers
Shasur
__________________
C# Code Snippets (http://www.dotnetdud.blogspot.com)

VBA Tips & Tricks (http://www.vbadud.blogspot.com)
 
Old March 9th, 2010, 04:46 AM
Registered User
 
Join Date: Mar 2010
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Smile passing file name as a parameter

Thanks Shasur.

One more thing, I would like to call this code from a java programme e.g.
Code:
Runtime.getRuntime().exec("cmd /c start calling.vbs 'D:\Zia_ALU\HA\CR\mytest\final\2.xls' ")
Is there a way to pass the file name as a parameter to the above vb script?
 
Old March 9th, 2010, 05:48 AM
Registered User
 
Join Date: Mar 2010
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default passing file name as a parameter

Thanks for the response. The script is working but I need to make another adjustment so I pass the file name and file path as parameters to this script. Any idea how this can be done so If I call somthing like

"Start script.vbs pfilepath filepath"

Code:
'script.vbs
Dim objXL
Dim sfilname = pfilename
Dim sfilepath = pfilepath
Set objXL = CreateObject("Excel.Application")
With objXL
    	'.Workbooks.Open ("D:\2.xls")
    	.Workbooks.Open (sfilepath + sfilname)
    	'Adjust column width
    	.Application.Cells.Select
    	.Application.Cells.EntireColumn.AutoFit      	
        'Adjust row height
        .Application.Rows("3:500").EntireRow.AutoFit
        'FreezePane
        .Application.Range("B5").Select
        .Application.ActiveWindow.FreezePanes = True   
        'Auto filter
        .Application.Rows("4:4").Select
    	.Application.Selection.AutoFilter
    	.Application.Range("B5").Select
	.Workbooks(sfilname).Save
	.Workbooks(sfilname).Close (False)
	.Application.Quit
End With
Set objXL = Nothing





Similar Threads
Thread Thread Starter Forum Replies Last Post
Create excel file in vb monika.vasvani VB Databases Basics 0 July 17th, 2008 10:24 PM
VB Script with Excel mateenmohd VB How-To 0 February 19th, 2005 06:33 AM
vb script + excel - select multiple columns mohit Excel VBA 1 January 21st, 2005 06:11 AM
Updating an excel file via PHP Annoyamouse PHP How-To 0 December 23rd, 2004 07:55 PM
Can i run A VB Script File From Visual Basic VB GURU VBScript 1 May 21st, 2004 01:42 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.