我想在 Word VBA 中重复将内容从 Excel 复制到 Word。
目标:我在 Excel 工作簿中的 C 列中有一个大约 250 个单元格长的范围,这是一个图形标题列表。我想将这些标题粘贴到 Word 中,作为“标题”(同时留出空间稍后放置数字,在它们上放置一致的源标题等)
我为一个单元格编写了代码。我想循环到下一个单元格并插入一个带有该新标题的新标题,直到输入所有 250 个不同的标题。
这是代码。我让它运行一个函数,该函数运行一个子程序以从一个单元格中获取标题。
Sub Macro123()
Selection.InsertCaption Label:="Figure", TitleAutoText:="InsertCaption2", _
Title:=".", Position:=wdCaptionPositionBelow, ExcludeLabel:=0
Selection.TypeText Text:=TitleDrop
Selection.Style = ActiveDocument.Styles("EcoCaption")
Selection.TypeParagraph
Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter
Selection.TypeParagraph
Selection.TypeText Text:="Source: Current study, based off landings data from CDFW."
Selection.Style = ActiveDocument.Styles("EcoSource")
Selection.TypeParagraph
Selection.TypeParagraph
Selection.TypeParagraph
End Sub
-----------
Function TitleDrop()
GetExcelTitles
Selection.PasteAndFormat (wdFormatPlainText)
End Function
-----------------
Sub GetExcelTitles()
Dim ObjXL As Object, xlWkBk
Dim strTitleName As String
On Error Resume Next
Set ObjXL = GetObject(, "Excel.Application")
If Err.Number <> 0 Then
MsgBox "No Excel Files are open (Excel is not running)"
Exit Sub
End If
For Each xlWkBk In ObjXL.Workbooks
If xlWkBk.Name = "130611 Figure Lists.xlsx" Then
xlWkBk.Sheets("Figuresonly").Range("C6").Select
xlWkBk.Sheets("Figuresonly").Range("C6").Copy
Exit For
End If
Next
Set ObjXL = Nothing
End Sub