Solved

How to apply Warning off message to my code

Posted on 2010-11-16
6
325 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
  • 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

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

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

Suggested Solutions

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

809 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