[ 初めての方へ | 一覧(最新更新順) | 全文検索 | 過去ログ ]
『写真台帳に挿入する画像を、リンク付けではなく貼付けにしたい』(fam)
画像をリンク付けしているため、画像を保存しているフォルダを別のフォルダに移動させたりファイル名を変更するとリンクが解除され画像が消えてしまいます。
リンク付けではなく貼付け状態にしたいのですが、修正方法がわからず困っています。ご教授お願いいたします。
Dim strFilter As String
Dim Filenames As Variant
Dim PIC As Picture
' 「ファイルを開く」ダイアログでファイル名を取得
strFilter = "画像ファイル(*.jpg;*.jpeg;*.gif;*.bmp;*.png),*.jpg;*.jpeg;*.gif;*.bmp;*.png"
Filenames = Application.GetOpenFilename( _
FileFilter:=strFilter, _
Title:="図の挿入(複数選択可)", _
MultiSelect:=True)
If Not IsArray(Filenames) Then Exit Sub
' ファイル名をソート
Call BubbleSort_Str(Filenames, True, vbTextCompare)
' 貼り付け開始セルを選択
' マクロ実行中の画面描写を停止
Application.ScreenUpdating = False
' 順番に画像を挿入
j = -1
For i = LBound(Filenames) To UBound(Filenames)
j = j + 1
Set PIC = ActiveSheet.Pictures.Insert(Filenames(i))
'-------------------------------------------------------------
' 画像の各種プロパティ変更
'-------------------------------------------------------------
With PIC
.Top = ActiveCell.Top ' 位置:アクティブセルの上側に重ねる
.Left = ActiveCell.Left ' 位置:アクティブセルの左側に重ねる
.Placement = xlMove ' 移動するがサイズ変更しない
.PrintObject = True ' 印刷する
End With
With PIC.ShapeRange
.LockAspectRatio = msoTrue ' 縦横比維持
' 画像の幅をアクティブセルにあわせる
' 結合セルの場合でも対応
.Width = ActiveCell.MergeArea.Width 'Height:高さに合わせる場合
End With
' 次の貼り付け先を選択(アクティブセルにする)[例:5個下のセル]
If j Mod 2 = 0 Then
ActiveCell.Offset(0, 3).Select
Else
ActiveCell.Offset(8, -3).Select
End If
Set PIC = Nothing
Next i
' 終了
Application.ScreenUpdating = True
MsgBox i - 1 & "枚の画像を挿入しました", vbInformation
End Sub
' バブルソート(文字列)
Private Sub BubbleSort_Str( _
ByRef Source As Variant, _
Optional ByVal SortAsc As Boolean = True, _
Optional ByVal Compare As VbCompareMethod = vbTextCompare)
If Not IsArray(Source) Then Exit Sub
Dim i As Long, j As Long
Dim vntTmp As Variant
For i = LBound(Source) To UBound(Source) - 1
For j = LBound(Source) To LBound(Source) + UBound(Source) - i - 1
If StrComp(Source(IIf(SortAsc, j, j + 1)), _
Source(IIf(SortAsc, j + 1, j)), Compare) = 1 Then
vntTmp = Source(j)
Source(j) = Source(j + 1)
Source(j + 1) = vntTmp
End If
Next j
Next i
End Sub
< 使用 Excel:Excel2019、使用 OS:Windows10 >
過去ログ漁ったら出てきたので、補足程度に・・・
何度も出てくるコードですけど、これだけ使いまわされるとうらやましいですねぇ。
(稲葉) 2020/08/03(月) 10:00
[ 一覧(最新更新順) ]
YukiWiki 1.6.7 Copyright (C) 2000,2001 by Hiroshi Yuki.
Modified by kazu.