VBA help- Macro to export png image.

remjac

New Member
Joined
Jun 13, 2014
Messages
1
Long story short the person that wrote that no longer works with us, I am very inexpeirenced in writing VBA's. He had a spreadsheet that I inserted a png.image into then used text boxes laid over with road #'s and field names. Then it has a export field command box that would export it to where it was supposed to go. I recently upgraded to Excel 2013 and this program no longer works, when I hit export field i receive a System Error &H80004008 (-2147467259) unspecified error. I will post the code he wrote. Any pointers will be extremely helpful.



Sub pasteField()
'Dim fieldnum As Integer

'fieldnum = ActiveSheet.Range("Q3").Value
'fieldnum = fieldnum + 1

'ActiveSheet.Range("Q3").Value = fieldnum

ActiveSheet.Range("Q3").ClearContents


Range("B3").Select
ActiveSheet.Paste
End Sub
Sub resizeField()


Selection.ShapeRange.LockAspectRatio = msoTrue
Selection.ShapeRange.Height = 253.5
Selection.ShapeRange.Width = 253.5
Selection.ShapeRange.Rotation = 0#
Selection.ShapeRange.ZOrder msoSendToBack
End Sub
'Private
Sub exportField()

Dim oRange As Range
Dim oCht As Chart
Dim oImg As Picture
Dim field1 As String
Dim field2 As String

'Make sure a grower is selected before exporting
If ActiveSheet.Range("Q3").Value = "" Then
MsgBox ("Please enter a valid field number before attempting to export.")
Exit Sub
Else

Application.ScreenUpdating = False


'Create file name for Grower files
If Range("S3").Value = "" Then
field1 = "F:\my DOCUMENTS\Growers\Growers\" & ActiveSheet.Range("R3").Value & "\Field Maps\" & ActiveSheet.Range("R3").Value & " " & ActiveSheet.Range("Q3").Value & ".png"
Else
field1 = "F:\my DOCUMENTS\Growers\Growers\" & ActiveSheet.Range("R3").Value & "\Field Maps\" & ActiveSheet.Range("R3").Value & " " & ActiveSheet.Range("Q3").Value & " - " & ActiveSheet.Range("S3").Value & ".png"
End If

'Create file name for Overall maps
field2 = "F:\my DOCUMENTS\Fields\Field Maps\" & ActiveSheet.Range("Q3").Value & ".png"


'Check to see if file already exists in Grower files, if so deletes it so new file can be saved
If FileFolderExists(field1) Then
Kill (field1)
Else
End If

'Check to see if file already exists in Overall maps, if so deletes it so new file can be saved
If FileFolderExists(field2) Then
Kill (field2)
Else
End If
'Creates a chart within "Area Map" Sheet and pastes the image in it
Set oRange = Range("B3:M17")

Set oCht = ActiveSheet.ChartObjects.Add(Left:=0, _
Top:=oRange.Top + oRange.Height + 10, _
Width:=oRange.Width, Height:=oRange.Height).Chart

With ActiveSheet.Shapes(oCht.Parent.Name)
.fill.Visible = msoFalse
.Line.Visible = msoFalse
End With

oRange.CopyPicture xlScreen, xlPicture

oCht.Paste

'Exports to Grower files
oCht.export Filename:=field1, Filtername:="png"
'Exports to Overall maps
oCht.export Filename:=field2, Filtername:="png"

'Deletes Chart from sheet
Dim wsItem As Worksheet
Dim chtObj As ChartObject

For Each wsItem In ThisWorkbook.Worksheets

For Each chtObj In wsItem.ChartObjects

chtObj.Delete

Next

Next

Application.ScreenUpdating = True
'Clears out grower name to prepare for next export
ActiveSheet.Range("Q3").ClearContents


End If
End Sub
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.

Forum statistics

Threads
1,215,326
Messages
6,124,270
Members
449,149
Latest member
mwdbActuary

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top