メインコンテンツへスキップ

テキストボックスに画像を挿入する方法は?

デフォルトでは、挿入機能を使用して画像または画像をワークシートにすばやく挿入できますが、この機能はテキストボックスへの画像の挿入には適用されません。 この記事では、必要に応じて画像をテキストボックスに挿入するお手伝いをします。

VBAコードを使用して画像または画像をテキストボックスに挿入します


矢印青い右バブル VBAコードを使用して画像または画像をテキストボックスに挿入します

画像をテキストボックスに挿入するには、次のVBAコードが役立つ場合があります。次のようにしてください。

1。 クリックしてテキストボックスを挿入します インセット > テキストボックス、次にテキストボックスを描画します。スクリーンショットを参照してください。

docテキストボックスに画像を挿入1

2。 次に、 Alt + F11 キーを押して アプリケーション向け Microsoft Visual Basic 窓。

3に設定します。 OK をクリックします。 インセット > モジュール、次のコードをに貼り付けます モジュール 窓。

VBAコード:テキストボックスに画像を挿入します。

Sub ShapePicture()
    Dim xSh As Shape
    Dim xPic As IPictureDisp
    Dim xFileName As String
    xFileName = "C:\Users\DT168\Desktop\pictures\Apple.JPG"
    Set xPic = LoadPicture(xFileName)
    Set xSh = Sheets("Sheet4").Shapes(1)
    xSh.Height = xPic.Height / xPic.Width * xSh.Width
    Set xPic = LoadPicture("")
    Set xPic = Nothing
    xSh.Fill.UserPicture xFileName
End Sub

Note:上記のコードでは、 Sheet4 画像を挿入するテキストボックスを含むシート名であり、 C:\ Users \ DT168 \ Desktop \ pictures \ Apple.JPG 挿入する画像の画像パスです。必要に応じて変更する必要があります。

4。 そして、 F5 このコードを実行するためのキーを押すと、特定の画像がテキストボックスに一度に挿入され、必要に応じてテキストボックスを編集できます。スクリーンショットを参照してください。

docテキストボックスに画像を挿入2


関連記事:

テキストボックスにデフォルト値を設定するにはどうすればよいですか?

テキストボックスに数字のみを入力できるようにするにはどうすればよいですか?

テキストボックスでスペルチェックを適用するにはどうすればよいですか?

Excelの値に基づいてテキストボックスの色を変更するにはどうすればよいですか?

最高のオフィス生産性向上ツール

🤖 Kutools AI アシスタント: 以下に基づいてデータ分析に革命をもたらします。 インテリジェントな実行   |  コードを生成  |  カスタム数式の作成  |  データを分析してグラフを生成する  |  Kutools関数を呼び出す...
人気の機能: 重複を検索、強調表示、または識別する   |  空白行を削除する   |  データを失わずに列またはセルを結合する   |   数式なしのラウンド ...
スーパールックアップ: 複数の基準の VLookup    複数の値の VLookup  |   複数のシートにわたる VLookup   |   ファジールックアップ ....
詳細ドロップダウン リスト: ドロップダウンリストを素早く作成する   |  依存関係のドロップダウン リスト   |  複数選択のドロップダウンリスト ....
列マネージャー: 特定の数の列を追加する  |  列の移動  |  Toggle 非表示列の表示ステータス  |  範囲と列の比較 ...
注目の機能: グリッドフォーカス   |  デザインビュー   |   ビッグフォーミュラバー    ワークブックとシートマネージャー   |  リソースライブラリ (自動テキスト)   |  日付ピッカー   |  ワークシートを組み合わせる   |  セルの暗号化/復号化    リストごとにメールを送信する   |  スーパーフィルター   |   特殊フィルター (太字/斜体/取り消し線をフィルター...) ...
上位 15 のツールセット12 テキスト ツール (テキストを追加, 文字を削除する、...)   |   50+ チャート 種類 (ガントチャート、...)   |   40+ 実用的 (誕生日に基づいて年齢を計算する、...)   |   19 挿入 ツール (QRコードを挿入, パスから画像を挿入、...)   |   12 変換 ツール (数字から言葉へ, 通貨の換算、...)   |   7 マージ&スプリット ツール (高度な結合行, 分割セル、...)   |   ... もっと

Kutools for Excel で Excel スキルを強化し、これまでにない効率を体験してください。 Kutools for Excelは、生産性を向上させ、時間を節約するための300以上の高度な機能を提供します。  最も必要な機能を入手するにはここをクリックしてください...

説明


Officeタブは、タブ付きのインターフェイスをOfficeにもたらし、作​​業をはるかに簡単にします

  • Word、Excel、PowerPointでタブ付きの編集と読み取りを有効にする、パブリッシャー、アクセス、Visioおよびプロジェクト。
  • 新しいウィンドウではなく、同じウィンドウの新しいタブで複数のドキュメントを開いて作成します。
  • 生産性を 50% 向上させ、毎日何百回もマウス クリックを減らすことができます!
Comments (6)
No ratings yet. Be the first to rate!
This comment was minimized by the moderator on the site
Yes, I changed both. I did not get any errors when I ran the macro using the F5 key.

I tried to place the macro in different folders of the workbook, worksheet, module,class module but the results were the same: no picture in the text box.


Thanks for writing back. The last name is Hecker, BTW....typo
This comment was minimized by the moderator on the site
Tried this macro on Excel 365. It did not work. Did not receive error message. Macro ran to completion. Picture is not shown in textbox.
This comment was minimized by the moderator on the site
Hello,harvey,
Do you change the sheet name and picture path to your own in the code? Please chek it, thank you!

Note: In the above code, Sheet4 is the sheet name which contains the text box you want to insert picture, and C:\Users\DT168\Desktop\pictures\Apple.JPG is the picture path of the picture you want to insert, you should change them to your need.
This comment was minimized by the moderator on the site
Hi Sky
I have changed the sheet name and the path of the picture. But it still doesn't work.

I got bugs here in "xSh.Fill.UserPicture xFileName"

Many thanks.
Meg
This comment was minimized by the moderator on the site
Hello, Meg,
The code in this article may work well for some simple name picture.
If your picture name is complex, you should apply the below code:
Note: Please change the Text Box 1 to your own name of the textbox.
Sub InsertPicture()
    Dim sPicture As String, pic As Picture
    Dim shp As Shape, shps As ShapeRange  
    sPicture = Application.GetOpenFilename("Pictures (*.gif; *.jpg; *.bmp; *.tif;*.png), *.gif; *.jpg; *.bmp; *.tif; *.png", _
        , "Select Picture to Import") 
    If sPicture = "False" Then Exit Sub   
    Set shp = ActiveSheet.Shapes("Text Box 1")
    Set pic = ActiveSheet.Pictures.Insert(sPicture)
    With pic
        .Name = "My Picture"
        .ShapeRange.LockAspectRatio = msoFalse
        .Height = shp.Height
        .Width = shp.Width
        .Top = shp.Top
        .Left = shp.Left
        .Placement = xlMoveAndSize
    End With
    Set shps = ActiveSheet.Shapes.Range(Array("Text Box 1", "My Picture"))
    shps.Group
    shps.Name = "Pic inside text box"    
    Set pic = Nothing
End Sub


Please have a try, hope it can help you!
If you have any other problem, please comment here.
This comment was minimized by the moderator on the site
It work perfectly. Only change the Text box name as shown:
Set xSh = Sheets("Sheet1").Shapes("TextBox 2")
There are no comments posted here yet
Please leave your comments in English
Posting as Guest
×
Rate this post:
0   Characters
Suggested Locations