从Excel导出到单词文档并插入标题 [英] Export from Excel to a word Document and insert a header in word
问题描述
我有以下代码:
Sub CreateRapport()
Dim wdApp As Object
Dim wd As Object
On Error Resume Next
Set wdApp = GetObject(, "Word.Application")
If Err.Number <> 0 Then
Set wdApp = CreateObject("Word.Application")
End If
On Error GoTo 0
Set wd = wdApp.Documents.Add
wdApp.Visible = True
Sheets("Rapport").Activate
Set Rng = ThisWorkbook.ActiveSheet.Range("A1:E76")
Rng.Copy
With wd.Range
.Collapse Direction:=0 'Slutet av dokumentet
.InsertParagraphAfter 'Lägg till rad
.Collapse Direction:=0 'Slutet av dokumentet
.PasteSpecial False, False, True 'Pasta som Enhanced Metafile
End With
End Sub
它的作用是创建一个单词文档,其数据范围为A1:E76
What it does is that it creates a word document with the data from range A1:E76
我想在包含图片和名称的这个单词文档中插入一个标题。此标题中的名称位于同一表格的单元格A1中。
I want to insert a header in this word document that contains a picture and a name. The name in this header is in cell A1 in the same sheet.
如果有人可以帮助我,将非常感谢。谢谢。
Would be very thankful if anyone could help me with this. Thank you.
推荐答案
如何将单元格A1的文本添加到文档中:
How to add text from cell A1 to the document:
wdApp.Selection.TypeText ThisWorkbook.ActiveSheet.Range("A1").Text
如何将当前文本转换成标题:
How to turn the current text into a header:
'***** Word VBA constant wdStyleHeading1 = -2
wdApp.Selection.Style = -2
如何添加一个图像:
wdApp.Selection.InlineShapes.AddPicture Filename:="PATH_TO_IMAGE", LinkToFile:=False, SaveWithDocument:=True
如果将上述代码放在一起,直接添加到 wdApp之后.Visible = True
您将在结尾处获得一张图片,但我无法从您的问题中得知您的文档是如何看待的。
If you put the above code together and add it directly after the line wdApp.Visible = True
you will get a header with an image at the end, but I can't tell from your question exactly how you want the document to look.
编辑
显示当前标题的代码:
'***** Word VBA constant wdSeekCurrentPageHeader = 9
wdApp.ActiveWindow.ActivePane.View.SeekView = 9
显示普通视图:
'***** Word VBA constant wdSeekMainDocument = 0
wdApp.ActiveWindow.ActivePane.View.SeekView = 0
将它们放在一起,粘贴在行 wdApp.Visible之后= True
,这次没有设置样式:
Putting it all together, paste this after the line wdApp.Visible = True
, this time without setting the style:
wdApp.ActiveWindow.ActivePane.View.SeekView = 9
wdApp.Selection.TypeText ThisWorkbook.ActiveSheet.Range("A1").Text
wdApp.Selection.InlineShapes.AddPicture Filename:="PATH_TO_IMAGE", LinkToFile:=False, SaveWithDocument:=True
wdApp.ActiveWindow.ActivePane.View.SeekView = 0
编辑2
我将嵌入图像从Excel转移到Word的建议是使用复制粘贴:
My suggestion about transferring the embedded image from Excel to Word is to use copy and paste:
'***** copy image from cell B1 in Excel
ThisWorkbook.ActiveSheet.Range("B1").Copy
'***** past image at the current position in Word
wdApp.Selection.Paste
根据您希望图像显示的位置,代码需要进入 wdApp.Selection.TypeText
命令附近的某个位置。
The code needs to go somewhere near the wdApp.Selection.TypeText
command, depending on where you want the image to display.
编辑3
添加页码字段的代码:
Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:="PAGE ", PreserveFormatting:=True
这篇关于从Excel导出到单词文档并插入标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!