programing

다른 이름으로 저장 대화 상자에서 선택한 파일을 저장하는 방법은 무엇입니까?

javajsp 2023. 8. 17. 20:46

다른 이름으로 저장 대화 상자에서 선택한 파일을 저장하는 방법은 무엇입니까?

온라인에서 다른 이름으로 저장 대화 상자를 여는 코드를 찾았습니다.

저장을 클릭해도 파일이 저장되지 않습니다.

Dim varResult As Variant
'displays the save file dialog
varResult = Application.GetSaveAsFilename(FileFilter:= _
  "Excel Files (*.xlsx), *.xlsx", Title:="Save PO", _
  InitialFileName:="\\showdog\service\Service_job_PO\")
'checks to make sure the user hasn't canceled the dialog
If varResult <> False Then
    Exit Sub
End If 

Excel에 워크북을 저장하라고 명시적으로 말해야 합니다.

Sub Mac2()
        Dim varResult As Variant
        Dim ActBook As Workbook

        'displays the save file dialog
        varResult = Application.GetSaveAsFilename(FileFilter:= _
                 "Excel Files (*.xlsx), *.xlsx", Title:="Save PO", _
                InitialFileName:="\\showdog\service\Service_job_PO\")

        'checks to make sure the user hasn't canceled the dialog
        If varResult <> False Then
            ActiveWorkbook.SaveAs Filename:=varResult, _
            FileFormat:=xlWorkbookNormal
            Exit Sub
        End If
End Sub

사용GetSaveAsFilename저장할 파일의 경로만 가져오는 반면,SaveAs메서드는 실제로 워크북을 저장합니다.

고려해 볼 때 단순히 다른 이름으로 저장하는 대신 다른 이름으로 저장하는 방법을 사용하는 것이 좋습니다.이름에서 알 수 있듯이 원본 워크북은 그대로 유지되고 사본은 저장됩니다.이렇게 하는 것은 다소 단순한 수정입니다.

대체할 수 있습니다.

ActiveWorkbook.SaveAs Filename:=varResult, _
FileFormat:=xlWorkbookNormal

와 함께

ActiveWorkbook.SaveCopyAs Filename:=varResult 

마지막으로 추가할 사항은 매크로 사용 워크북을 .xlsx(다른 이름으로 저장 또는 다른 이름으로 저장)로 저장하면 다른 이름으로 저장할 경우 원본 워크북에서 매크로가 손실되거나 다른 이름으로 저장할 경우 복사본에서 매크로가 손실된다는 것입니다.매크로를 사용할 수 있어야 한다면 파일을 .xlsm으로 저장하는 것을 고려하겠습니다.

가장 짧은 코드를 사용하는 것을 선호합니다.

    Application.Dialogs(xlDialogSaveAs).Show ("c:\my_folder\")

표준 Excel 저장 대화 상자입니다.

이름이 지정되지 않은 몇 가지 매개 변수가 있습니다. 이러한 매개 변수가 필요할 수 있습니다.

    Dim strFilename As String: strFilename = "report1"
    Dim strFolder As String: strFolder = "C:\temp\" 'initial directory - NOTE: Only works if file has not yet been saved!
    Dim xlfFileFormat As XlFileFormat: xlfFileFormat = XlFileFormat.xlOpenXMLWorkbook 'or replace by other XlFileFormat
    Dim strPassword As String: 'strPassword = "password" 'The password with which to protect the file - if any
    Dim booBackup As Boolean: 'booBackup = True  '(Whether to create a backup of the file.)
    Dim strWriteReservationPassword As String: 'strWriteReservationPassword = "password2" ' (The write-reservation password of the file.)
    Dim booReadOnlyRecommendation As Boolean: booReadOnlyRecommendation = False '(Whether to recommend to the user that the file be opened in read-only mode.)
    Dim booWorkbookSaved As Boolean ' true if file saved, false if dialog canceled
    If Len(strFolder) > 0 Then ChDir strFolder
    booWorkbookSaved = Application.Dialogs(xlDialogSaveAs).Show(Arg1:=strFilename, Arg2:=xlfFileFormat, Arg3:=strPassword, _
            Arg4:=booBackup, Arg5:=strWriteReservationPassword, Arg6:=booReadOnlyRecommendation)

언급URL : https://stackoverflow.com/questions/29522278/how-to-save-a-file-selected-in-save-as-dialog