promotion image of download ymail app
Promoted
凱文 asked in 電腦與網際網路軟體 · 1 decade ago

請問要如何在Excel巨集內動態改變插入圖片

如以下的語法

如何動態改變圖片. 1.jpg要如何設定成變數呢

Selection.ShapeRange.Fill.UserPicture "D:\1.jpg"

1 Answer

Rating
  • 1 decade ago
    Favorite Answer

    底下巨集的 shrec 是一個物件變數,jpgfile 則就是你問的一個 Variant 型態的變數,你可將 jpgfile 宣告為 String 型態的變數,不過因為如果你在開啟圖檔對話方塊時按了「取消」,因開啟圖檔對話方塊會傳回 False 所以你就得把 If jpgfile <> False Then 改成 If jpgfile <> "False" Then

    Sub 插入文字方塊並設定背景圖() '宣告 Shape型態的變數 Dim shrec As Shape '在 A1 插入一個 100*100 的文字方塊 Set shrec = ActiveSheet.Shapes.AddShape(msoShapeRectangle, 1, 1, 100, 100) '宣告 Variant型態的變數 Dim jpgfile As Variant '選擇一個圖檔 jpgfile = Application.GetOpenFilename("圖檔, *.jpg;*.bmp;*.gif") '如果你有選擇圖檔的話就將該圖檔設為文字方塊的背景圖 If jpgfile <> False Then shrec.Fill.UserPicture jpgfile End If '釋放物件變數 Set shrec = NothingEnd Sub

    2008-11-19 15:48:20 補充:

    單純一點直接在程式碼裡面將路徑給變數,雖然程式碼顯得有點奇怪。

    Dim jpgfile As String

    ActiveSheet.Shapes("Rectangle 1").Select

    jpgfile = "D:\1.jpg"

    Selection.ShapeRange.Fill.UserPicture jpgfile

    MsgBox "按下確定後背景會改為 2.jpg"

    jpgfile = "D:\2.jpg"

    Selection.ShapeRange.Fill.UserPicture jpgfile

    • Commenter avatarLogin to reply the answers
Still have questions? Get your answers by asking now.