vba 从 Excel 导出到 word 文档并在 word 中插入标题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23865854/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 03:14:38  来源:igfitidea点击:

Export from Excel to a word Document and insert a header in word

excelvbaexcel-vbams-wordword-vba

提问by RiMa

I have the following code:

我有以下代码:

    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

What it does is that it creates a word document with the data from range A1:E76

它的作用是使用范围 A1:E76 中的数据创建一个 Word 文档

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.

我想在这个包含图片和名称的 word 文档中插入一个标题。此标题中的名称位于同一工作表的单元格 A1 中。

Would be very thankful if anyone could help me with this. Thank you.

如果有人能帮助我解决这个问题,我将不胜感激。谢谢你。

采纳答案by Olle Sj?gren

How to add text from cell A1 to the document:

如何将单元格 A1 中的文本添加到文档:

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

How to add an image:

如何添加图片:

wdApp.Selection.InlineShapes.AddPicture Filename:="PATH_TO_IMAGE", LinkToFile:=False, SaveWithDocument:=True

If you put the above code together and add it directly after the line wdApp.Visible = Trueyou 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.

如果您将上述代码放在一起并直接添加到该行之后,wdApp.Visible = True您将在末尾获得一个带有图像的标题,但我无法从您的问题中确切地知道您希望文档看起来如何。

EDIT

编辑

Code to show current header:

显示当前标题的代码:

'***** Word VBA constant wdSeekCurrentPageHeader = 9
wdApp.ActiveWindow.ActivePane.View.SeekView = 9

Show normal view:

显示普通视图:

'***** Word VBA constant wdSeekMainDocument  = 0
wdApp.ActiveWindow.ActivePane.View.SeekView = 0

Putting it all together, paste this after the line wdApp.Visible = True, this time without setting the style:

放在一起,将其粘贴在 line 之后wdApp.Visible = True,这次不设置样式:

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

EDIT 2

编辑 2

My suggestion about transferring the embedded image from Excel to Word is to use copy and paste:

我关于将嵌入的图像从 Excel 传输到 Word 的建议是使用复制和粘贴:

'***** copy image from cell B1 in Excel
ThisWorkbook.ActiveSheet.Range("B1").Copy
'***** past image at the current position in Word
wdApp.Selection.Paste

The code needs to go somewhere near the wdApp.Selection.TypeTextcommand, depending on where you want the image to display.

代码需要放在wdApp.Selection.TypeText命令附近的某个位置,具体取决于您希望图像显示的位置。

EDIT 3

编辑 3

Code for adding page number field:

添加页码字段的代码:

Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:="PAGE  ", PreserveFormatting:=True