一、VBA编写代码获得word每一行的单词,如何做才对
设文档共20行,500个字hs代表文档的行数,从第1行到第20行 hs = 1 To ActiveDocument.BuiltInDocumentProperties("Number of lines").Value在写入时,Print #1, ActiveDocument.Words(hs).text,表示只写到第20个字就结结束了,而不是第20行。
如果要把所有的字写到a.csd中,则"Number of lines"可改为Number of words,如果只写每行的第一个单词,则ActiveDocument.Words(hs)改为ActiveDocument.lines(hs).Words(1)(查一下有没有lines这个对象)。
二、WordVBA复制某一行,将这一行的内容作为文件名保存
试试下面的代码,在网上找的: Sub Word文件改名() Application.ScreenUpdating = False Down = MsgBox("请保证每个Word文档的第三行没有空行" & vbCrLf & "否则无法重命名,运行宏会出错" & vbCrLf & vbCrLf & "也不能出现第三行相同相等字段内容" & vbCrLf & "否则只能重命名到第一个打开的同名文档", vbQuestion + vbYesNo, "★☆ 重命名时请注意 (1) ☆★")If Down = vbNo Then Exit Sub End If '选择“是”,执行下列操作 Dim MyPath As String, i As Integer, myDoc As Document With Application.FileDialog(msoFileDialogFolderPicker) .Title = "选择要处理目标文件夹" & "——(给文档进行重命名)" If .Show = -1 Then MyPath = .SelectedItems(1) Else Exit Sub End If End With With Application.FileSearch .LookIn = MyPath .FileType = msoFileTypeWordDocuments If .Execute > 0 Then For i = 1 To .FoundFiles.Count Set myDoc = Documents.Open(FileName:=.FoundFiles(i)) ' B可以替换的宏' 以下是处理格式所录制的宏,可根据所需录制Dim myS, myP As String myP = ActiveDocument.Path Selection.GoTo What:=wdGoToLine, Which:=wdGoToAbsolute, Count:=3 '修改数值可以以不同的行号命名 Selection.EndKey wdLine Selection.HomeKey wdLine, wdExtend myS = Selection.Range.Text ActiveDocument.SaveAs FileName:=myP & "\" & myS & ".doc"' 以上可以换成是你自己录制的宏' C公共部分的代码Application.DisplayAlerts = False '强制执行“是”'ActiveDocument.Saved = True'强制执行“否”ActiveDocument.Close '退出 Next End If End With Application.ScreenUpdating = True MsgBox "所选文件夹内的Word已经重命名完毕!!!" & vbCrLf & "" & vbCrLf & "但不能对第二个同名文件进行重命名" & vbCrLf & "" & vbCrLf & "修改日期为当前时间的即是重命名后的文档", 64, "☆★批量处理完毕★☆"ThisDocument.Application.QuitEnd Sub。
三、如何在word中使用VBA得出每一页都多少行文字啊
Sub LinesOfPage() '方法很简单,就是数 Dim PageNo As Integer, Lines As Integer, MovedLines As Integer '先确定现在的页码 PageNo = Selection.Information(wdActiveEndAdjustedPageNumber) '行计数清零 Lines = 0 Do '向上挪一行,如果到头了没挪动或挪到上一页去了就停止 If Selection.Move(wdLine, -1) = 0 Or PageNo <> Selection.Information(wdActiveEndAdjustedPageNumber) Then Exit Do '还在同一页行数加1,继续挪 Lines = Lines + 1 Loop '向下挪回开始的位置 Selection.Move wdLine, Lines Do '现在开始向下挪,,如果到尾了没挪动或挪到下一页去了就停止 If Selection.Move(wdLine, 1) = 0 Or PageNo <> Selection.Information(wdActiveEndAdjustedPageNumber) Then Exit Do '还在同一页行数加1,继续挪 Lines = Lines + 1 Loop '最后Lines就是这页的行数End Sub。
四、如何通过VBA 知道word中 某一行的内容
Sub PageLine()
nPage = InputBox(Chr(13) &; "输入要查看的页数。", "文本的页数", 13)
nLine = InputBox(Chr(13) &; "输入要查看的行数。", "查看的行数", 7)
Selection.GoTo What:=wdGoToPage, Which:=wdGoToNext, Name:=nPage
Selection.GoTo What:=wdGoToLine, Which:=wdGoToNext, Name:=nLine - 1
Selection.Extend
Selection.EndKey Unit:=wdLine
Selection.EscapeKey
MsgBox Selection.Range
End Sub
转载请注明出处51数据库 » wordvba新增一行
梅耶一罗斯柴尔德