我正在构建一个Access表单,允许用户以地址(号码),街道,城镇,县,州的形式输入街道地址。完成此操作后,他们可以单击按钮生成一个KML文件,在该地址定义地标并在Google地球中打开。但是,当文件打开时,Google地球会发出错误,说明“没有结果 - 空KML文件"”。如果随后通过文件资源管理器手动打开该文件,则该地标将按预期生成(几乎)。输入address = 3
,street = "Todd Lane"
,Town = "Old Tappan"
,County = Bergen
,State = "New Jersey"
的方法代码和生成的KML如下所示。
Private Sub ExportToKMLButton_Click()
Dim FName As String
FName = "C:\Users\Public\QueryOutput.KML"
Close #1 'Make sure nothing is already open as #1.
Open FName For Output As #1 'Open the file defined by FName.
Dim outputtext As New Collection
outputtext.Add "<?xml version=""1.0"" encoding=""UTF-8""?>" 'Denotes the language to follow.
outputtext.Add Item:="<kml xmlns=""http://earth.google.com/kml/2.0"">"
outputtext.Add Item:="<Document>"
outputtext.Add Item:="<Placemark>"
outputtext.Add Item:="<name>Active Location</name>"
outputtext.Add Item:="<lookat>Active Location</name>"
outputtext.Add Item:="<address>" & Address.Value & " " & Street.Value & _
", " & Town.Value & ", " & County.Value & " County, " & State.Value & "</address>"
outputtext.Add Item:="</Placemark>"
outputtext.Add Item:="</Document>"
outputtext.Add Item:="</kml>"
Dim output As String
Dim i As Long
For i = 1 To outputtext.Count
output = output & outputtext.Item(i)
Next i
output = Replace(output, "&", "and") 'Remove any ampersands from the output to avoid errors.
Print #1, output 'Print to file.
Dim wsh As Object
Set wsh = VBA.CreateObject("WScript.Shell")
wsh.Run FName, 1, True
On Error GoTo EndOfExport 'If the user runs this function twice consecutively, without closing Google Earth, both instances of the function _
will resume simultaneously when Earth is closed. One will delete the single extant copy of the KML file, and the _
other will attempt to do the same. When the file is not found, an error will occur. This error does no harm, so _
we simply skip it and end the function.
'Kill FName 'Deletes the generated file after opening it.
GoTo EndOfExport
ErrorFound:
MsgBox ("An error has occurred. Your export may be incomplete.")
EndOfExport:
Close #1
End Sub
生成的KML(为了便于阅读而编辑;原文中没有换行符):
<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://earth.google.com/kml/2.0">
<Document>
<Placemark>
<name>Active Location</name>
<address>3 Todd Lane, Old Tappan, Bergen County, New Jersey</address>
</Placemark>
</Document>
</kml>
答案 0 :(得分:0)
似乎问题在于,当调用运行它的shell脚本时,文件仍然可以打开进行编辑。在Close #1
和Print #1, output
之间移动Dim wsh As Object
后,问题就停止了。
Print #1, output 'Print to file.
Close #1
Dim wsh As Object