Notifications
Clear all

Erro 1004 ao salvar arquivo já existente

3 Posts
2 Usuários
0 Reactions
1,163 Visualizações
(@edilsonjc)
Posts: 39
Eminent Member
Topic starter
 

Fala Galera.
Tenho um código que abre a caixa de diálogo e salva o arquivo em pdf, porém retorna erro 1004 se já existe o arquivo com o mesmo nome no diretório selecionado.
Gostaria de resolver este impasse, se possível, com as seguintes situações:
. Deletar o arquivo antigo e salvar o novo ou, substituir o antigo pelo novo, assim como o Windows já faz por padrão, porém via macro.
segue código que estou usando:

Private Sub PrintPage_Click()
Dim v As Variant
v = Application.GetSaveAsFilename("Meu Arquivo.pdf", "PDF Files (*.pdf), *.pdf")

If VarType(v) = vbString Then
  ActiveWorkbook.ExportAsFixedFormat Type:=xlTypePDF, Filename:=v, _
    Quality:=xlQualityStandard, IncludeDocProperties:=True, _
    IgnorePrintAreas:=False, From:=1, To:=3, OpenAfterPublish:=True
End If

End Sub
 
Postado : 31/03/2015 10:32 am
Issamu
(@issamu)
Posts: 605
Honorable Member
 

Tente conforme o código:

Private Sub PrintPage_Click()
Dim v As Variant

If ArquivoExiste(ThisWorkbook.Path & "Meu arquivo.pdf") = True Then
    Kill ThisWorkbook.Path & "Meu arquivo.pdf"
End If

v = Application.GetSaveAsFilename("Meu Arquivo.pdf", "PDF Files (*.pdf), *.pdf")

If VarType(v) = vbString Then
  ActiveWorkbook.ExportAsFixedFormat Type:=xlTypePDF, Filename:=v, _
    Quality:=xlQualityStandard, IncludeDocProperties:=True, _
    IgnorePrintAreas:=False, From:=1, To:=3, OpenAfterPublish:=True
End If

End Sub

Function ArquivoExiste(sCaminho As String) As Boolean
        
    If Dir(sCaminho) = vbNullString Then
        ArquivoExiste = False
    Else
        ArquivoExiste = True
    End If
    
End Function

Rafael Issamu F. Kamimura
Moderador Oficial Microsoft Community - MCC (Contribuidor do Microsoft Community)
http://zip.net/bjrt0X - http://zip.net/bhrvbR
Foi útil? Clique na mãozinha
Conheça: http://excelmaniacos.com/

 
Postado : 31/03/2015 10:49 am
(@edilsonjc)
Posts: 39
Eminent Member
Topic starter
 

Issamu.
Cara não tenho nem o que falar. Showwwwwwwwwwwwwwwww.
Super Obrigado.
:o

 
Postado : 31/03/2015 11:10 am