導航:首頁 > 文字圖片 > word模板vba批量插入圖片

word模板vba批量插入圖片

發布時間:2023-01-31 18:56:13

1. 尋找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

2. 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會進入無響應狀態。圖片越多,無響應的時間越長。

3. word用vba批量導入圖片文件時,怎樣保留圖片原有大小,而不是統一固定大小跪謝!下為代碼

刪掉紅圈裡面的

4. 如何實現Word 中批量插入圖片

Sub批量添加圖片()
'程序功能:批量添加圖片宏
'作者Q:766110727
'日期:2017.06.07
'使用步驟1:word中ALT+11進入VBA宏界面;
'使用步驟2:將本段代碼從sub到endsub,全部粘到代碼框最後的空白處;
'使用步驟3:按F5執行代碼。
'說明:代碼按最直觀的邏輯寫的,也方便大家使用修改。
'OnErrorResumeNext
Dimn,圖片路徑1(),圖片路徑2(),圖片路徑3(),圖片路徑4(),圖片數量最大值
DimmyTableAsTable
'###################################################################
'#######第一部分:獲取每個文件夾下所有jpg圖片名,寫入相應數組#######
'###################################################################
n=0
圖1=Dir("I: tjbt插入圖片圖片*.jpg")
DoWhile圖1<>""
n=n+1
ReDimPreserve圖片路徑1(1Ton)
圖片路徑1(n)="I: tjbt插入圖片圖片"&圖1
圖1=Dir()
Loop
Ifn>圖片數量最大值Then圖片數量最大值=n
n=0
圖2=Dir("I: tjbt插入圖片圖片2*.jpg")
DoWhile圖2<>""
n=n+1
ReDimPreserve圖片路徑2(1Ton)
圖片路徑2(n)="I: tjbt插入圖片圖片2"&圖2
圖2=Dir()
Loop
Ifn>圖片數量最大值Then圖片數量最大值=n
n=0
圖3=Dir("I: tjbt插入圖片圖片3*.jpg")
DoWhile圖3<>""
n=n+1
ReDimPreserve圖片路徑3(1Ton)
圖片路徑3(n)="I: tjbt插入圖片圖片3"&圖3
圖3=Dir()
Loop
Ifn>圖片數量最大值Then圖片數量最大值=n
n=0
圖4=Dir("I: tjbt插入圖片圖片4*.jpg")
DoWhile圖4<>""
n=n+1
ReDimPreserve圖片路徑4(1Ton)
圖片路徑4(n)="I: tjbt插入圖片圖片4"&圖4
圖4=Dir()
Loop
Ifn>圖片數量最大值Then圖片數量最大值=n

'###################################################################
'##################第二部分:插入圖片到word表格中###################
'###################################################################
'新建一個一行兩列表格
SetmyTable=ActiveDocument.Tables.Add(Range:=ActiveDocument.Range(Start:=0,End:=0),NumRows:=1,NumColumns:=2)
'設定後續操作的目標表格為文檔的第一個表格
SetmyTable=ActiveDocument.Tables(1)
n=0
DoWhilen<圖片數量最大值
n=n+1
之前表格行數=myTable.Rows.Count
myTable.Rows.Last.Select
Selection.InsertRowsBelow4'下方插入4行
'第一行兩列,圖片序號12
myTable.Cell(Row:=之前表格行數+1,Column:=1).Range.InsertAfterText:="圖片1"
myTable.Cell(Row:=之前表格行數+1,Column:=2).Range.InsertAfterText:="圖片2"
myTable.Cell(Row:=之前表格行數+3,Column:=1).Range.InsertAfterText:="圖片3"
myTable.Cell(Row:=之前表格行數+3,Column:=2).Range.InsertAfterText:="圖片4"
'第二行兩列,插入圖片並設置圖片大小
myTable.Cell(Row:=之前表格行數+2,Column:=1).Range.InlineShapes.AddPictureFileName:=_
圖片路徑1(n),LinkToFile:=False,SaveWithDocument:=True
myTable.Cell(Row:=之前表格行數+2,Column:=2).Range.InlineShapes.AddPictureFileName:=_
圖片路徑2(n),LinkToFile:=False,SaveWithDocument:=True
myTable.Cell(Row:=之前表格行數+4,Column:=1).Range.InlineShapes.AddPictureFileName:=_
圖片路徑3(n),LinkToFile:=False,SaveWithDocument:=True
myTable.Cell(Row:=之前表格行數+4,Column:=2).Range.InlineShapes.AddPictureFileName:=_
圖片路徑4(n),LinkToFile:=False,SaveWithDocument:=True
Loop

ActiveDocument.Tables(1).Rows(1).Delete'剛開始建的表格中第一行是空行,刪掉

'###################################################################
'######################第三部分:圖片格式處理#######################
'###################################################################
'統一設置圖片長寬,美化文檔。這里會卡好久時間,按需要是否添加本段代碼
Forn=1ToActiveDocument.InlineShapes.Count
ActiveDocument.InlineShapes(n).Height=210'設置高度
ActiveDocument.InlineShapes(n).Width=110'設置寬度
Nextn
EndSub

5. 求助:如何通過VBA批量選中word文檔里的嵌入型圖片或表格

提供示例代碼供參考:

SubBatEditPic()
DimShapesCountAsInteger
DimiAsInteger
ShapesCount=ActiveDocument.InlineShapes.Count
Fori=1ToShapesCount
WithActiveDocument.InlineShapes(i)
'修改圖片邊框
With.Borders(wdBorderTop)
.LineStyle=wdLineStyleSingle
.LineWidth=wdLineWidth050pt
EndWith
With.Borders(wdBorderBottom)
.LineStyle=wdLineStyleSingle
.LineWidth=wdLineWidth050pt
EndWith
With.Borders(wdBorderLeft)
.LineStyle=wdLineStyleSingle
.LineWidth=wdLineWidth050pt
EndWith
With.Borders(wdBorderRight)
.LineStyle=wdLineStyleSingle
.LineWidth=wdLineWidth050pt
EndWith
EndWith
Nexti
EndSub

6. 利用VBA如何將批量圖片導入WORD的指定表格格式里

將excel表格中的數據批量插入到word中的操作方法:
1、單擊插入----對象按鈕;
2、彈出插入對話框對話框,選擇由文件創建對話框,單擊瀏覽按鈕;
3、彈出瀏覽對話框,選擇需要插入有Excel文件;
4、單擊插入按鈕即可。
註:
(1)若選中鏈接到文件,源文件修改,Word中的表格也隨之改變;
(2)若選中顯示為圖片,則以圖標的形式插入到Word,雙擊可以打開其源文件。

7. 在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」的確是不應該要的,但那也不是語法錯誤啊。

8. vba關於批量插入圖片

批量插入圖片代碼:
sub 圖片導入
dim s as shape
dim rg as range
'刪除已有的圖片
for each s in activesheet.shapes
if s.type <> 8 then
s.delete
end if
'導入圖片
for each rg in range("B2:B10")'這里放要插入圖片的單元格範圍 B2:B10可以改成所需要的范圍。
activesheet.shapes.addshape(msoshaperectangle,rg.left,rg.top,rg.width,rg.height).select
selection.shaperange.fill.userpicture "E:\圖片"& rg.offset(0, -1) & ".jpg"'這里指定的是圖片的存儲路徑為E盤下的圖片文件夾,然後圖片名稱與A列的數據一致,後綴名為JPG格式。這些都可以自行更改,根據需要來定。
next rg
end sub

閱讀全文

與word模板vba批量插入圖片相關的資料

熱點內容
部落圖片簡單又漂亮 瀏覽:703
女生學挖掘機的圖片 瀏覽:526
衣服披在後面的圖片 瀏覽:624
人物的動漫圖片 瀏覽:967
第二套衣服圖片 瀏覽:321
運輸局制服衣服圖片 瀏覽:938
鏍絲燙發發型圖片 瀏覽:675
美女露100奶頭無擋的圖片 瀏覽:919
灰白色頭發圖片女生 瀏覽:733
微信鈔票圖片大全 瀏覽:385
中澳混血兒圖片女孩唯美 瀏覽:380
唯美手寫文字圖片 瀏覽:301
女孩露臀誘惑圖片 瀏覽:231
棕黑衣服圖片 瀏覽:204
男生圖片怎麼調色好看 瀏覽:749
衣服的logo圖片怎樣清除 瀏覽:338
拿話筒的女生頭像圖片動漫 瀏覽:531
火影忍者男生的內褲圖片 瀏覽:984
如何將ppt保存為長圖片 瀏覽:727
長莫西干發型圖片 瀏覽:883