用其他图像替换图像并使用VBA在Word中对齐文本框

时间:2018-07-26 21:25:55

标签: vba ms-word word-vba

我一直在尝试使用记录的宏替换大量Word文档上的标题图像并替换页脚图像。但是,当我在新文档上运行宏时,它不会替换图像并从文本框中删除文本。它根本不触及页脚图像。我过去使用过基本的录制宏,但没有替换页脚中的图像或图像。一位朋友提到可能需要VBA插入图像,但是我还不是那种语言的新手。

Sub BrandingUpdateV2()
'
' BrandingUpdateV2 Macro
'
'
    ActiveDocument.Shapes.Range(Array("Text Box 2")).Select
    Selection.TypeBackspace
    Selection.MoveDown Unit:=wdLine, Count:=4
    Selection.MoveDown Unit:=wdLine, Count:=13
    Selection.MoveUp Unit:=wdLine, Count:=3
    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 = wdSeekCurrentPageFooter
    ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
    Selection.MoveDown Unit:=wdLine, Count:=42
    Selection.MoveUp Unit:=wdLine, Count:=10
    Selection.MoveDown Unit:=wdLine, Count:=1
    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 = wdSeekCurrentPageFooter
    Selection.HeaderFooter.Shapes.Range(Array("Group 50")).Select
    Selection.HeaderFooter.Shapes.Range(Array("Slide Number Placeholder 11" _
        )).Select
    ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
End Sub

1 个答案:

答案 0 :(得分:0)

假设图像采用行格式设置,并且分别位于页眉和页脚中,则以下代码应对选定文件夹中的所有文档起作用-只需在'FileName:中添加图像的路径和名称: =“”'变量。

Sub UpdateImages()
Application.ScreenUpdating = False
Dim strFolder As String, strFile As String, strDocNm As String, sWdth As Single
Dim wdDoc As Document, wdHdFt As HeaderFooter, wdRng As Range, wdIshp As InlineShape
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
      With .Sections(1)
        For Each wdHdFt In .Headers
          With wdHdFt
            If .Exists Then
              With .Range
                If .InlineShapes.Count > 0 Then
                  Set wdRng = .InlineShapes(1).Range
                  With .InlineShapes(1)
                    sWdth = .Width
                    .Delete
                  End With
                  Set wdIshp = .InlineShapes.AddPicture(Range:=wdRng, FileName:="")
                  With wdIshp
                    .LockAspectRatio = True
                    .Width = sWdth
                  End With
                End If
              End With
            End If
          End With
        Next
        For Each wdHdFt In .Footers
          With wdHdFt
            If .Exists Then
              With .Range
                If .InlineShapes.Count > 0 Then
                  Set wdRng = .InlineShapes(1).Range
                  With .InlineShapes(1)
                    sWdth = .Width
                    .Delete
                  End With
                  Set wdIshp = .InlineShapes.AddPicture(Range:=wdRng, FileName:="")
                  With wdIshp
                    .LockAspectRatio = True
                    .Width = sWdth
                  End With
                End If
              End With
            End If
          End With
        Next
      End With
      .Close SaveChanges:=True
    End With
  End If
  strFile = Dir()
Wend
Set wdDoc = Nothing
Application.ScreenUpdating = True
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

如您所见,甚至寻址页眉和页脚本身也不是一件容易的事(一个节可以有三个),正如辛迪所说,没有VBA等效于UI的“更改图片”按钮。