使用vba创建kml文件 - 未正确保存

时间:2014-09-15 09:17:43

标签: vba kml google-earth

我正在尝试使用VBA从SQL表中获取一些坐标,创建一些位于excel选项卡中的代码,然后将其保存为.kml文件并在Google Earth中打开该文件。

当代码创建kml文件时,它会打开GE,但没有任何反应(例如,它没有显示侧栏中的坐标,也没有指向任何东西)。 同样,当我手动导航到kml文件并在GE中打开它时,没有任何反应。 但是,如果我返回到保存为.kml的excel选项卡,将代码复制/粘贴到记事本中并手动保存为.kml,文件将在GE中打开并显示代码中的坐标。 我已经将代码剥离回到突出问题所需的最低限度(见下文)。 根据我的观察,似乎我没有正确保存文件。

Sub Mapping()

Range("A1").Value = "<?xml version=""1.0"" encoding=""UTF-8""?>"
Range("A2").Value = "<kml xmlns=""http://www.opengis.net/kml/2.2"""
Range("A3").Value = "xmlns:gx=""http://www.google.com/kml/ext/2.2"" "
Range("A4").Value = "xmlns:kml=""http://www.opengis.net/kml/2.2"" "
Range("A5").Value = "xmlns:atom=""http://www.w3.org/2005/Atom"">"
Range("A6").Value = "<Document>"
Range("A7").Value = " <Placemark> <name>" & "Name here..." & "</name> <description>" & "Testing" & "</description>"
Range("A8").Value = "<Style> <IconStyle> <scale>1.2</scale> <Icon> <href>http://maps.google.com/mapfiles/kml/pal4/icon16.png</href> </Icon> </IconStyle> </Style> "
Range("A9").Value = "<Point> <coordinates>" & " -114.232195463845,53.0160219116952,0" & "</coordinates> </Point> </Placemark>"
Range("A10").Value = "</Document> </kml> "

ActiveSheet.SaveAs "C:\Users\user\Desktop\KMLTESTING4.kml"

Dim KMLLoc As String
KMLLoc = "C:\Users\user\Desktop\KMLTESTING4.kml"
Call Shell("explorer.exe " & KMLLoc, vbNormalFocus)


End Sub 

1 个答案:

答案 0 :(得分:0)

在我的保存结束时添加了FileFormat:= xlTextPrinter,现在工作正常。