使用Excel VBA函数时保存Excel文件时出现错误信息

时间:2018-01-04 14:53:06

标签: excel vba excel-vba

当我尝试保存以下代码时,会显示错误消息,但我无法保存。

下面是我的错误消息的图像文件链接。 我使用的是韩语Excel 2007,所以我不知道这条消息到底是什么,但我可以告诉你这条错误信息的含义。

(this error message means... couldn't find file.)

(this error message means.... &H8000FFFF system error occurs.)

(I tried Export UserForm File but there were same error messages.)

我尝试打开另一个Excel窗口并粘贴用户表单,将Userform导出为文件,保存为其他名称并保存到其他路径,但我一直收到相同的错误消息。

我在几天前将My windows7更新为最新版本。

以下是从Excel发送电子邮件的代码的一部分(所有代码无法上传。它太长并被视为垃圾邮件。)我上传了UserForm ScreenShots供您参考。

-----------我的部分代码和UserForm

My UserForm

Function NPP메일보내기함수()

    '//ThisWorkbook.SaveAs ThisWorkbook.Path & "\" & Date & " " & "Position Report Ver.2.xlsx", FileFormat:=51 '//xlsm은 52
    '//위 방법은 xlsx 저장은 잘 되나 아래와 같은 문제가 있다.
    '//I have a Excel sheet, and if I save the file using the Save as... option in Excel VBA the currently open document would close, and switch over to the newly created document.
    '//How can I save a copy of the document without switching over the control?
    '//해결하려면 여러가지 방법이 있다. 여기엔 하나만 적는다. 아래와 같이 하는건 잘못된 방법이다. SaveCopyAS는 확장자 못 바꿈.
    '//ThisWorkbook.SaveCopyAs ThisWorkbook.Path & "\" & Date & " " & "Position Report Ver.2 MEIN.xlsx" '//, FileFormat:=51
    '//위 방법은 새창으로 안 열리기는 하나 확장자가 안 바뀜.
    '//아래 방법이 새창으로 안 열리면서 확장자도 바뀌는 완벽한 방법임.

'
'    Dim wb As Workbook, pstr As String
'
'    pstr = ThisWorkbook.Path & "\" & Date & " Position Report Ver. 02 MEIN" & ".xlsm"
'    ActiveWorkbook.SaveCopyAs Filename:=y
'
'    Set wb = Workbooks.Open(pstr)
'    wb.SaveAs Left(pstr, Len(pstr) - 1) & "x", 52
'    wb.Close False
'
'    Kill pstr
'    오류뜸

    '//http://www.excely.com/excel-vba/save-workbook-as-new-file.shtml


    ThisWorkbook.Sheets.Copy
    Application.DisplayAlerts = False
    Dim 매크로파일경로 As String
    매크로파일경로 = ThisWorkbook.Path
    ActiveWorkbook.SaveAs 매크로파일경로 & "\눈레포트 첨부 엑셀파일\" & Format(Date, "yyyy-mm-dd") & " " & "Position Report Ver.3 MEIN.xlsx", FileFormat:=51
    ActiveWorkbook.Close


    On Error GoTo Error_Handler
    Dim oOutlook        As Object
    Dim sAPPPath        As String

    If IsAppRunning("Outlook.Application") = True Then    'Outlook was already running
        Set oOutlook = GetObject(, "Outlook.Application")    'Bind to existing instance of Outlook
    Else    'Could not get instance of Outlook, so create a new one
        sAPPPath = GetAppExePath("outlook.exe")    'determine outlook's installation path
        Shell (sAPPPath)    'start outlook
        Do While Not IsAppRunning("Outlook.Application")
            DoEvents
        Loop
        Set oOutlook = GetObject(, "Outlook.Application")    'Bind to existing instance of Outlook
    End If

    '    MsgBox "Outlook Should be running now, let's do something"
    Const olMailItem = 0
    Dim oOutlookMsg     As Object
    Set oOutlookMsg = oOutlook.CreateItem(olMailItem)    'Start a new e-mail message

    Dim 보낼메세지 As String
    Dim 반복문카운터 As Integer
    For 반복문카운터 = 95 To 127
        보낼메세지 = 보낼메세지 & ThisWorkbook.Worksheets("NPP").Range("C" & 반복문카운터).Value & Chr(13) & Chr(10)
    Next
    With oOutlookMsg
        .To = "해사운항팀"
        .CC = " 사업안전팀; 최종범차장; 조달팀; 공무팀; 사업팀; 박준영대리; 고현해운"
        .BCC = ""
        .Subject = Range("C99").Value
        '// .Body = Range("C95:C127").Value 요렇게 하면 안돼요.
        .Body = 보낼메세지
        '//Attachments를 Attachment라고 써서 에러가 나던 것.
        .Attachments.Add 매크로파일경로 & "\눈레포트 첨부 엑셀파일\" & Format(Date, "yyyy-mm-dd") & " " & "Position Report Ver.3 MEIN.xlsx"
        '//ThisWorkbook.Path하니까 파일이 없다는 오류가 나서 시도해봄.
        .Display    'Show the message to the user
    End With


Error_Handler_Exit:
    On Error Resume Next
    Set oOutlook = Nothing
    Exit Function

Error_Handler:
    MsgBox "The following error has occured" & vbCrLf & vbCrLf & _
           "Error Number: " & Err.Number & vbCrLf & _
           "Error Source: StartOutlook" & vbCrLf & _
           "Error Description: " & Err.Description _
           , vbOKOnly + vbCritical, "An Error has Occured!"
    Resume Error_Handler_Exit
End Function

'---------------------------------------------------------------------------------------
' Procedure : IsAppRunning
' Author    : Daniel Pineault, CARDA Consultants Inc.
' Website   : http://www.cardaconsultants.com
' Purpose   : Determine is an App is running or not
' Copyright : The following may be altered and reused as you wish so long as the
'             copyright notice is left unchanged (including Author, Website and
'             Copyright).  It may not be sold/resold or reposted on other sites (links
'             back to this site are allowed).
'
' Input Variables:
' ~~~~~~~~~~~~~~~~
' sApp      : GetObject Application to verify if it is running or not
'
' Usage:
' ~~~~~~
' IsAppRunning("Outlook.Application")
' IsAppRunning("Excel.Application")
' IsAppRunning("Word.Application")
'
' Revision History:
' Rev       Date(yyyy/mm/dd)        Description
' **************************************************************************************
' 1         2014-Oct-31                 Initial Release
'---------------------------------------------------------------------------------------
Function IsAppRunning(sApp As String) As Boolean
    On Error GoTo Error_Handler
    Dim oApp            As Object

    Set oApp = GetObject(, sApp)
    IsAppRunning = True

Error_Handler_Exit:
    On Error Resume Next
    Set oApp = Nothing
    Exit Function

Error_Handler:
    Resume Error_Handler_Exit
End Function

'---------------------------------------------------------------------------------------
' Procedure : GetAppExePath
' Author    : Daniel Pineault, CARDA Consultants Inc.
' Website   : http://www.cardaconsultants.com
' Purpose   : Determine the path for a given exe installed on the local computer
' Copyright : The following may be altered and reused as you wish so long as the
'             copyright notice is left unchanged (including Author, Website and
'             Copyright).  It may not be sold/resold or reposted on other sites (links
'             back to this site are allowed).
'
' Input Variables:
' ~~~~~~~~~~~~~~~~
' sEXEName  : Name of the exe to locate
'
' Usage:
' ~~~~~~
' Call GetAppExePath("msaccess.exe")
' GetAppExePath("firefox.exe")
' GetAppExePath("outlook.exe")
'
' Revision History:
' Rev       Date(yyyy/mm/dd)        Description
' **************************************************************************************
' 1         2014-Oct-31                 Initial Release
'---------------------------------------------------------------------------------------
Function GetAppExePath(ByVal sExeName As String) As String
    On Error GoTo Error_Handler
    Dim WSHShell        As Object

    Set WSHShell = CreateObject("Wscript.Shell")
    GetAppExePath = WSHShell.RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\" & sExeName & "\")

Error_Handler_Exit:
    On Error Resume Next
    Set WSHShell = Nothing
    Exit Function

Error_Handler:
    If Err.Number = -2147024894 Then
        'Cannot locate requested exe????
    Else
        MsgBox "The following error has occured." & vbCrLf & vbCrLf & _
               "Error Number: " & Err.Number & vbCrLf & _
               "Error Source: GetAppExePath" & vbCrLf & _
               "Error Description: " & Err.Description, _
               vbCritical, "An Error has Occured!"
    End If
    Resume Error_Handler_Exit
End Function

0 个答案:

没有答案