vbscript 循环文档替换图像

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了vbscript 循环文档替换图像相关的知识,希望对你有一定的参考价值。

Sub UpdateDocuments()
Application.ScreenUpdating = False
Dim strFolder As String, strFile As String, strDocNm As String
Dim wdDoc As Document, Rng As Range, Sctn As Section, HdFt As HeaderFooter
strDocNm = ActiveDocument.FullName
strFolder = GetFolder
If strFolder = "" Then Exit Sub
strFile = Dir(strFolder & "\*.doc", vbNormal)
While strFile <> ""
  If strFolder & "\" & strFile <> strDocNm Then
    Set wdDoc = Documents.Open(FileName:=strFolder & "\" & strFile, _
    AddToRecentFiles:=False, Visible:=False)
    With wdDoc
    For Each Sctn In .Sections
        For Each HdFt In Sctn.Headers
        If Sctn.Index = 1 Then
            Set Rng = HdFt.Range
            Call ReplaceShape(Rng)
        ElseIf HdFt.LinkToPrevious = False Then
            Set Rng = HdFt.Range
            Call ReplaceShape(Rng)
        End If
        Next
    Next
    .Close SaveChanges:=True
    End With
  End If
  strFile = Dir()
Wend
Set wdDoc = Nothing
Application.ScreenUpdating = True
End Sub
Sub ReplaceShape(Rng As Range)
Dim sngWdth As Single, SngHght As Single
With Rng
  If .InlineShapes.Count > 0 Then
    Set Rng = .InlineShapes(1).Range
    With .InlineShapes(1)
    sngWdth = .Width
    SngHght = .Height
    .Delete
    End With
    .InlineShapes.AddPicture FileName:="path\to\image.jpg", LinkToFile:=False, SaveWithDocument:=True, Range:=Rng
    With .InlineShapes(1)
    .Width = sngWdth
    .Height = SngHght
    End With
  End If
End With
End Sub
Function GetFolder() As String
Dim oFolder As Object
GetFolder = ""
Set oFolder = CreateObject("Shell.Application").BrowseForFolder(0, "Choose a folder", 0)
If (Not oFolder Is Nothing) Then GetFolder = oFolder.Items.Item.Path
Set oFolder = Nothing
End Function

Sub Update_docx_File_Brand()
Application.ScreenUpdating = False
Dim strFolder As String, strFile As String, wdDoc
strFolder = GetFolder
If strFolder = "" Then Exit Sub
strFile = Dir(strFolder & "\*.docx", vbNormal)
While strFile <> ""
  Set wdDoc = Documents.Open(FileName:=strFolder & "\" & strFile, AddToRecentFiles:=False, Visible:=False)
  With wdDoc.Range.Find
    .ClearFormatting
    .Text = "text"
    .Replacement.Text = "new text"
    .Forward = True
    .Wrap = wdFindContinue
    .Format = False
    .MatchCase = True
    .MatchWholeWord = True
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
    .Execute Replace:=wdReplaceAll
  End With
  RTFDoc.Close SaveChanges:=True
  strFile = Dir()
Wend
Set RTFDoc = Nothing
Application.ScreenUpdating = True
End Sub


Sub process()

Call UpdateDocuments
Call Update_docx_File_Brand

End Sub

以上是关于vbscript 循环文档替换图像的主要内容,如果未能解决你的问题,请参考以下文章

VBscript 替换文本但添加空行

Excel自动替换VBScript

vbscript 替换泰字符

vbscript 字符串查找和替换

vbscript 报表:复制或替换

在 WebView 中加载页面之前,用本地 Android 资源替换 HTML 文档的图像