VBA, or Visual Basic for Applications, is a macro language based on VB. It can be used across Office applications. Here I describe how to use it in Word documents to improve efficiency.
1. Batch-replace document content, such as a company name
In the Word document, first replace the content to be changed with a marker distinct from normal text; for example, write the company name as GSMC. Then use VBA’s InputBox to let the user enter information in a prompt and pass its value to a variable. Batch replacement can be done with the Selection function’s Find method.
2. Automatically save a copy and set the filename according to a rule
The ChangeFileOpenDirectory function changes the document’s current directory. For example, ChangeFileOpenDirectory “C:UsersAdministratorDesktop” sets the current directory to the desktop. You can then call the Date function to obtain the system time and set the filename according to a rule. ActiveDocument.SaveAs2 saves a copy of the document.
The following VBA program was used when writing a test report and makes report writing simpler and more convenient.
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