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 November 17th, 2006, 11:07 PM
Registered User
 
Join Date: Nov 2006
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default Opening Modifying Printing excel File

I am trying to open an existing excel file (C:\Documents\dfdf32.xls) and edit one of the cells (H7) by entering today's date and print that sheet. This is my code:

Private Sub Command1_Click()

Dim oXL As Excel.Application
Dim oWB As Excel.Workbook
Dim oWS As Excel.Worksheet

Set oXL = New Excel.Application
Set oWB = oXL.Workbooks.Open("C:\Documents\dfdf32.xls")
Set oWS = oWB.Worksheets("Sheet1")

oWS.Range("H7").Value = "=TODAY()"

oWB.Save

oWB.Workbooks.Open("C:\Documents\dfdf32.xls").Prin tOut Copies:=1
oWB.Workbooks("C:\Documents\dfdf32.xls").Close

oXL.Quit

When I execute this, it says "a file named "dfdf32.xls" already exist . Do you want replace.."

Can anybody help me to correct my code?

Thank you
`Ali

 
Old November 17th, 2006, 11:13 PM
Friend of Wrox
 
Join Date: Sep 2005
Posts: 812
Thanks: 1
Thanked 53 Times in 49 Posts
Default

Replace

oWB.Workbooks.Open("C:\Documents\dfdf32.xls").Prin tOut Copies:=1

with

oWB.PrintOut Copies:=1

It should work

The message is displayed as you are trying to open a workbook that was already opened.

Cheers
Shasur

http://www.vbadud.blogspot.com
 
Old November 18th, 2006, 12:09 AM
Registered User
 
Join Date: Nov 2006
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thank you.

It worked but only once, and I got this message: run time error 438 , object doesn't support this property or method
and I pressed debug and it highlited this segment:
oWB.Workbooks("C:\Documents\dfdf32.xls").Close

and then I tried to execute it for the second time it said this file is a read only file you should rename the file and...





 
Old November 18th, 2006, 12:59 PM
Friend of Wrox
 
Join Date: Sep 2005
Posts: 812
Thanks: 1
Thanked 53 Times in 49 Posts
Default

Hi Ali

You have the Workbook linked to the object oWB, so you need not have the following

oWB.Workbooks("C:\Documents\dfdf32.xls").Close

oWB.close will do

Cheers
Shasur


http://www.vbadud.blogspot.com





Similar Threads
Thread Thread Starter Forum Replies Last Post
Opening an excel file with VBA TwEaKFrEaK Excel VBA 6 May 23rd, 2014 12:08 PM
Problem in opening excel file in MS Excel 2000 kallol Visual C++ 0 November 16th, 2007 05:48 AM
Opening an Excel file in OWC10.AxSpreadSheet ashu_from_india Visual Studio 2005 0 May 31st, 2007 06:32 AM
Opening Excel file p2pMember ASP.NET 1.0 and 1.1 Professional 1 January 21st, 2006 01:54 PM





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