用户登录
用户注册

分享至

word页码的vba代码

  • 作者: 你在弄啥咧
  • 来源: 51数据库
  • 2020-04-21

1.VBA怎麽读取word的页码

亲,代码如下:

Private Sub TJYS_Click()

Dim d, p As Integer

Dim f, ff As Object

Set fso = CreateObject("Scripting.FileSystemObject")

Set ff = fso.getfolder(ThisDocument.Path)

d = 0: p = 0

For Each f In ff.Files

If f.Name ThisDocument.Name And Left(f.Name, 2) "~$" And f.Name Like "*.doc*" Then

d = d + 1

Documents.Open FileName:=ThisDocument.Path & "\" & f.Name

p = p + ActiveDocument.BuiltInDocumentProperties(wdPropertyPages)

ActiveDocument.Close False

End If

Next f

MsgBox "总共统计了 " & d &; " 个文件,总页数为 " & p &; "页。", vbOKOnly, "结果"

End Sub

2.批量修改word页脚的宏代码

Sub 插入普通页码()

ActiveDocument.PageSetup.OddAndEvenPagesHeaderFooter = False '关闭奇偶页相同(防止上回启用奇偶页不同后没有关闭此项设置)

With ActiveWindow.ActivePane.View

.SeekView = wdSeekCurrentPageFooter

With Selection

.Expand wdStory

.Font.Size = 14 '字号

.Font.Name = "宋体" '字体

.ParagraphFormat.Alignment = wdAlignParagraphCenter

.TypeText "-"

.Fields.Add .Range, wdFieldPage

.TypeText "-"

.ParagraphFormat.Alignment = wdAlignParagraphRight '页码的位置居右

End With

.SeekView = wdSeekMainDocument '退出页码的编辑(即关闭页眉和页脚工具栏)

End With

End Sub我这儿有个,插入页码的VBA代码,希望能在到你!

3.用VBA在Word中指定页码做一个饼图

目前我想到的方案是利用Word里面的“索引”功能。

1、选中需要提取的文字,执行菜单命令“插入-引用-索引和目录”;

2、在“索引”选项页里面选择单击按钮“标记索引项”;

3、指定“选项”为“当前页”,根据需要可以指定提取之后显示的页码格式为加粗或者倾斜,单击“标记”;

4、此时标记索引项窗口并不会关闭,而且你还能在文档中找到并选中下一个需要提取的文字,重复步骤3;

5、如果没有其它需要提取的文字了,单击“关闭”;

6、在需要插入索引表的地方,执行菜单命令“插入-引用-索引和目录”,根据需要设置索引表的格式(比如分栏、对齐、缩进等风格),然后单击“确定”。

这样所有标记过索引的文字以及它们所在的页码就都提取出来了。

如果需要提取的文字有很多,那么或许也可以用VBA宏来自动标记索引(即相当于自动完成上面的1-5步骤)。但是前提是这些文字必须都有非常特定的格式,即可以保证当前文档中凡是具有这个格式的文本,都是需要提取索引的文本。

如果楼主感兴趣的话,可以补充提问或者和我联系,并请告知Word版本号。

_______________________

补充:

下列VBA代码可以自动为当前文档中的所有红色文字建立索引条目,并在当前光标位置处插入带文字条目以及对应页码的索引表:

Option Explicit

Sub AutoMarkIndexForAllRedText()

Dim oSaveSelection As Range

Set oSaveSelection = Selection.Range

Selection.StartOf wdStory

Selection.Find.ClearFormatting

Selection.Find.Font.Color = wdColorRed ' 在这里可以设置更多的匹配格式,如字体、字号等

While Selection.Find.Execute(FindText:="", Forward:=True, Format:=True)

ActiveDocument.Indexes.MarkEntry(Selection.Range, Selection.Range.Text).Select

Selection.Collapse wdCollapseEnd

Wend

oSaveSelection.Select

ActiveDocument.Indexes.Add Selection.Range

MsgBox "完成!"

End Sub

如果楼主对于这个代码生成的索引表样式不太满意,也没关系。因为现在所有索引条目已经建好,所以只需把自动生成的索引表删除后用Word自己的“插入-引用-索引和目录”命令来重新生成符合自己要求样式的索引表即可。

4.求助VBA代码运行的问题,获取word文档页数

这是因为DIR函数在作怪(你遍历子文件夹时调用带参数的Dir(folder, vbDirectory))

说实话,VBA中的DIR用法我不太喜欢,个人偏好使用fso技术。

建议使用下面方法,遍历子文件夹:

Sub test002(folderspec)

Dim fs, f, f1, sf, fc

Dim mypath As String, fn As String

Dim i As Long, lmyRow As Long

On Error Resume Next

Set fs = CreateObject("Scripting.FileSystemObject")

Set f = fs.GetFolder(folderspec)

Set sf = f.SubFolders

Set fc = f.Files

For Each f1 In sf

fn = f1.Name

mypath = folderspec & fn & "\"

'这里进一步使用 mypath

Next

For Each f1 In fc

fn = f1.Name

Next

End Sub

5.分了好多节的word通过VBA调出某一节的页码范围

sub test ()

istart = InputBox("输入你要打印的起始节")

iEnd = InputBox("输入你要打印的结束节") '为了方便搞了两个inputbox,你可以用一个,方法如下:

'规定一个格式,然后用instr,left,right等得到起始和结束的节数

Dim doc As Document

Dim rngDoc As Range

Set doc = ActiveDocument

Set rngDoc = doc.Range(Start:=doc.Paragraphs(istart).Range.Start, _

End:=doc.Paragraphs(iEnd).Range.End)

rngDoc.Select

doc.PrintOut Range:=wdPrintSelection

Set rngDoc = Nothing

Set doc = Nothing

end sub

6.谁能提供一个用VB编写的向word里面插入页码的软件,或者源码也可

1将光标定位在需要启动的页码榜首的位置。

2,选择“插入” - “BREAK打开”分隔符“对话框中,单击”下一步“单选按钮被选中分隔符类型。分节(节单独的设置页面。状态栏显示部分的顺序)的文件, BR /> 3,选择“视图” - “页眉和页脚”,并将光标定位在页脚中。

4在页眉和页脚“工具栏上,执行以下命令:

①单击”链接“上一步”按钮,断开与前一个的链接。 (?删除右上角前一个一样,“的话,所以这部分可以单独设置页)

②点击”插入页码“按钮,然后单击”页面格式“按钮,打开”页码格式“对话框中。

③在”起始页码“框中,键入适当的起始编号。

④单击”关闭“按钮。

7.word vba 编辑页眉,求代码

其实通过宏录制你就完全可以明白,有些问题是可以自己轻易解决的,宏录制是学习vba很重要的一个步骤,自己要多尝试 :

如下

If ActiveWindow.View.SplitSpecial wdPaneNone Then

ActiveWindow.Panes(2).Close

End If

If ActiveWindow.ActivePane.View.Type = wdNormalView Or ActiveWindow. _

ActivePane.View.Type = wdOutlineView Then

ActiveWindow.ActivePane.View.Type = wdPrintView

End If

ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader

ActiveDocument.AttachedTemplate.BuildingBlockEntries(" 空白").Insert Where:= _

Selection.Range, RichText:=True

Selection.TypeText Text:="暗暗暗暗"

8.word vba怎么查找字符串,并获取所在页码、所在行数

Private Sub CommandButton1_Click()

Dim p, r, s, t

s= "石膏板造型顶"

With Selection.Find

.ClearFormatting

.MatchWholeWord = True

.MatchCase = False

t = .Execute(FindText:=s)

End With

p= Selection.Information(wdActiveEndPageNumber)

r= Selection.Information(wdFirstCharacterLineNumber)

If t Then

MsgBox "成功,已找到“" & s & "”" & vbCrLf & _

"页码:"& p & vbCrLf &; "行数:" & r, vbOKOnly, _

"成功"

Else

MsgBox "很遗憾,没有找到“" & s & "”", vbOKOnly, _

"遗憾"

End If

End Sub 写代码很辛苦,好心的楼主请 救济 点儿 悬赏分

9.word vba 怎么获取当前光标所在页码,并获取该页有多少行

Sub Macro2()

Dim r, p, r0, p0, rr, pp

p= Selection.Information(wdActiveEndPageNumber) '当前页码

r= Selection.Information(wdFirstCharacterLineNumber) '当前行

'数行数

p0 = p

rr = r

Do

Selection.MoveDown Unit:=wdLine, Count:=1

pp = Selection.Information(wdActiveEndPageNumber)

r0 = Selection.Information(wdFirstCharacterLineNumber)

If pp > p Then

'退回原处

Selection.MoveUp Unit:=wdLine, Count:=(rr - r + 1)

Exit Do

End If

If rr = r0 Then

'退回原处

Selection.MoveUp Unit:=wdLine, Count:=(rr - r)

Exit Do

End If

rr = r0

Loop

MsgBox "当前页码:" & p & vbCrLf &; "本页总行数:" & rr

End Sub 申请加悬赏分!!!

转载请注明出处51数据库 » word页码的vba代码

软件
前端设计
程序设计
Java相关