Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How to create - save - write a specific excel file?

Posted on 2008-06-23
1
Medium Priority
?
368 Views
Last Modified: 2008-07-14
Hi experts,

I have a Sub that create an excel file and a Sub that add info in the file but it doesn't save the file and if I open a new file, the script takes that new file and write in the active file.

I would like my script to save the file in the same directory where the script strated, give the actual date and time as a name and write in that file even if I open or work in another excel file.

Can you help on this?

Thank you
Sub CreateExcelFile()
	objXL.Visible = True
	objXL.WorkBooks.Add
	'Set column width.
	objXL.Columns(1).ColumnWidth = 20
	objXL.Columns(2).ColumnWidth = 20
	objXL.Columns(3).ColumnWidth = 20
	objXL.Columns(4).ColumnWidth = 35
	
	
	''Set column headers
	objXL.Cells(1, 1).Value = "Computer Name"
	objXL.Cells(1, 2).Value = "Ping Status Code"
	objXL.Cells(1, 3).Value = "Logon Name"
	objXL.Cells(1, 4).Value = "Folder Exists"
	
	''Format text (bold)
	objXL.Range("A1:D1").Select
	objXL.Selection.Font.Bold = True
	objXL.Selection.Interior.ColorIndex = 1
	objXL.Selection.Interior.Pattern = 1 ''xlSolid
	objXL.Selection.Font.ColorIndex = 2
	''Left Align text
	objXL.Columns("B:B").Select
	objXL.Selection.HorizontalAlignment = &hFFFFEFDD '' xlLeft
	objXL.Cells(2, 1).Select
End Sub
 
Sub AddToExcelFile(strName, strCode, strReg, strFolder)
    objXL.Cells(intIndex, 1).Value = strName
    objXL.Cells(intIndex, 2).Value = strCode    
    objXL.Cells(intIndex, 3).Value = strReg
    objXL.Cells(intIndex, 4).Value = strFolder
    intIndex = intIndex + 1
    objXL.Cells(intIndex, 1).Select
End Sub

Open in new window

0
Comment
Question by:EricPelletier
[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 38

Accepted Solution

by:
Shift-3 earned 1500 total points
ID: 21846371
Here is a modified version of the first Sub which saves the spreadsheet with a name equal to the date and time.

I'm not sure how the two are supposed to work together, but you should be able to make similar changes to the second one.
Sub CreateExcelFile()
	strFile = Date & " " & Time & ".xls"
	strFile = Replace(strFile, "/", "-")
	strFile = Replace(strFile, ":", "-")
	
	Set objXL = CreateObject("Excel.Application")
	objXL.Visible = True
	Set objWorkbook = objXL.Workbooks.Add()
	Set objWorksheet = objWorkbook.Worksheets(1)
 
	'Set column width.
	objXL.Columns(1).ColumnWidth = 20
	objXL.Columns(2).ColumnWidth = 20
	objXL.Columns(3).ColumnWidth = 20
	objXL.Columns(4).ColumnWidth = 35
 
 
	''Set column headers
	objXL.Cells(1, 1).Value = "Computer Name"
	objXL.Cells(1, 2).Value = "Ping Status Code"
	objXL.Cells(1, 3).Value = "Logon Name"
	objXL.Cells(1, 4).Value = "Folder Exists"
 
	''Format text (bold)
	objXL.Range("A1:D1").Select
	objXL.Selection.Font.Bold = True
	objXL.Selection.Interior.ColorIndex = 1
	objXL.Selection.Interior.Pattern = 1 ''xlSolid
	objXL.Selection.Font.ColorIndex = 2
	''Left Align text
	objXL.Columns("B:B").Select
	objXL.Selection.HorizontalAlignment = &hFFFFEFDD '' xlLeft
	objXL.Cells(2, 1).Select
	objWorkbook.SaveAs(strFile)
End Sub

Open in new window

0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

Recently I finished a vbscript that I thought I'd share.  It uses a text file with a list of server names to loop through and get various status reports, then writes them all into an Excel file.  Originally it was put together for our Altiris server…
Welcome, welcome!  If you are new to the series and haven't been following along, please take a brief moment to review the first three installments: Part 1 (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/A_266-VBScri…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

721 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