用vba打开word模板并修改后保存
1、打开Word文件的 VBA编辑器,快捷键 Alt+F11,右击【ThisDocument】-》 【插入模块】;用VBA代码设置Word自动保存的步骤2、双击刚才插入的【模块1】,添加如下代码:Sub 自动备份()Dim NewTimeNewTime = Now + TimeValue(“00:05:10”)Dim myPath$, myName$myPath = ActiveDocument.PathmyName = Left$(ActiveDocument.Name, Len(ActiveDocument.Name) - 4)ChangeFileOpenDirectory myPathActiveDocument.SaveAs FileName:=myName & “_temp.doc”, ReadOnlyRecommended:=TrueActiveDocument.SaveAs FileName:=myName & “.doc”, ReadOnlyRecommended:=FalseApplication.OnTime NewTime, “自动备份”CreateObject(“Wscript.shell”).popup “备份成功,备份文件名为:” & myName & “_temp.doc”, 2, “提示!2秒后自动关闭!”End Sub用VBA代码设置Word自动保存的步骤用VBA代码设置Word自动保存的步骤3、双击【ThisDocument】并在其中 添加如下代码:Private Sub Document_Open()Call 自动备份End Sub用VBA代码设置Word自动保存的步骤4、默认自动备份时间为5min,如要调整请修改【模块1】中一句代码:如图中红框所示:时间格式为:HH : mm : ss用VBA代码设置Word自动保存的步骤5、保存代码及文件,且关闭word并重新打开,重新打开点击【选项】-》 【启用此内容】,如图:用VBA代码设置Word自动保存的步骤6、默认备份文件名为:【原文件名_temp,Lee.doc】且为只读,提示对话框2s后自动关闭。
备份效果显示如下:
VBA修改word文档
开启模糊查询即可,再判断一下查找的长度Sub 分章()Set myrange = ActiveDocument.ContentFor n = 1 To 8myrange.Find.Execute findtext:="第*章", Forward:=True, MatchWildcards:=TrueIf myrange.End - myrange.Start < 20 Thenmyrange.Style = ActiveDocument.Styles("标题 2")End IfNextEnd Sub测试运行了下,效果有点不好,修改一下Sub 分章()Set myrange = ActiveDocument.ContentFor n = 1 To 8myrange.Find.Execute findtext:="第*章", Forward:=True, MatchWildcards:=TrueIf myrange.End - myrange.Start < 20 Thenmyrange.Style = ActiveDocument.Styles("标题 2")Elsemyrange.Start = myrange.Start + 1End IfNextEnd Sub
有多个文件夹,修改里面多层文件夹内表格名称,VBA怎么写呢
展开全部 Sub 批量插入图片()Dim myfile As FileDialogSet myfile = Application.FileDialog(msoFileDialogFilePicker)With myfile.InitialFileName = "E:\工作文件" ‘这里输入你要插入图片的目标文件夹If .Show = -1 ThenFor Each Fn In .SelectedItemsSelection.Text = Basename(Fn) '这两句移到这里Selection.EndKeyIf Selection.Start = ActiveDocument.Content.End - 1 Then '如光标在文末Selection.TypeParagraph '在文末添加一空段ElseSelection.MoveDownEnd IfSet MyPic = Selection.InlineShapes.AddPicture(FileName:=Fn, SaveWithDocument:=True) '按比例调整相片尺寸WidthNum = MyPic.Widthc = 6 '在此处修改相片宽,单位厘米MyPic.Width = c * 28.35MyPic.Height = (c * 28.35 / WidthNum) * MyPic.HeightIf Selection.Start = ActiveDocument.Content.End - 1 Then '如光标在文末Selection.TypeParagraph '在文末添加一空段ElseSelection.MoveDownEnd IfNext FnElseEnd IfEnd WithSet myfile = NothingEnd SubFunction Basename(FullPath) '取得文件名Dim x, yDim tmpstringtmpstring = FullPathx = Len(FullPath)For y = x To 1 Step -1If Mid(FullPath, y, 1) = "\" Or _Mid(FullPath, y, 1) = ":" Or _Mid(FullPath, y, 1) = "/" Thentmpstring = Mid(FullPath, y + 1)Exit ForEnd IfNextBasename = Left(tmpstring, Len(tmpstring) - 4)End Function执行此代码后,弹出的选择对话框, 全选目标文件夹下的所有图片文件之后,点击确定。
然后静静的等待电脑完成处理工作,次数word会进入无响应状态。
图片越多,无响应的时间越长。
WORD 如何使用VBA修改页脚的字体,字号
Sub setPageFont() Application.PrintCommunication = False With ActiveSheet.PageSetup .LeftHeader = "" '页眉左设置为:空 .CenterHeader = "&F" '页眉中设置为:文件名 .RightHeader = "" '页眉右设置为:空 .LeftFooter = "&B 机密 &B" '页脚左设置为:加粗的 机密两个字 .CenterFooter = "&D" '页脚中设置为:当前日期 .RightFooter = " 第 &P 页" '页脚右设置为:页码 End With Application.PrintCommunication = TrueEnd Sub
vba批量修改同文件夹工作薄名称
展开全部 批量替换前缀吧。
Sub RenameFiles()'Edit this variable to start the name search from a different row:StartRow = 1'Edit this variable to specify in which column the names are:StartColumn = 1'Edit this variable to look for a different string at the beginning of filenamesMatch = "Budget"'Define an FSO objectDim FSO As FileSystemObjectSet FSO = New FileSystemObject'Create a variable to store the folder holding the filesDim FilesDir As Folder'Change the string here to look in a different folder (highly recommended :P)Set FilesDir = FSO.GetFolder("C:\yotam\temp\renamefiles\test\")'Define a counter variable and set it zeroDim i As Integeri = 0'Loop through all of the files in the folderDim CurFile As FileFor Each CurFile In FilesDir.Files'If the file begins with the word "budget" then rename itIf LCase(Left(CurFile.Name, Len(Match))) = LCase(Match) Then'Rename the file to the value in the specified cellCurFile.Move FilesDir + "\" + Me.Cells(StartRow + i, StartColumn).Text'Increment the counter so next time we will use a cell from the next row for namingi = i + 1End IfNextEnd Sub
请问哪位知道要怎么样批量修改word、excel等文档的密码?密码都是...
是可以实现的。
以excel为例,首先将该文件夹里的文档整理一份列表,可以在命令模式下使用dir命令实现。
如键入dir *.xls >list.txt,该目录下的excel文档列表就会生成一个list.txt的文件里。
然后针对文件进行设置密码的操作:第1步:使用Workbooks.Open过程打开文件示例:Workbooks.Open ("C:\MyFolder\MyBook.xls") --打开 C 盘上名为 MyFolder 的文件夹中的 MyBook.xls 工作簿第2步:启动工作簿里的某张要设密码的工作表。
Worksheets(1).Activate 启动工作簿中的第1张工作表。
第3步:撤销工作表保护并取消旧密码ActiveSheet.Unprotect Password:=旧密码第4步: 保护工作表并设置密码 ActiveSheet.Protect Password:=新密码第5步:保存并关闭工作薄ActiveWorkbook.Save ActiveWorkbook.Close对每一个该工作夹中的文件都按第1到5步编程。
更改密码时,只需使用程序编辑器的查找-替换功能更新新旧密码即可。
[求助] :excel vba替换word文档的内容问题!谢谢!
展开全部 这个代码,这么贴出来,可读性太差了吧? 不能替换的原因: excel中的内容和word文档中的内容不能匹配; excel中的字符串含有很多空格,而WORd中没有,多以find不到,因此不能做Replace。
检查下文档就好了。
另外,代码我也帖一个吧。
Sub test1() iweizhi = 14 '生成合同文件 Dim yangbenpath As String yangbenpath = Application.ActiveWorkbook.Path & Sheets("明细").Cells(iweizhi, 23).Value '获取修改文件《测试.doc》的位置 Dim deskpath As String Dim weizhi As Variant Dim WdApp, Wd, i%, Rng Rng = Sheets("明细").Range("W23:X32") Application.ScreenUpdating = False Set WdApp = CreateObject("word.application") WdApp.Visible = True With WdApp.Documents.Open(yangbenpath) WdApp.Visible = True With .Content For i = 1 To UBound(Rng) If .Find.Execute(Rng(i, 1)) Then .Text = Rng(i, 2) End If Next i End With .SaveAs "C:\2.doc" End With' deskpath = CreateObject("WScript.Shell").SpecialFolders("Desktop") & "\" '获取桌面位置路径' Wd.SaveAs deskpath & Sheets("明细").Cells(3, 24).Value & ".doc" '另存到桌面,文件名称为合同名 WdApp.Visible = True Set Wd = Nothing Set WdApp = Nothing Application.ScreenUpdating = TrueEnd Sub...
如何在WORD文件名后面自动添加时间?
展开全部 1、首先需要确保Word的宏安全性设置至少为“中”(工具-宏-安全性-中)。
2、在Word里面新建一个空白文档,给它随便取个名字(比如“我的日志”)保存一下——这个文档就是你以后日志文档的开端了!(注意,如果你用的是Word 2007的话,需要保存为启用宏的文档*.docm)3、键入ALT+F11打开VBA编辑窗口,键入CTRL+R激活左边的“工程 – Project”面板;4、双击工程面板中的“Project (我的日志)\Microsoft Word 对象\ThisDocument”打开代码窗口;粘贴如下代码进去:Option ExplicitDim oLogFileCreator As LogFileCreatorPrivate Sub Document_Open()On Error Resume Next Set oLogFileCreator = New LogFileCreatorSet oLogFileCreator.WordApplication = ApplicationEnd SubPrivate Sub Document_Close()On Error Resume NextSet oLogFileCreator.WordApplication = NothingSet oLogFileCreator = NothingEnd Sub5、选择菜单“插入-类模块”,然后在工程面板里双击这个刚生成的“Project (我的日志)\类模块\类1”,粘贴如下代码进去:Option ExplicitPublic WithEvents WordApplication As Word.Application' 根据需要修改下面引号中的内容作为你日子文件的默认前缀Private Const c_strFileNamePrefix = "我的日志_"Private Sub WordApplication_DocumentBeforeSave(ByVal Doc As Document, SaveAsUI As Boolean, Cancel As Boolean)On Error Resume NextDim fso As ObjectDim strOldName As String, strNewName As String Set fso = CreateObject("Scripting.FileSystemObject") strOldName = ActiveDocument.FullNamestrNewName = fso.BuildPath(fso.GetParentFolderName(strOldName), Format(Now, c_strFileNamePrefix & "yyyy-mm-dd.") & fso.GetExtensionName(strOldName)) If UCase(strNewName) UCase(strOldName) ThenActiveDocument.SaveAs strNewNamefso.DeleteFile strOldNameCancel = TrueEnd If Set fso = NothingEnd Sub6、可以根据需要修改代码中的日志文件名前缀。
默认的日志文件名是类似 “我的日志_2009-09-12”这样的形式。
7、键入F4打开属性面板,在里面把“(名称)”从“类1”改为“LogFileCreator”。
8、键入ALT+Q关闭VBA编辑窗口返回Word文档窗口,保存,关闭。
9、再次重新打开这个文档,如果Word提示是否启用宏,选择启用。
10、现在你的这个自动按日期保存的日志就可以正常运作了。
每次你做完修改后,只要你一保存,它就会根据今天的日期自动把文档保存成类似这样的形式“我的日志_2009-09-12”(前缀“我的日志”可以在上面代码中修改)。
如何用vba修改word页眉中的文本框内容
其实通过宏录制你就完全可以明白,有些问题是可以自己轻易解决的,宏录制是学习vba很重要的一个步骤,自己要多尝试 :如下If ActiveWindow.View.SplitSpecial <> wdPaneNone ThenActiveWindow.Panes(2).CloseEnd IfIf ActiveWindow.ActivePane.View.Type = wdNormalView Or ActiveWindow. _ActivePane.View.Type = wdOutlineView ThenActiveWindow.ActivePane.View.Type = wdPrintViewEnd IfActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeaderActiveDocument.AttachedTemplate.BuildingBlockEntries(" 空白").Insert Where:= _Selection.Range, RichText:=TrueSelection.TypeText Text:="暗暗暗暗"
转载请注明出处51数据库 » vba 修改word文件名
百里轩轅