Salvar Planilha como Imagem

jp.bolsairriga

15-08-2016 14:12:13

Bom dia a todos.

preciso salvar alguma parte da minha planilha como imagem, achei um código excelente na web, fiz algumas alterações.
Até ai tudo bem, mais preciso alterar este código de forma que eu posso escolher o nome do arquivo e o local que o mesmo será salvo.

Já fiz algumas pesquisas, mais nada até agora

segue o código:

Sub Salva_Range_Como_Imagem()
'http://tech.groups.yahoo.com/group/ms_e ... sage/38865
' =========================================
' Code to save selected Excel Range as Image
' adjusted by Pascal Daulton 15-Sep-2011
'Readaptado em 16-09-2011 por RLM
' =========================================
Dim sRange As Range
Dim oCht As Chart
Dim oImg As Picture
Dim sPath As String

sPath = ThisWorkbook.Path
'Acrescenta a barra invertida "\"; se necessaria
If Right(sPath, 1) <> "\" Then
sPath = sPath & "\"
Else
sPath = sPath
End If
'Acrescenta nome arquivo na range F2
sPath = sPath & Sheets("Plan1").Range("F2").Value & ".jpeg"

Set sRange = Range("B2:C6")

With sRange
.CopyPicture xlScreen, xlPicture
Set oCht = ActiveSheet.ChartObjects.Add(50, 50, .Width + 5, .Height + 5).Chart
End With


With oCht
.Paste
.Shapes(1).Left = -.ChartArea.Left
.Shapes(1).Top = -.ChartArea.Top
.Parent.Width = sRange.Width
.Parent.Height = sRange.Height
.Export Filename:=sPath, Filtername:="jpeg"
.Parent.Delete
End With
End Sub

jp.bolsairriga

15-08-2016 18:38:57

Boa tarde a todos.

Sobre o tópico que postei anteriormente, se alguém tiver alguma outra solução, sem problemas

Reinaldo

15-08-2016 18:44:25

Puxa, ja nem lembrava mais dessa rotina, ja se foram cinco anos; estou ficando velho :lol::lol:
Bom não deixou claro como pretende "escolher" o local, assim suponho que em uma celula de planilha.
a Variavel que define o local é sPath, e na rotina está utilizando o mesmo local da planilha.
Assim, pode por exemplo alterar:
de: sPath = ThisWorkbook.Path

Para: sPath=Sheets("Plan1").Range("F1").Value

e em f1 determinar o caminho a ser utilizado.

Se não for assim de maiores detalhes

jp.bolsairriga

15-08-2016 19:25:40

Reinaldo, boa tarde.

o que eu realmente queria era que ao executar o comando aparece uma caixa de dialogo perguntando onde vou salvar o arquivo de imagem (tipo aquela janela de salvar como)

Obs: este código esta em um userform que vai rodar sem aparecer a planilha, ele vai realizar cálculos que sempre vai ter resultados diferentes.
estes resultados estão na planilha, precisava de uma opção para poder mudar o nome do arquivo e escolher em qual pasta vou salvar (tipo aquela janela de salvar como).

por isto não vou poder colocar o caminho na célula, já que a planilha não aparece

jp.bolsairriga

16-08-2016 00:15:18

Boa noite a todos

Consegui resolver parte do problema depois de ler a resposta do Reinaldo.

Criei uma textbox, dentro do meu formulario para receber o nome do meu arquivo a ser salvo como imagem, deste modo deixei de ficar substituindo o arquivo que já estavam salvo.

Mais ainda fica aquela questão da pasta onde o arquivo de imagem vai ficar salvo
Neste código ele salva no mesmo local onde a planilha já se encontra salva.
como posso alterar meu código para salvar a imagem que estou salvando em outra pasta, por exemplo neste endereço"C:\Users\João Paulo\Desktop\teste de imagem".

Tentei alterar a parte abaixo, creio que seja aqui, mais não deu certo:
sPath = ThisWorkbook.Path
mudei para
sPath =C:\Users\João Paulo\Desktop\teste de imagem

mais nada aconteceu, este formulário vai realizar cálculos, e eu gostaria de ter a opção de escolher o local onde vou salvar a imagem, desta forma cada calculo poderia ser salvo juntamente com os demais arquivos referente aos cálculos

Private Sub Salva_Range_Como_Imagem()

'http://tech.groups.yahoo.com/group/ms_e ... sage/38865
' =========================================
' Code to save selected Excel Range as Image
' adjusted by Pascal Daulton 15-Sep-2011
'Readaptado em 16-09-2011 por RLM
' =========================================
Dim sRange As Range
Dim oCht As Chart
Dim oImg As Picture
Dim sPath As String

sPath = ThisWorkbook.Path
'Acrescenta a barra invertida "\"; se necessaria
If Right(sPath, 1) <> "\" Then
sPath = sPath & "\"
Else
sPath = sPath
End If
'Acrescenta nome arquivo na range F2
sPath = sPath & TextBox1 & ".jpeg"

Set sRange = Range("B2:C4")

With sRange
.CopyPicture xlScreen, xlPicture
Set oCht = ActiveSheet.ChartObjects.Add(50, 50, .Width + 5, .Height + 5).Chart
End With


With oCht
.Paste
.Shapes(1).Left = -.ChartArea.Left
.Shapes(1).Top = -.ChartArea.Top
.Parent.Width = sRange.Width
.Parent.Height = sRange.Height
.Export Filename:=sPath, Filtername:="jpeg"
.Parent.Delete
End With

End Sub

Reinaldo

16-08-2016 00:58:19

Teste / experimente / adapte

Código: Selecionar todos

Sub Salva_Range_Como_Imagem1()
' =========================================
' Code to save selected Excel Range as Image
' adjusted by Pascal Daulton 15-Sep-2011
'Readaptado em 16-09-2011/21-02-2014/15-08-2016 por Reinaldo(RLM)
' =========================================
Dim sRange As Range
Dim oCht As Chart
Dim oImg As Picture
Dim sPath As String, rRange As String, sArqNome As String

sPath = Application.FileDialog(msoFileDialogFolderPicker).Show
sPath = Application.FileDialog(msoFileDialogFolderPicker).SelectedItems(1)
'Verifica se foi selecionado uma pasta

sArqNome = Application.InputBox(prompt:="Informe o nome do Arquivo", Title:="Arquivo")
'Acrescenta a barra invertida "\"; se necessaria
    If Right(sPath, 1) <> "\" Then
        sPath = sPath & "\"
        Else
        sPath = sPath
    End If
MsgBox sPath & sArqNome
    Set sRange = Application.InputBox(prompt:= _
           "Selecione o intervalo a ser copiado", _
           Title:="Lê Intervalo", Type:=8)
       If sRange.Address = "" Then
            MsgBox "Sem  informação da range"
            Exit Sub
       End If
With sRange
.CopyPicture xlScreen, xlPicture
Set oCht = ActiveSheet.ChartObjects.Add(50, 50, .Width + 5, .Height + 5).Chart
End With

With oCht
.Paste
.Shapes(1).Left = -.ChartArea.Left
.Shapes(1).Top = -.ChartArea.Top
.Parent.Width = sRange.Width
.Parent.Height = sRange.Height
.Export Filename:=sPath & sArqNome & ".gif", FilterName:="gif"
.Parent.Delete
End With
End Sub

jp.bolsairriga

15-09-2016 13:52:05

Bom dia a todos.
A solução do Reinaldo funcionou muito bem, muito obrigado.
Agora notei que a resolução/qualidade da imagem esta bem baixa, alguém tem alguma sugestão de como melhorar isto?