【问题标题】:MS Word Caption with the Image Name带有图像名称的 MS Word 标题
【发布时间】:2019-10-26 04:07:09
【问题描述】:

下面的代码就像一个魅力。它允许用户选择一个包含 .jpgs 和其他图像类型的文件夹到每页 2 张图像中。当前代码只是将图像标题为“图片”。我需要帮助的是将图像名称作为标题减去.jpg。任何方向都会很棒:

Sub AddPic()
Dim fd As FileDialog
Dim oTbl As Table
Dim oILS As InlineShape
Dim vrtSelectedItem As Variant
  '''''''''''''''
  'Add a 1 row 2 column table to take the images
  '''''''''''''''
Set oTbl = Selection.Tables.Add(Selection.Range, 4, 1)
With oTbl
     .AutoFitBehavior (wdAutoFitWindow)
End With
  '''''''''''''''
Set fda = Application.FileDialog(msoFileDialogFilePicker)
With fda
     .Title = "Select image files and click OK"
     .Filters.Add "Images", "*.gif; *.jpg; *.jpeg; *.bmp; *.tif; *.png"
     .FilterIndex = 2
     If .Show = -1 Then
         CaptionLabels.Add Name:="Picture"
         For Each vrtSelectedItem In .SelectedItems
             With Selection
                 Set oILS = .InlineShapes.AddPicture(FileName:= _
                 vrtSelectedItem, LinkToFile:=False, SaveWithDocument:=True, _
                 Range:=Selection.Range)
                 oILS.Range.InsertCaption Label:="Picture", TitleAutoText:="", Title:="", _
                 Position:=wdCaptionPositionBelow, ExcludeLabel:=0
                 .MoveRight wdCell, 1
             End With
         Next vrtSelectedItem
If Len(oTbl.Rows.Last.Cells(1).Range) = 2 Then oTbl.Rows.Last.Delete
Set fd = Nothing
End If
End With

  '''''''''''''''
For Each pic In ActiveDocument.InlineShapes
     With pic
         .LockAspectRatio = msoFalse
         If .Width > .Height Then ' horizontal
             .Width = InchesToPoints(5.5)
             .Height = InchesToPoints(3.66)

         Else  ' vertical
             .Width = InchesToPoints(5.5)
         End If
     End With
     Next
  '''''''''''''''
Selection.WholeStory
Selection.Font.Bold = wdToggle
Selection.Font.Bold = wdToggle
Selection.Font.Color = wdColorBlack
  '''''''''''''''
End Sub

【问题讨论】:

标签: vba ms-word


【解决方案1】:

看来vrtSelectedItem 提供了所需的信息,所以唯一的问题是切断文件扩展名。

这可以通过字符串操作来完成。在下面的代码 sn-p 中,取自问题,确定了文件名中. 的位置,以及文件名的长度。然后使用Mid 函数提取该点左侧的文本。

Dim dotPos as long, lenName as Long
Dim capt as String

 For Each vrtSelectedItem In .SelectedItems
    dotPos = Instr(vrtSelectedItem, ".")
    lenName = Len(vrtSelectedItem)
    capt = Mid(vrtSelectedItem, lenName + (dotPos - 1 - lenName ))
     With Selection
         Set oILS = .InlineShapes.AddPicture(FileName:= _
           vrtSelectedItem, LinkToFile:=False, SaveWithDocument:=True, _
           Range:=Selection.Range)
         oILS.Range.InsertCaption Label:="Picture", TitleAutoText:="", Title:=capt, _
           Position:=wdCaptionPositionBelow, ExcludeLabel:=0
         .MoveRight wdCell, 1
     End With
 Next vrtSelectedItem

【讨论】:

  • @CindyMeister 代码运行良好,我唯一需要的更改是如何关闭自动编号以及如何切断 .jpg。上面是当前代码的全部内容。
  • @Zompac 您能否回滚您的编辑并将新要求作为新问题发布? Stack Overflow 不是一个在持续讨论中开发一个主题的论坛。在给出答案后更改问题违反了网站指南。 FWIW 我的答案中的代码应该已经切断了文件扩展名。如果不是,请包括文件名示例。此外,使用 Word 的内置功能插入标题会自动包括编号。因此,还请包括确切的要求集,并举例说明事物的外观/排列方式
  • @CindyMeister 代码运行良好,我唯一需要的更改是如何关闭自动编号以及如何切断.jpg。(扩展名)当前代码的全部内容在上面.非常感谢您的帮助。
  • @Zomjac 请根据我之前评论中概述的信息针对新要求提出一个新问题。 不可能使用原始问答中的方法关闭自动编号。这必须是一个新问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多