VBA在Word中的应用

VBA即Visual Basic for Applications,使VB的一种宏语言。这种语言在Office办公软件中均可使用。这里主要写在Word撰写文档时如何使用这种语言提高工作效率。
1. 批量替换文档中的内容,如公司名称
在Word文档中先将需要替换的内容用统一的区别于正常文本的标志代替,如公司名称可以写为GSMC,然后使用VBA中的InputBox让用户从提示框中输入信息,并将信息的值传给变量。通过Selection函数的Find方法可以实现批量替换。
2. 自动另存为,并按照规则设置文件名
ChangeFileOpenDirectory 函数可以改变文档的当前目录。如ChangeFileOpenDirectory “C:UsersAdministratorDesktop”可以将当前目录设置到桌面。然后可以调用Date函数获取系统时间等方法,按照规则设置文件名。ActiveDocument函数SaveAs2方法可以将文件另存。

以下是在写测试报告是用到的一段VBA程序,可以更简单便捷的完成报告的撰写。

Attribute VB_Name = "模块1"
Public Sub PentestReport()
Dim Company As String
Dim URL As String
Dim AppName As String
Dim BeginDate As String
Dim EndDate As String
NowDate = Format$(Now, "Long Date")
Company = InputBox("请输入客户名称", "客户信息")
AppName = InputBox("请输入系统名称", "系统信息")
URL = InputBox("请输入系统URL", "系统信息")
BeginDate = InputBox("请输入测试开始日期", "测试日期", NowDate)
EndDate = InputBox("请输入测试结束日期", "测试日期", NowDate)

‘客户名称替换
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = “KHMC”
.Replacement.Text = Company
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = True
.MatchWholeWord = False
.MatchByte = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll

‘系统URL替换
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = “XTURL”
.Replacement.Text = URL
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = True
.MatchWholeWord = False
.MatchByte = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll

‘系统名称替换
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = “XTMC”
.Replacement.Text = AppName
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = True
.MatchWholeWord = False
.MatchByte = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll

‘日期的替换
If EndDate = BeginDate Then
BeginDate = “”
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = “KSRQ1”
.Replacement.Text = EndDate
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = True
.MatchWholeWord = False
.MatchByte = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
Else
BeginDate = BeginDate + “-“
With Selection.Find
.Text = “KSRQ1”
.Replacement.Text = BeginDate
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = True
.MatchWholeWord = False
.MatchByte = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
End If
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = “JSRQ”
.Replacement.Text = EndDate
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = True
.MatchWholeWord = False
.MatchByte = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = “KSRQ”
.Replacement.Text = BeginDate
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = True
.MatchWholeWord = False
.MatchByte = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll

‘更新所有域
Selection.WholeStory
Selection.Fields.Update
Selection.HomeKey Unit:=wdLine

‘另存为
SaveDate = Format$(Now, “yyyymmdd”) + “.”
ChangeFileOpenDirectory “C:UsersAdministratorDesktop”
FileName = SaveDate + AppName + “安全渗透测试报告V1.0.docx”

ActiveDocument.SaveAs2 FileName:=FileName, FileFormat:= _
wdFormatXMLDocument, LockComments:=False, Password:=””, AddToRecentFiles _
:=True, WritePassword:=””, ReadOnlyRecommended:=False, EmbedTrueTypeFonts _
:=False, SaveNativePictureFormat:=False, SaveFormsData:=False, _
SaveAsAOCELetter:=False, CompatibilityMode:=15
FinalPath = “已另存为” + “C:UsersAdministratorDesktop” + FileName
MsgBox FinalPath

发表评论

Fill in your details below or click an icon to log in:

WordPress.com 徽标

You are commenting using your WordPress.com account. Log Out /  更改 )

Google photo

You are commenting using your Google account. Log Out /  更改 )

Twitter picture

You are commenting using your Twitter account. Log Out /  更改 )

Facebook photo

You are commenting using your Facebook account. Log Out /  更改 )

Connecting to %s