VBA怎样实现 批量选择word文档读取其文件名并填表 的功能
Sub Test()Dim f, n, x, wb, fNameOn Error Resume NextCells.Clear'打开文件(可多选)f = Application.GetOpenFilename("Word文件,*.docm,", 1, "选择文件", MultiSelect:=True)'遍历每个选择的文件For x = 1 To UBound(f)sFile = f(x)'取文件名,并赋值给单元格n = Len(sFile) - InStrRev(sFile, "\")fName = Right(sFile, n)Cells(x, 1) = Left(fName, InStr(fName, " ") - 1) '取1到空格前的字符'Cells(x, 1) = Left(fname, 9) '取文件名的前9个字符Cells(x, 2) = Mid(fName, InStr(fName, " ") + 1, Len(fName) - InStr(fName, ".") + 1) '取空格后到点之前的字符'Cells(x, 2) = Mid(fName, 10, Len(fName) - InStr(fName, ".") + 1) '从10开始取到点之前的字符Next xEnd Sub
EXCEL VBA 获取打开的word文件名
给你代码吧:Sub a()Dim str As StringFor Each c In ThisDocument.CharactersIf InStr(str, c.Font.Name) = 0 And Len(c.Font.Name) > 0 Thenstr = str & c.Font.Name & ","End IfNextMsgBox UBound(Split(Left(str, Len(str) - 1), ",")) + 1 &; "种字体,分别是" & vbCrLf & Left(str, Len(str) - 1)End Sub
怎样使VBA按文件名顺序批量读取word文档?
操作步骤。
第一,首先将需要批量替换的多个Word文档放在同一文件夹下面。
第二,新建一空白Word文档,右击空白工具栏,单击“控件工具箱”,就可以看到屏幕上调出的控件工具箱。
第三,在控件工具箱上单击“命令按钮”,文档中就放置了一个按钮了。
第四,双击该按钮,进入VB代码编写模式,将以下代码复制进去。
Private Sub CommandButton1_Click()Application.ScreenUpdating = FalseDim myPas As String, myPath As String, i As Integer, myDoc As DocumentWith Application.FileDialog(msoFileDialogFolderPicker).Title = "选择目标文件夹"If .Show = -1 ThenmyPath = .SelectedItems(1)ElseExit SubEnd IfEnd WithmyPas = InputBox("请输入打开密码:")With Application.FileSearch.LookIn = myPath.FileType = msoFileTypeWordDocumentsIf .Execute >0 ThenFor i = 1 To .FoundFiles.CountSet myDoc = Documents.Open(FileName:=.FoundFiles(i), Passworddocument:=myPas)Selection.Find.ClearFormattingSelection.Find.Replacement.ClearFormattingWith Selection.Find.Text = "大家好".Replacement.Text = "你好".Forward = True.Wrap = wdFindAsk.Format = False.MatchCase = False.MatchWholeWord = False.MatchByte = True.MatchWildcards = False.MatchSoundsLike = False.MatchAllWordForms = FalseEnd WithSelection.Find.Execute Replace:=wdReplaceAllmyDoc.SavemyDoc.CloseSet myDoc = NothingNextEnd IfEnd WithApplication.ScreenUpdating = TrueEnd Sub第五,保存上面代码,退出VB编辑模式,返回Word文档界面。
第六,单击选中该按钮,再单击控件工具箱的第一个按钮“退出设计模式”。
第七,进行测试:点击按钮,选择要放置多个WORD文档所在的文件夹,确定后即可完成!注意如果WORD文档没有加密的话,密码项就不填,直接确认。
word vba 如何通过打开对话框获得打开文件名
Declare Function GetOpenFileName Lib "comdlg32.dll" Alias _"GetOpenFileNameA" (pOpenfilename As OPENFILENAME) As LongType OPENFILENAMElStructSize As LonghwndOwner As LonghInstance As LonglpstrFilter As StringlpstrCustomFilter As StringnMaxCustFilter As LongnFilterIndex As LonglpstrFile As StringnMaxFile As LonglpstrFileTitle As StringnMaxFileTitle As LonglpstrInitialDir As StringlpstrTitle As Stringflags As LongnFileOffset As IntegernFileExtension As IntegerlpstrDefExt As StringlCustData As LonglpfnHook As LonglpTemplateName As StringEnd TypeSub t()Dim ofn As OPENFILENAMEDim rtn As String ofn.lStructSize = Len(ofn)ofn.lpstrFilter = "doc Files (*.doc)" & Chr(0) & "*.doc" & Chr(0)ofn.lpstrFile = Space(254)ofn.nMaxFile = 255ofn.lpstrFileTitle = Space(254)ofn.nMaxFileTitle = 255ofn.lpstrInitialDir = "C:"ofn.lpstrTitle = "打开文件"ofn.flags = 6148 rtn = GetOpenFileName(ofn) If rtn >= 1 ThenMsgBox ofn.lpstrFileElseMsgBox "Cancel Was Pressed"End IfEnd Sub
excel vba打开文件 并且获得文件名 保存
提供网上示例代码供参考(此代码的思路是遍历 word 文档中的 Shapes ,缩放到原始图片尺寸,再转粘贴到 Excel 中,借用 Excel 的 ChartObjects 提供的导出功能实现图片原样导出): Sub test() Rem 工具--引用--勾选 Microsoft Excel x.x Object Library.. Dim myshape As Object, ExcelApp As New Excel.Application Dim Excel As Workbook, i%, z% Set Excel = ExcelApp.Workbooks.Add For Each myshape In ActiveDocument.InlineShapes If myshape.Type = 3 Then i = i + 1 myshape.Select Set myshape = myshape.ConvertToShape Rem 以下代码将图片以原始比例展示 With myshape .ScaleHeight 1, True, msoScaleFromMiddle .ScaleWidth 1, True, msoScaleFromMiddle End With Selection.Copy With Excel.ActiveSheet.ChartObjects.Add(0, 0, myshape.Width, myshape.Height).Chart .Paste .Export ActiveDocument.Path & "" & i & ".png" .Parent.Delete End With End If Next Excel.Close False ExcelApp.QuitEnd Sub...
如何用vba读取多个txt文件名和txt文件内容写入excel中?
PathSeparatormyF = Dir(myDir & "*.txt".Resize(,在其VBA中输入ReadTextFiles(););)n = n 1ReDim Preserve a(1 To n)a(n) = xLoopClose #ffmyF = Dir()LoopCells, a(), ff As Integer;)Do While myF <.Range(".Offset(i - 1).ClearWith ThisWorkbook.Worksheets(">, "|",然后运行:Sub ReadTextFiles()Dim n As Long, xDim myF As String;Sheet1")For i = 1 To UBound(a);a1", txt As String, myDir As String, i As LongmyDir = ThisWorkbook.Path & Application, txtx = Split(txt; ""ff = FreeFileOpen myDir & myF For Input As #ffDo While Not EOF(ff)Line Input #ff在多个txt文件的文件夹中新建一个EXCEL...
转载请注明出处51数据库 » vba怎样获取word文件名称