Solved

How to apply Warning off message to my code

Posted on 2010-11-16
6
329 Views
Last Modified: 2012-06-27
I have a code below.  It works, but when I try to 1) check if csv file has appropriate column title and 2) if not, update the titles, and 3) save it into xls file, the warning message pops up, saying "A file named S:\db\invoice.xls exists.  Do you want to replace it?  I click "Yes" anyway, so how can I turn off the warning?  Thank you very much in advance.  -- hb

My code is below:

Private Sub ImportCSV_Click()

    Dim xl As Object, strFilePath As String
    strFilePath = "S:\Db\invoice.csv"
    Set xl = CreateObject("Excel.Application")
    ' make the excel application visible
    xl.Visible = True
    ' open the workbook
   
    xl.Workbooks.Open strFilePath
     If xl.Range("A1").Value = "InvoiceNo" Then
         'do nothing
         Else
    xl.Range("1:1").insert
    xl.ActiveSheet.Range("A1").Value = "InvoiceNo"
    xl.ActiveSheet.Range("B1").Value = "Amount"
    xl.ActiveSheet.Range("C1").Value = "DueDate"

    ' save and close the file
    If Val(xl.Version) < 12 Then
        xl.Workbooks(1).SaveAs "S:\Db\invoice.xls"
    Else
        ' to use XLSX format:
        'xl.Workbooks(1).SaveAs "S:\Db\invoice.xlsx", 51
        ' or to use XLSM format:
        'xl.Workbooks(1).SaveAs "S:\Db\invoice.xlsm", 52
        ' or to use XLSB format:
        'xl.Workbooks(1).SaveAs "S:\Db\invoice.xlsb", 50
        ' or to use good old XLS format:
        xl.Workbooks(1).SaveAs "S:\Db\invoice.xls", 56
    End If
   
    xl.Workbooks(1).Close SaveChanges:=True
    ' close Excel
   
    End If
    xl.Quit
    'clean up
    Set xl = Nothing    
             
    DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel9, "Invoice", "S:\Db\invoice.xls", True
     
    MsgBox "Completed"

End Sub
0
Comment
Question by:kg6lfz
[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
  • 3
  • 2
6 Comments
 
LVL 24

Expert Comment

by:broomee9
ID: 34148875
Re-posting as expert comment:

To turn off warnings in Excel, do this:
Application.DisplayAlerts = False

Turn it back on like this:
Application.DisplayAlerts = True

In Acces it would be this to turn them off
DoCmd.SetWarnings False

And this to turn them on:
DoCmd.SetWarnings True
0
 
LVL 24

Expert Comment

by:broomee9
ID: 34148894
And for your specific example, it would be this:

xl.DisplayAlerts = False

Since you define the application as xl in your code.
0
 

Author Comment

by:kg6lfz
ID: 34149781
Thank you, broomee9 -
I am sorry to ask, but based on my code, where should I put the "x1.DisplaiyAlerts = False" and "x1.DisplaiyAlerts = True".  Sorry for being novice.  Thank you.
0
 
LVL 24

Accepted Solution

by:
broomee9 earned 300 total points
ID: 34149971
No worries.

You want to put it at the beginning and then again at the end to turn it back on.
Private Sub ImportCSV_Click()

    Dim xl As Object, strFilePath As String
    
    strFilePath = "S:\Db\invoice.csv"
    Set xl = CreateObject("Excel.Application")
    
    'Turn off warnings
    xl.DisplayAlerts = False
    
    ' make the excel application visible
    xl.Visible = True
    ' open the workbook
   
    xl.Workbooks.Open strFilePath
     If xl.Range("A1").Value = "InvoiceNo" Then
         'do nothing
         Else
    xl.Range("1:1").insert
    xl.ActiveSheet.Range("A1").Value = "InvoiceNo"
    xl.ActiveSheet.Range("B1").Value = "Amount"
    xl.ActiveSheet.Range("C1").Value = "DueDate"

    ' save and close the file
    If Val(xl.Version) < 12 Then
        xl.Workbooks(1).SaveAs "S:\Db\invoice.xls"
    Else
        ' to use XLSX format:
        'xl.Workbooks(1).SaveAs "S:\Db\invoice.xlsx", 51
        ' or to use XLSM format:
        'xl.Workbooks(1).SaveAs "S:\Db\invoice.xlsm", 52
        ' or to use XLSB format:
        'xl.Workbooks(1).SaveAs "S:\Db\invoice.xlsb", 50
        ' or to use good old XLS format:
        xl.Workbooks(1).SaveAs "S:\Db\invoice.xls", 56
    End If
   
    xl.Workbooks(1).Close SaveChanges:=True
    ' close Excel
   
    End If
    
    xl.Quit
    
    'Turn on warnings
    xl.DisplayAlerts = False
    'clean up
    Set xl = Nothing    
             
    DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel9, "Invoice", "S:\Db\invoice.xls", True
     
    MsgBox "Completed"

End Sub

Open in new window

0
 

Author Comment

by:kg6lfz
ID: 34150466
It worked perfectly.  Thank you very much for your support.  
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

This article describes two methods for creating a combo box that can be used to add new items to the row source -- one for simple lookup tables, and one for a more complex row source where the new item needs data for several fields.
Access custom database properties are useful for storing miscellaneous bits of information in a format that persists through database closing and reopening.  This article shows how to create and use them.
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

687 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