how to read a KML file with VBA

941 views
Skip to first unread message

darbid73

unread,
Mar 9, 2009, 11:38:37 AM3/9/09
to KML Developer Support - Getting Started with KML
I do not have http access so cannot load kml files.

Thus as far as I understand I must use the "parsekml"

I thought I would be able to read a kml file quite simply like this


*******************************************************************************
Dim GE_Features As GEFeatureContainerCoClass
Dim KMLstring As String
Dim la As KmlLookAtCoClass
Dim GE_View As GEViewCoClass
Dim edna As Object

Dim oFSO As New FileSystemObject
Dim oFS

Set oFS = oFSO.OpenTextFile("C:\Documents and Settings\mch02080\Desktop
\GoogleVBexample\test.txt")

Do Until oFS.AtEndOfStream
KMLstring = KMLstring & vbCr & oFS.ReadLine
Loop

KMLstring = Right(KMLstring, Len(KMLstring) - 4)

Set GE_Features = Me.GEPluginCoClass3.getFeatures
Set edna = Me.GEPluginCoClass3.parseKml(KMLstring)

GE_Features.appendChild edna
***********************************************************

But I keep getting an error.

Assuming that my code above is correct the question is how do I get my
KMLstring set up correctly?

darbid73

unread,
Mar 10, 2009, 12:55:59 PM3/10/09
to KML Developer Support - Getting Started with KML
I realise that VBA is causing an uneasy feeling for you but could you
at least give me some help. There is very little about this on the
internet.

I have tried reading the kml file with MSXML. and other methods of
reading a kml file line by line.

I have also tried coding the string like this

KMLstring = "<?xml version=""1.0"" encoding=""UTF-8""?>" & _
"<kml xmlns=""http://earth.google.com/kml/2.1"">" & _
"<Placemark>" & _
"<name>Geneva</name>" & _
"<LookAt>" & _
"<longitude>6.156182706892176</longitude>" & _
"<latitude>46.20746317320977</latitude>" & _
"<altitude>0</altitude>" & _
"<range>316.3662914479763</range>" & _
"<tilt>0</tilt>" & _
"<heading>6.199453434125936</heading>" & _
"</LookAt>" & _
"<Point>" & _
"<coordinates>6.156019183879536,46.20743386584116,0</coordinates>" &
_
"</Point>" & _
"</Placemark>" & _
"</kml>"

or in one big line like this

KMLstring = "<?xml version=""1.0"" encoding=""UTF-8""?><kml
xmlns=""http://earth........</kml>"

I have a feeling that there is a problem with my coding as well
because I consider these string to be correct.

If I use the above string and code i simply get no error at all? what
things can/should I check to see where the problem is? What
functions or checks are built in to GEFeatureContainerCoClass or
KmlObjectCoClass.

What are my alternatives here? Is there anyway that the plugin can
itself take a kml from anything other than http server?

If nobody really wants to answer me on this then at least tell me to
whom I can have a winge to that they intentionally disabled the plugin
from gettting kml files from a local drive. I have also tried the COM
API with google earth and am having a great time as I just have to
reference the kml.

Grok Lobster

unread,
Mar 12, 2009, 10:36:24 AM3/12/09
to KML Developer Support - Getting Started with KML
It's not clear what you are trying to accomplish. What version of
Excel are you using? What is the plugin that you refer to?

Have you tried importing as text?
> > ***************************************************************************­****
> > Dim GE_Features As GEFeatureContainerCoClass
> > Dim KMLstring As String
> > Dim la As KmlLookAtCoClass
> > Dim GE_View As GEViewCoClass
> > Dim edna As Object
>
> > Dim oFSO As New FileSystemObject
> > Dim oFS
>
> > Set oFS = oFSO.OpenTextFile("C:\Documents and Settings\mch02080\Desktop
> > \GoogleVBexample\test.txt")
>
> > Do Until oFS.AtEndOfStream
> > KMLstring = KMLstring & vbCr & oFS.ReadLine
> > Loop
>
> > KMLstring = Right(KMLstring, Len(KMLstring) - 4)
>
> > Set GE_Features = Me.GEPluginCoClass3.getFeatures
> > Set edna = Me.GEPluginCoClass3.parseKml(KMLstring)
>
> > GE_Features.appendChild edna
> > ***********************************************************
>
> > But I keep getting an error.
>
> > Assuming that my code above is correct the question is how do I get my
> > KMLstring set up correctly?- Hide quoted text -
>
> - Show quoted text -

Grok Lobster

unread,
Mar 12, 2009, 10:39:42 AM3/12/09
to KML Developer Support - Getting Started with KML
I think you mean VB, not VBA.
> > - Show quoted text -- Hide quoted text -

darbid73

unread,
Mar 13, 2009, 3:18:52 AM3/13/09
to KML Developer Support - Getting Started with KML
Hey Grok thanks for your interest in a person who must be standing
naked in the snow playing with cold water. (read doing something
different).

I am using MSAccess and I am using VBA. The first time I ever
opened an object browser was 7 months ago. I am a beginner playing.
The plugin exposes an ActiveX control that can be put on a form.

On load with Me.GEPluginCoClass3.start_ you can initialise and get the
globe. Then most things work except the click events I have a feeling
they are broken because the do not pass the correct infomation to the
event.

This means it is great to play with as you could have a series of
addresses in a database and read those onto the globe. Set up a loop
of teleports and you can fly. It is just a play toy I cannot see any
commercial use for it as it is not available for the public to see
your work on the internet.

All that I wanted to do is be able to read a kml file so that I can
have a string to parse to "Parsekml"

I worked it out. Instead of reading each line with FileSystemObject I
can just readall AND before you parse it here you have to add Cstr
funtion

Set edna = Me.GEPluginCoClass3.parseKml (Cstr(KMLstring))
Reply all
Reply to author
Forward
0 new messages