利用excel VBA批量修改文件名以及自动插图到word.doc_第1页
利用excel VBA批量修改文件名以及自动插图到word.doc_第2页
利用excel VBA批量修改文件名以及自动插图到word.doc_第3页
全文预览已结束

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

前段时间,因编写报告,需要把成果图片修改名字后,将图以及相应的名字插入word文档中。一次报告,至少200张图,花了数个小时才弄完工作,同时难免出现差错。之后就一直寻找捷径,基于excel vba以前有一点基础,现将整理出来的代码分享给大家。可以去下载我编好的excel 小程序,里面有详细代码,地址在最下方。欢迎直接试用下,给个反馈建议.1.查找文件夹中符合图片格式的文件,返回其名字Dim fs, f, f1, fc, sDim arr As StringSet fs = CreateObject(Scripting.FileSystemObject)Address = ThisWorkbook.FullNameAddress = Left(Address, InStrRev(Address, , Len(Address) 获得当前工作表所在文件夹路径Set f = fs.GetFolder(Address)Set fc = f.Filesi = 2For Each f1 In fc 遍历文件If FileIspicture(f1.Name) Then 引用了自定义函数 FileIspicture 判断是否为需要查找的文件格式 phname = f1.Name 获取文件名houzhui = Right(phname, Len(phname) - InStrRev(phname, ., Len(phname) + 1)Sheet1.Cells(i, 1) = Left(phname, InStrRev(phname, ., Len(phname) - 1)Sheet1.Cells(i, 2) = houzhui i = i + 1End IfNext2.修改文件名称Sub changename()Dim Address As StringAddress = ThisWorkbook.FullNameAddress = Left(Address, InStrRev(Address, , Len(Address)n = Sheet1.Cells(Rows.Count, 1).End(xlUp).rowFor i = 2 To n 修改名称pname = Sheet1.Cells(i, 1) & Sheet1.Cells(i, 2)textname = Sheet1.Cells(i, 3)houzhui = Right(pname, Len(pname) - InStrRev(pname, ., Len(pname) + 1) 获取后缀Name Address & pname As Address & textname & houzhuiNext iMsgBox 名称已改End Sub3.批量插图到wordDim appWD As Word.ApplicationDim Address As StringmyName = picture.doc 新建的word名称Address = ThisWorkbook.FullNameAddress = Left(Address, InStrRev(Address, , Len(Address)mydoc = Address & myNameOn Error Resume Next 错误处理Kill (mydoc)On Error GoTo 0On Error Resume NextSet appWD = GetObject(, Word.Application)appWD.Documents.Close SaveChanges:=wdDoNotSaveChangesappWD.QuitSet appWD = CreateObject(Word.Application) 连接wordappWD.Documents.AddappWD.ActiveDocument.SaveAs filename:=mydocappWD.Visible = Truen = Sheet1.Cells(Rows.Count, 1).End(xlUp).row 获取工作表有效部分的最大行数For i = 2 To n 插入图片pname = Sheet1.Cells(i, 1) & Sheet1.Cells(i, 2)textname = Sheet1.Cells(i, 3)appWD.Selection.InlineShapes.AddPicture filename:=Address & pname, LinkToFile:= _False, SaveWithDocument:=TrueappWD.Selection.TypeParagraphappWD.Selection.TypeText Text:=textnameappWD.Selection.TypeParagraphhouzhui = Right(pname, Len(pname) - InStrRev(pname, ., Len(pname) + 1)Next i居中,修改字体大小为10,字体加粗appWD.Selection.WholeStoryappWD.Selection.ParagraphFormat.Alignment = wdAlignParagraphCenterappWD.Selection.Font.Size = 10appWD.Selection.Font.Name = 宋体appWD.Selection.Font.Bold = wdToggle4.修改图片大小,使每页正好两张图Dim picwidthDim picheightFor i = 1 To appWD.ActiveDocument.InlineShapes.Count InlineShapes类型图片H = appWD.ActiveDocument.InlineShapes(i).HeightW = appWD.ActiveDocument.InlineShapes(i).WidthHIV = W / HH = 325W = HIV * HIf W = 415 ThenW = 415H = W / HIVEnd IfappWD.ActiveDocument.Inl

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论