Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vba 如何使用宏/按钮在MS Word 2013中选择不同的标题图像_Vba_Templates_Ms Word - Fatal编程技术网

Vba 如何使用宏/按钮在MS Word 2013中选择不同的标题图像

Vba 如何使用宏/按钮在MS Word 2013中选择不同的标题图像,vba,templates,ms-word,Vba,Templates,Ms Word,我想创建一个Word固定模板,能够循环浏览其标题中不同颜色的徽标。我的公司使用五种不同颜色的徽标,我想创建一个带有按钮的单一模板,该按钮允许我在每次使用此模板创建新文档时循环浏览不同颜色的徽标。也许用一点VBA就能做到这一点吗 编辑: 根据Olle Sjögren的回答,我想出了以下工作脚本: Option Explicit Public imgCounter As Integer Sub cycle_logos() Dim I As Variant Dim logoColors(4) As

我想创建一个Word固定模板,能够循环浏览其标题中不同颜色的徽标。我的公司使用五种不同颜色的徽标,我想创建一个带有按钮的单一模板,该按钮允许我在每次使用此模板创建新文档时循环浏览不同颜色的徽标。也许用一点VBA就能做到这一点吗

编辑: 根据Olle Sjögren的回答,我想出了以下工作脚本:

Option Explicit
Public imgCounter As Integer

Sub cycle_logos()

Dim I As Variant
Dim logoColors(4) As String
logoColors(0) = "logo_magenta.png"
logoColors(1) = "logo_teal.png"
logoColors(2) = "logo_orange.png"
logoColors(3) = "logo_red.png"
logoColors(4) = "logo_grayscale.png"

For Each I In logoColors
    ActiveDocument.StoryRanges(wdPrimaryHeaderStory).ShapeRange.Item(I).Visible = msoFalse
Next I

imgCounter = imgCounter + 1
If imgCounter = 5 Then imgCounter = 0
ActiveDocument.StoryRanges(wdPrimaryHeaderStory).ShapeRange.Item(logoColors(imgCounter)).Visible = msoTrue

End Sub
值得一提的是,我是如何想出图像名称的,因为我并没有找到一种从单词内部做到这一点的方法。我将模板扩展名重命名为zip并解压缩。在提取的文件中,我打开了word\header2.xml,这可能会有所不同,具体取决于文档中的位置和编辑的包含图片名称的节点,例如:

成为:

<wp:docPr name="logo_magenta.png" id="1"/>

然后,我用编辑过的版本替换了ZIP中的XML文件,并将扩展名改回dotm。

如前所述,有几种方法可以做到这一点。我建议将图像存储在模板之外,否则基于模板的所有文档将包括所有徽标图像,即使它们没有显示,也会使文档比需要的大。这种方法使得安装模板更加困难,因为您必须将图像以及模板文件复制到客户端

要回答有关标题中图像寻址的问题,可以通过正确的故事范围对象进行寻址。在我的示例中,我假设它们位于主标题中。要区分不同的图像,可以使用项目集合中的Name属性或索引:

ThisDocument.StoryRanges(wdPrimaryHeaderStory).ShapeRange.Item("Picture 1").Visible = msoTrue
ThisDocument.StoryRanges(wdPrimaryHeaderStory).ShapeRange.Item(1).Visible = msoFalse

可能有很多方法可以做到这一点,到目前为止你都尝试了什么?我将所有五幅图像都放在标题中,因为它不仅与颜色有关,而且更容易用这种方式解释。我知道我可以用VBA控制可见性,但我不知道如何处理这些图像。谢谢你给我指明了正确的方向。我知道你提到的膨胀问题,但是在模板中存储所有徽标图像更方便,最终产品几乎总是PDF文件。太棒了!关于您对图像名称问题的更新:名称属性也可以在VBA中通过宏或即时窗口进行更新:ThisDocument.StoryRangeswdPrimaryHeaderStory.ShaperAge.ItemPicture 1.Name=logo_magenta.png。
ThisDocument.StoryRanges(wdPrimaryHeaderStory).ShapeRange.Item("Picture 1").Visible = msoTrue
ThisDocument.StoryRanges(wdPrimaryHeaderStory).ShapeRange.Item(1).Visible = msoFalse