Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Expand Excel columns using VBscript

Posted on 2011-03-09
1
Medium Priority
?
716 Views
Last Modified: 2012-05-11
Running Excel 2000 on a pc running Windows XP pro.   I want to expand the columns.  My code is below.  The command:

objExcel.Range("A3:H3")Columns.AutoFit

Does not work.  What is the correct syntax to expand the columns so the data will fit?

Thank you.
' ==============  DiskDriveFileList.vbs ========================
'   Purpose:  List files and folders from a disk drive to an Excel spreadsheet
' =========================================================================

Option Explicit

Dim objExcel   'Reference to Excel

Dim strSheetName
Dim strFolderName

Set objExcel = WScript.CreateObject("Excel.Application") 'Instantiate Excel

'Main Function

  strSheetName	= "\\sbs2003server\E-drive\TVDeleteFiles\Test.xls"
  strFolderName	= "\\sbs2003server\E-drive\TVDeleteFiles\"
  
  fnCreateNewSheet()
  
  fnWriteExcelData(strFolderName) 
  
  fnSaveExcelSheet(strSheetName)
  
  fnCloseSheetAndExcel()
  
  fnTerminateScript()
  
  Function fnCreateNewSheet()
    
    ' == Workbooks is a collection.  Add() is a method belonging to Workbooks collection
    ' == Open new worksheet
    objExcel.Workbooks.Add
    
  End Function

  Function fnWriteExcelData(strFldName)
    With objExcel.Range("A1")
         .Formula	= "Folder Contents"
         .Font.Bold	= True
         .Font.Size	= 12
    End With

    objExcel.Range("A3").Formula		= "File Name:"
    objExcel.Range("B3").Formula		= "File Size:"
    objExcel.Range("C3").Formula		= "File Type:"
    objExcel.Range("D3").Formula		= "Date Created:"
    objExcel.Range("E3").Formula		= "Date Last Accessed"
    objExcel.Range("F3").Formula		= "Date Last Modified"
    objExcel.Range("G3").Formula		= "Attributes"
    objExcel.Range("H3").Formula		= "Short File Name"
    objExcel.Range("A3:H3").Font.Bold		= True
    objExcel.Range("A3:H3")Columns.AutoFit
    
    subListFilesInFolder strFldName, True
    
  End Function

  ' == List files in folders and sub folders
  Sub subListFilesInFolder(strSourceFldrName, bolSubFolders)
    
  End Sub

  Function fnSaveExcelSheet(strSheetNm)
    ' == Use the ActiveWorkBook property to reference the current workbook.
    ' == Use the WorkBook object SaveAs function
    
    objExcel.ActiveWorkBook.SaveAs(strSheetNm)
    
  End Function

  Function fnCloseSheetAndExcel()
    ' == Use the Workbook object Close() Method
    objExcel.ActiveWorkbook.Close()
    objExcel.Quit()
    
  End Function

  Function fnTerminateScript()
    WScript.Quit()    'Terminate script execution
    
  End Function

Open in new window

0
Comment
Question by:donpick
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 6

Accepted Solution

by:
TinTombStone earned 2000 total points
ID: 35093466
Try

objExcel.Columns("A:H").AutoFit
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It is a general practice to get rid of old user profiles on a computer  in a LAN environment. As I have been working with a company in a LAN environment where users move from one place to some other place at times. This will make many user profil…
The Windows functions GetTickCount and timeGetTime retrieve the number of milliseconds since the system was started. However, the value is stored in a DWORD, which means that it wraps around to zero every 49.7 days. This article shows how to solve t…
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Suggested Courses

610 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question