2015-11-06 76 views
2

我試圖將Google街景視圖嵌入到Excel中。我發現這個link它有下面的代碼。對我來說根本不起作用,並尋找一些幫助開始。顯然,我需要設置變量來查找街景網址。但我從來沒有通過VBA插入圖片,尋找一些指導。將Google街景視圖圖像嵌入到Excel中

Sub GoogleStaticStreetView(oShape As Shape, _ 
         sAddress As String, _ 
         lHeading As Long, _ 
         Optional lHeight As Long = 512, _ 
         Optional lWidth As Long = 512) 

    'https://developers.google.com/maps/documentation/streetview/ 

    Dim sURL As String 
    Dim sMapsURL As String 

    On Error GoTo RETURN_FALSE 

    If bRunMode Then On Error Resume Next 'Error if quota exceeded 

    If Len(sAddress) > 0 Then 
     'URL-Escaped addresses 
     sAddress = Replace(sAddress, " ", "+") 
    Else 
     Exit Sub 
    End If 

    sURL = _ 
    "http://maps.googleapis.com/maps/api/streetview?" & _ 
    "&location=" & sAddress & _ 
    "&size=" & lWidth & "x" & lHeight & _ 
    "&heading=" & lHeading & _ 
    "&sensor=false" 

    sMapsURL = "http://maps.google.com/maps?q=" & _ 
    sAddress & "&t=m&layer=c&panoid=0" & _ 
    "&cbp=12," & lHeading & ",,0,4.18" 

    oShape.Fill.UserPicture sURL 
    oShape.AlternativeText = sMapsURL 

    Exit Sub 

RETURN_FALSE: 

End Sub 

Sub GoogleStaticMap(oShape As Shape, _ 
        sAddress As String, _ 
        Optional sMapType As String = "roadmap", _ 
        Optional lZoom As Long = 12, _ 
        Optional lHeight As Long = 512, _ 
        Optional lWidth As Long = 512) 

    'https://developers.google.com/maps/documentation/staticmaps/ 

    Dim sURL As String 
    Dim sMapsURL As String 
    Dim sMapTypeURL As String 

    On Error GoTo RETURN_FALSE 

    ' Google Maps Parameters '&t=m' = roadmap, '&t=k' = satellite 
    sMapTypeURL = "m" 
    If sMapType = "satellite" Then 
     sMapTypeURL = "k" 
    End If 

    If bRunMode Then On Error Resume Next 'Error if quota exceeded 

    If Len(sAddress) > 0 Then 
     'URL-Escaped addresses 
     sAddress = Replace(sAddress, " ", "+") 
    Else 
     Exit Sub 
    End If 

    sURL = _ 
    "http://maps.googleapis.com/maps/api/staticmap?center=" & _ 
    sAddress & "," & _ 
    "&maptype=" & sMapType & _ 
    "&markers=color:green%7Clabel:%7C" & sAddress & _ 
    "&zoom=" & lZoom & _ 
    "&size=" & lWidth & "x" & lHeight & _ 
    "&sensor=false" & _ 
    "&scale=1" 

    sMapsURL = "http://maps.google.com/maps?q=" & _ 
    sAddress & _ 
    "&z=" & lZoom & _ 
    "&t=" & sMapTypeURL 

    oShape.Fill.UserPicture sURL 
    oShape.AlternativeText = sMapsURL 

    Exit Sub 

RETURN_FALSE: 

End Sub 

回答

1

你可以得到的代碼中加入這一行的其他DIMS在GoogleStaticStreetView工作:

Dim bRunMode As Boolean 

然後運行該模塊:

Sub makeThisCodeWork() 
    GoogleStaticStreetView Sheets(1).Shapes.AddShape(msoShapeRectangle, 0, 0, 512, 512), "GooglePlex, CA 94043", 100 
    Debug.Assert False 
    Sheets(1).Shapes.Delete 
End Sub 

這只是創建一個矩形物體作爲容器使用,則它允許代碼粘貼圖像。

它會當它到達debug.assert false時暫停執行,然後它將刪除工作表上的所有形狀,以便您可以再次運行它。你必須使用地址和標題變量來獲得你想要的東西。

我沒有嘗試運行其他模塊,因爲這是回國的地圖,你剛纔說的街景:)

希望這有助於 - 讓你要我更詳細的我知道/解釋這裏發生了什麼。

+0

太棒了!我的代表並不是15,以支持你。第二個是我會給你你應得的道具! 一個後續問題: The Sheets(1)。 Shapes.Delete正在踢出一個錯誤。我試過Sheet.Sheet1.Shapes.Delete,但它也不那麼喜歡。我們不應該確定我們正在清除圖像的哪張紙嗎? – user5469188

+0

道歉這麼晚纔回復,儘量activesheet而不是紙板(1) –

+0

使用這個工作非常出色的: 昏暗的小水電爲形狀 對於每個SHP在ActiveSheet.Shapes Shp.Delete 接下來SHP – user5469188

相關問題