Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Visual Basic > VB 6 Visual Basic 6 > VB How-To
|
VB How-To Ask your "How do I do this with VB?" questions in this forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the VB How-To 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 19th, 2007, 03:59 PM
Registered User
 
Join Date: Jul 2007
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default Disabling second Excel Save prompt

I've got a VB.NET 2005 application that exports data to an Excel worksheet. The problem I'm having is that if the file the user wants to save to already exists I get two confirmations to overwrite prompts. One of them occurs in the file dialog box after they select their file name. The other occurs when I do a SaveAs to save the file. Also, if I click on "No" on the second prompt a System.Runtime.InteropServices.COMException error occurs.

I start off the subroutine thusly:

Dim file As New SaveFileDialog
  With file
    'Set the default extension to xls
    .DefaultExt = "xls"
    'Prompt them if the file exists and ask if its okay to overwrite
    .OverwritePrompt = True
    'Set the initial directory to the user's My Documents folder
    .InitialDirectory = My.Computer.FileSystem.SpecialDirectories.MyDocume nts
    'In the file list, display only xls files, but allow them to change to view all files
    .Filter = "Excel Workbook files (*.xls)|*.xls|All files (*.*)|*.*"
    'Set the filter index to 1, indicating the xls files
    .FilterIndex = 1
  End With
Dim filePath As String = ""

'If the user clicked "Save" (OK), then continue with the export
If file.ShowDialog = System.Windows.Forms.DialogResult.OK Then
  Try
     My.Application.DoEvents() 'makes the dialog box go away
     filePath = file.FileName 'gets the filename

     'Create the Excel worksheet
     Dim app As Application
     app = New Application()
     Dim workbooks As Workbooks
     workbooks = app.Workbooks
     Dim workbook As _Workbook
     workbook = workbooks.Add(XlWBATemplate.xlWBATWorksheet)
     Dim sheets As Sheets
     sheets = workbook.Worksheets
     Dim worksheet As _Worksheet
     worksheet = sheets.Item(1)


Next I retrieve the data from the mainframe and populate the Excel cells. At the end of this process I save the file with:

workbook.SaveAs(filePath)
workbook.Close()

Is there a way I can disable the prompting on the workbook.SaveAs(filepath) line?

Thanks for any help.

John


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

Just turn of the Alerts. It should solve the problem

Application.DisplayAlerts = False

workbook.SaveAs(filePath)

Application.DisplayAlerts = True

Cheers
Shasur

http://www.dotnetdud.blogspot.com

VBA Tips & Tricks (http://www.vbadud.blogspot.com)
 
Old November 20th, 2007, 09:44 AM
Registered User
 
Join Date: Jul 2007
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I tried Application.DisplayAlerts = False but got a syntax error of "Reference to a non-shared member requires an object reference."


 
Old November 20th, 2007, 09:51 AM
Registered User
 
Join Date: Jul 2007
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I discovered that Application.DisplayAlerts is part of the Excel PIA and that if I used my application variable app it would work:

app.DisplayAlerts = False

Thanks, Shasur!







Similar Threads
Thread Thread Starter Forum Replies Last Post
how to save as an excel sheet azizsaad19 Beginning VB 6 2 May 25th, 2007 09:48 AM
Prompt to Save arholly Access 13 December 18th, 2006 06:32 PM
Save Pictures outside Excel jud_ltd Excel VBA 1 May 7th, 2006 08:26 AM
Excel Save Prompt in javascript interrupt Javascript How-To 2 March 18th, 2005 04:52 AM





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