|
作者:杨洋
链接:https://www.zhihu.com/question/64821272/answer/224650964
来源:知乎
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
Option Explicit
Sub insertTextBoxes()
Dim tbox As Object, file As Object, appWord As Object, appExl As Object, appPPT As Object
Dim fName As String, fPath As String, fExt As String
Set appWord = CreateObject("word.application")
Set appExl = CreateObject("excel.application")
Set appPPT = CreateObject("powerpoint.application")
'将待处理文件放置于同一个目录下,将绝对路径写入fPath变量。
fPath = "d:\vbademo\a\"
fName = Dir(fPath)
Do While fName <> ""
If InStr(fName, ".") > 0 Then
fExt = Left(LCase(Mid(fName, InStrRev(fName, ".") + 1)), 3)
'如果扩展名前三位是doc,则按word处理,可酌情修改word文本框各种格式参数。
If fExt = "doc" Then
Set file = appWord.documents.Open(fPath & fName)
'添加文本框,后四个参数为该文本框的left/top/width/height,
'excel与ppt中该方法的参数含义相同。
Set tbox = file.Shapes.AddTextbox(1, 50, 120, 100, 50)
With tbox.Line
.ForeColor.RGB = RGB(255, 0, 0)
.Weight = 2
End With
tbox.TextFrame.TextRange.Text = "2017/9/2" & Chr$(10) & "Checked"
file.Save
file.Close
'如果扩展名前三位是xls,则按excel处理,可酌情修改excel文本框各种格式参数
ElseIf fExt = "xls" Then
Set file = appExl.Workbooks.Open(fPath & fName)
Set tbox = file.Worksheets(1).Shapes.AddTextbox(1, 50, 120, 100, 50)
With tbox.Line
.ForeColor.RGB = RGB(255, 0, 0)
.Weight = 2
End With
tbox.TextFrame2.TextRange.Characters.Text = "2017/9/2" & Chr$(10) & "Checked"
file.Save
file.Close
'如果扩展名前三位是ppt,则按powerpoint处理,可酌情修改ppt文本框各种格式参数
ElseIf fExt = "ppt" Then
Set file = appPPT.presentations.Open(fPath & fName)
'PPT中允许没有幻灯片,所以需要特别判断
If file.slides.Count > 0 Then
Set tbox = file.slides(1).Shapes.AddTextbox(1, 50, 120, 100, 50)
With tbox.Line
.Weight = 2
.ForeColor.RGB = RGB(255, 0, 0)
End With
tbox.TextFrame.TextRange.Characters.Text = "2017/9/2" & Chr$(10) & "Checked"
End If
file.Save
file.Close
End If
End If
Set file = Nothing
fName = Dir
Loop
Set appWord = Nothing
Set appExl = Nothing
Set appPPT = Nothing
End Sub
可以用这个改一下 |
|