去除WORD文档首行空格VBA代码
我把我以前用excel做的类似的module改了下发给你你可能需要在工具引用那里添加 microsoft word 14.0 object library以及 microsoft scripting runtime。
你可以改下用word vba,然后txt那部分的处理用别的方法做就不用添加引用了Sub CountTxtWord()Dim fName As String, fPath As StringDim s As StringDim i As IntegerDim WrdApp As ObjectSet WrdApp = CreateObject("word.application")ActiveSheet.Cells.ClearfPath = "D:\test6\"fName = Dir(fPath)i = 2Do Until fName = ""If Right(fName, 4) = ".txt" ThenWith New Scripting.FileSystemObjectWith .OpenTextFile(fPath & fName)s = .ReadAlls = Replace(s, " ", "")Cells(i, 1) = fPath & fNameCells(i, 2) = Len(s)i = i + 1End WithEnd WithElseWrdApp.Documents.Open (fPath & fName)s = WrdApp.ActiveDocument.Range.TextWrdApp.ActiveDocument.Close Falses = Replace(s, " ", "")Cells(i, 1) = fPath & fNameCells(i, 2) = Len(s)i = i + 1End IffName = DirLoopColumns(1).AutoFitWrdApp.QuitEnd Sub
关于Word中 的VBA 编程
解决你的“顺便问下".....Sub Macro1()Dim strText As StringstrText = "建立反对撒客里空的龙卷风"Selection.TypeText strTextSelection.MoveStart Unit:=wdLine, Count:=-1Selection.MoveEnd Unit:=wdCharacter, Count:=-1 * (Len(Selection.Text) - 4)Selection.MoveStart Unit:=wdCharacter, Count:=3Selection.Font.Bold = TrueMsgBox Selection.TextEnd Sub
如何使用VBA判断一个excel单元格是空单元格
网上有现成的代码:========要判断 以上表格中如果有空时,则弹出对话框页面=========='当打开Word时弹出对话框 要用户输入内容 Private Sub Document_Open() Dim flag As Boolean flag = True With ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Range.Tables(1).Range For i = 2 To 9 Step 1 If Len(.Cells(i).Range.Text) = 2 Then flag = False End If Next If flag = False Then AddHeader.Show End If End With End Sub=====点确定后将页面对话框中的值 插入到word 的页眉表格中==== Private Sub CommandButton1_Click() With ActiveDocument.Sections(1)'Cell(4, 2).Range.InsertBefore (TextBox4.value) ' .Headers(wdHeaderFooterPrimary).Range.Tables(1).Cell(1, 2).Range.InsertAfter ("aaa") .Headers(wdHeaderFooterPrimary).Range.Tables(1).Cell(1, 3).Range.Delete .Headers(wdHeaderFooterPrimary).Range.Tables(1).Cell(1, 3).Range.InsertAfter (TxtFileName.Value) .Headers(wdHeaderFooterPrimary).Range.Tables(1).Cell(2, 2).Range.Delete .Headers(wdHeaderFooterPrimary).Range.Tables(1).Cell(2, 2).Range.InsertAfter (TxtSecrecy.Value) .Headers(wdHeaderFooterPrimary).Range.Tables(1).Cell(2, 4).Range.Delete .Headers(wdHeaderFooterPrimary).Range.Tables(1).Cell(2, 4).Range.InsertAfter (TxtRev.Value) .Headers(wdHeaderFooterPrimary).Range.Tables(1).Cell(2, 6).Range.Delete .Headers(wdHeaderFooterPrimary).Range.Tables(1).Cell(2, 6).Range.InsertAfter (TxtID.Value) '页尾的做法' .Footers(wdHeaderFooterPrimary).Range.Text = "Copyright BYD Company Limited" End With AddHeader.Hide End Sub========提问 ================= 比如说sheet1 中A1:A10不为空,用代码判断不为空后,光标移向A11,并且A11=11,B11=22 再录入数据后光标又光标移向A12,并且A12=22,B12=23,依此类推========解决方法:=============================== Sub Text() If IsEmpty([A1]) Then [A1] = "你的数据" Else [A65536].End(xlUp).Offset(1, 0).Value = "你的数据" End If End Sub ========提问 ================= 判断word中表格是否为空========解决方法:==============================='a.Cells.Count=41,46,41+5*7 Dim list%'你的要求可能是这样 With ActiveDocument.Tables(1).Range For i = 41 To 41 + 5 * 7 Step 5 '测得第一项的后面对应的是第41个单元格,相差是5。
所以,step=5 list = list + 1 '利用list来得到是第几项没有加项 If Len(.Cells(i).Range.Text) = 2 Then '因为类回车加+竖线加在一起是2个长度。
所以。
。
这样判断 'n = .Cells(i - 4).Range.Text '取得单元格的内容+回车+竖线 'n = Mid(n, 1, Len(n) - 2) '去掉回车+竖线的,即我们所看到的内容 MsgBox "提示:没有给第" & list & "项打分!", vbOKOnly, "www.excelhome.net" .Cells(i).Range.Select '选中所在的单元格 Exit Sub End If Next End With
如何用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文档中“大家好”已被替换为“你好”了。
转载请注明出处51数据库 » vba word 空白