2025年4月22日 星期二

VBA SVG 幼幼班

 SVG(可縮放向量圖形)是一種基於XML的圖像格式,適合透過VBA進行基礎操作。以下為結合VBA的幼幼班教學,從簡單形狀繪製到進階應用分階段說明:


p.s office 365 版本適用

基礎形狀繪製

步驟1:建立SVG檔案結構

Sub CreateBasicSVG()

    Dim svgContent As String

    svgContent = "<?xml version=""1.0"" encoding=""UTF-8"" standalone=""no""?>" & vbCrLf & _

                 "<svg width=""200"" height=""200"" xmlns=""http://www.w3.org/2000/svg"">" & vbCrLf

    ' 在此插入形狀代碼

    svgContent = svgContent & "</svg>"

    

    ' 儲存檔案

    Open "C:\BasicShape.svg" For Output As #1

    Print #1, svgContent

    Close #1

End Sub


步驟2:加入矩形

svgContent = svgContent & "<rect x=""50"" y=""50"" width=""100"" height=""60"" fill=""#FF0000""/>" & vbCrLf


步驟3:加入圓形

svgContent = svgContent & "<circle cx=""100"" cy=""100"" r=""40"" fill=""#00FF00""/>" & vbCrLf


Sub AddSVGToSlide()

    Dim shp As Shape

    Set shp = ActivePresentation.Slides(1).Shapes.AddShape(msoShapeRectangle, 100, 100, 200, 200)

     ' 設定圖形樣式為SVG

    shp.GraphicStyle = msoGraphicStyleFlat  ' 使用預設平面樣式[2]

    shp.Fill.UserPicture "C:\BasicShape.svg"  ' 載入外部SVG檔案

End Sub


沒有留言:

張貼留言

指數變化(2026.01.23) 開始透過AI做整理

  指數變化(2026.01.23) 開始透過AI做整理 上周焦點: 1/13消費者物價相關 美國消費者物價指數(CPI)年增率:2.7%(符合預期),月增率:0.3%。 美國核心CPI年增率:2.6%。 1/14企業庫存、房市、能源及房貸相關 美國企業庫存月增率:0.3%(10...