導航:首頁 > 文字圖片 > word宏vba插圖片代碼

word宏vba插圖片代碼

發布時間:2024-12-30 16:16:12

1. word vba 插入圖片

Sub 批量插入圖片()
Dim myfile As FileDialog
Set myfile = Application.FileDialog(msoFileDialogFilePicker)
With myfile
.InitialFileName = "E:\工作文件" 『這里輸入你要插入圖片的目標文件夾
If .Show = -1 Then
For Each Fn In .SelectedItems
Selection.Text = Basename(Fn) '這兩句移到這里
Selection.EndKey
If Selection.Start = ActiveDocument.Content.End - 1 Then '如游標在文末
Selection.TypeParagraph '在文末添加一空段
Else
Selection.MoveDown
End If
Set MyPic = Selection.InlineShapes.AddPicture(FileName:=Fn, SaveWithDocument:=True) '按比例調整相片尺寸
WidthNum = MyPic.Width
c = 6 '在此處修改相片寬,單位厘米
MyPic.Width = c * 28.35
MyPic.Height = (c * 28.35 / WidthNum) * MyPic.Height
If Selection.Start = ActiveDocument.Content.End - 1 Then '如游標在文末
Selection.TypeParagraph '在文末添加一空段
Else
Selection.MoveDown
End If
Next Fn
Else
End If
End With
Set myfile = Nothing
End Sub
Function Basename(FullPath) '取得文件名
Dim x, y
Dim tmpstring
tmpstring = FullPath
x = Len(FullPath)
For y = x To 1 Step -1
If Mid(FullPath, y, 1) = "\" Or _
Mid(FullPath, y, 1) = ":" Or _
Mid(FullPath, y, 1) = "/" Then
tmpstring = Mid(FullPath, y + 1)
Exit For
End If
Next
Basename = Left(tmpstring, Len(tmpstring) - 4)
End Function
執行此代碼後,彈出的選擇對話框, 全選目標文件夾下的所有圖片文件之後,點擊確定。然後靜靜的等待電腦完成處理工作,次數word會進入無響應狀態。圖片越多,無響應的時間越長。

2. 如何運用vba將指定圖片插入word中

例如,新建一個4行1列的表格,然後在Cell(3, 1)內插入圖片:
Sub Macro1()
Dim mysel
mysel = ActiveDocument.Tables(1).Cell(3, 1)
mysel.InlineShapes.AddPicture FileName:="C:\a.jpg", LinkToFile:=True, SaveWithDocument:=True
End Sub

3. 尋找WORD VBA高手解決WORD批量插入圖片程序的問題

Selection.Text = Basename(Fn) '這兩句移到這里
Selection.EndKey

這兩句移一下位置,其它不變。

Sub 批量插入圖片()
Dim myfile As FileDialog
Set myfile = Application.FileDialog(msoFileDialogFilePicker)
With myfile
.InitialFileName = "D:\111"
If .Show = -1 Then
For Each Fn In .SelectedItems
Selection.Text = Basename(Fn) '這兩句移到這里
Selection.EndKey
Set mypic = Selection.InlineShapes.AddPicture(FileName:=Fn, SaveWithDocument:=True)
'按比例調整相片尺寸
WidthNum = mypic.Width
c = 18 '在此處修改相片寬,單位厘米
mypic.Width = c * 28.35
mypic.Height = (c * 28.35 / WidthNum) * mypic.Height
If Selection.Start = ActiveDocument.Content.End - 1 Then '如游標在文末
Selection.TypeParagraph '在文末添加一空段
Else
Selection.MoveDown
End If
If Selection.Start = ActiveDocument.Content.End - 1 Then '如游標在文末
Selection.TypeParagraph '在文末添加一空段
Else
Selection.MoveDown
End If
Next Fn
Else
End If
End With
Set myfile = Nothing
End Sub
Function Basename(FullPath) '取得文件名
Dim x, y
Dim tmpstring
tmpstring = FullPath
x = Len(FullPath)
For y = x To 1 Step -1
If Mid(FullPath, y, 1) = "\" Or _
Mid(FullPath, y, 1) = ":" Or _
Mid(FullPath, y, 1) = "/" Then
tmpstring = Mid(FullPath, y + 1)
Exit For
End If
Next
Basename = Left(tmpstring, Len(tmpstring) - 4)
End Function

4. 在word中如何用VBA實現插入多張圖片

要從兩個方面考慮:
1、如何把現有文檔中的圖片導出?這個代碼片斷可以參考一下:
Set ImageStream = CreateObject("ADODB.Stream")
With ImageStream
.Type = 1
.Open
.Write ActiveDocument.InlineShapes(1).Range.EnhMetaFileBits
.SaveToFile "d:\Temp\Output.bmp"
.Close
End With
Set ImageStream = Nothing

2、如何把已經到處的圖片導入到新文檔中?這個函數調用可以參考一下:
ActiveDocument.InlineShapes.AddPicture

如果還是不明白的話,請補充提問。

_____
補充:
請問樓主你運行那個代碼片斷得到的*完整*錯誤信息是什麼?報錯的時候,系統一般會把游標移動到出錯的那行代碼上,你看到的是哪一行代碼出錯呢?
我看你貼上來的代碼應該是沒有什麼語法上的錯誤,「Exit For」的確是不應該要的,但那也不是語法錯誤啊。

5. 想在word頁面中,批量插入圖片,使得每頁有4張圖片均勻分布在頁面中,該怎麼做呢

這是統一設置word文檔中的圖片樣式,你的問題我不會,但是希望這個例子會對你有點提示。

使用宏:
一,在word中按alt+f11組合鍵,進入VBA模式
二,在左邊的工程資源管理器中找到你的word文檔,在其上右鍵/添加/模塊
三,把下面代碼復制,粘貼進去.
四,更改數值, 改一下寬度和高度數值(10),點運行(類似播放按鈕.)或f5,即可設置文檔中全部圖片
Sub Macro()
Mywidth=10'10為圖片寬度(厘米)
Myheigth=10'10為圖片高度(厘米)
For Each iShape In ActiveDocument.InlineShapes
iShape.Height = 28.345 * Myheigth
iShape.Width = 28.345 * Mywidth
Next iShape
End Sub

閱讀全文

與word宏vba插圖片代碼相關的資料

熱點內容
畫圖工具編輯圖片文字 瀏覽:35
qq小清新圖片女生 瀏覽:290
uc中如何識別圖片 瀏覽:890
服設動漫圖片 瀏覽:860
天下南北高清圖片 瀏覽:939
男生穿高筒鞋和高筒鞋圖片 瀏覽:816
女生膜破裂後圖片 瀏覽:890
動漫k帥氣圖片 瀏覽:645
簡單動漫圖畫大全圖片 瀏覽:865
男生淋浴圖片 瀏覽:238
動漫Q版呆萌圖片大全 瀏覽:360
黑夜帥氣男生圖片 瀏覽:332
白底黑字的圖片怎麼做 瀏覽:165
矮個子男生發型圖片短發 瀏覽:873
國外美女生活圖片 瀏覽:771
寵物豬睡覺圖片大全 瀏覽:460
挑染男生頭發圖片大全 瀏覽:225
女孩夏季衣服圖片背帶褲 瀏覽:290
怎麼把保存圖片類型 瀏覽:43
老鼠吃雞圖片大全可愛 瀏覽:421