VBA隐藏和显示Word标题中的特定图片

问题描述 投票:0回答:1

我有一个带有锁定页眉和页脚的文档,这些文档由受密码保护的用户表单控制,以防止未经授权的用户更改版本文档编号等。

我现在需要根据客户更改标题中的图片,我希望使用选项组使授权人员能够选择要使用的客户徽标,方法是使所选徽标可见并隐藏其余徽标

但是,我无法单独处理每张图片,它们都被格式化为文本前面的格式,并且通过选择窗格我已将它们重命名为 picCustomer1、picCustomer2 等

我已经尝试了下面的 3 部分代码,但没有一个像在 Excel 中那样按我的预期工作

Sub ShowAllPictures()
  Dim nResponse As Integer
 
  nResponse = MsgBox("Do you want to show all pictures?", 4, "Show pictures")
 
  If nResponse = 6 Then
    With ActiveWindow.View
      If .ShowPicturePlaceHolders = True Or .ShowDrawings = False Then
      .ShowPicturePlaceHolders = False
      .ShowDrawings = True
      Else
        MsgBox ("No picture is hidden.")
      End If
    End With
 End If **'doesnt unhide anything**
End Sub
Sub showhide()
 
  If ActiveDocument.Shapes("picOcado").Visible = False Then **'Item with the specified name not found**
    ActiveDocument.Shapes("picOcado").Visible = True
      Else
    ActiveDocument.Shapes("picOcado").Visible = False
  End If
End Sub
Sub showhidePic()
 ActiveDocument.InlineShapes("picOcado").Height = 1 **' type mismatch error**
 'ActiveDocument.Shapes("picOcado").Height = 1 **'Item with the specified name not found**
 
End Sub

Selection Pane

vba ms-word
1个回答
0
投票

要操作页眉或页脚内容,您需要访问 HeaderFooter 对象。以下示例代码展示了如何显示带书签的内联形状或文本、内联形状和浮动形状:

Sub ShowGraphics()
  Application.ScreenUpdating = False
  Dim aSection As Section
  For Each aSection In ActiveDocument.Sections
    With aSection
      .Headers(wdHeaderFooterPrimary).Range.Bookmarks("Disclaimer").Range.Font.Hidden = False  'shows bookmarked range
      .Headers(wdHeaderFooterFirstPage).Range.InlineShapes(1).Range.Font.Hidden = True  'hides in-line graphics. In-line graphics cannot be named
      .Headers(wdHeaderFooterFirstPage).Shapes("LogoPage1").Visible = True  'shows a named graphic
    End With
  Next aSection
  Application.ScreenUpdating = True
End Sub

在上面的代码中,wdHeaderFooterPrimary将设置所有标题的可见性,除非文档设置为具有不同的首页,或不同的奇数页和偶数页。如果文档有不同的首页,则 wdHeaderFooterFirstPage 将设置首页的页眉,而 wdHeaderFooterPrimary 将设置所有后续页面。

要处理多个图形,我将设置一个 Select Case 语句。这些比使用许多 If-Else 语句更容易阅读。

© www.soinside.com 2019 - 2024. All rights reserved.