简体   繁体   English

使用 VBA 向 Word 文档添加连续信息

[英]Adding consecutive information to Word Document using VBA

I am trying to add information from Excel to Word using VBA and at the same time add text/paragraphs to that word doc.我正在尝试使用 VBA 将信息从 Excel 添加到 Word,同时将文本/段落添加到该 Word 文档。

I have the below code which for some reason is resulting in the table note being added.我有以下代码,由于某种原因导致添加了表格注释。 I want to add tables and then content consecutively.我想添加表格,然后连续添加内容。

Tried to play with the paragraph numbers but with no success?尝试使用段落编号但没有成功?

Qualifier - I am new to VBA and not sure I am doing this right.预选赛 - 我是 VBA 的新手,不确定我这样做是否正确。

Here is my code:这是我的代码:

Sub create_Word()
  
Dim tblRange As Excel.Range
Dim WrdRange As Word.Range
Dim WordApp As Word.Application
Dim WordDoc As Word.Document
Dim WordTable As Word.Table
Dim intRows
Dim intColumns

'Define number of rows
intNoOfRows = 2
intNoOfColumns = 2

' Check if word is open, otherwise open word

On Error Resume Next
Set WordApp = GetObject(class:="Word.Application")
If WordApp Is Nothing Then Set WordApp = CreateObject(class:="Word.Application")

' Make word visible and activate the program
WordApp.Visible = True
WordApp.Activate

' Create document inside word

Set WordDoc = WordApp.Documents.Add

'Section 1

With WordApp.Selection
        
    .Paragraphs.Alignment = wdAlignParagraphLeft
    .Font.Size = 12
    .TypeText "Heading" & vbCrLf & vbCrLf & "Date: _____________" & vbCrLf & vbCrLf
    .BoldRun
    .Font.Size = 12
    .TypeText "Content" & vbCrLf
    .TypeText "Period ended: 31 December 2020" & vbCrLf & vbCrLf
    .BoldRun
    
End With
     
'Add table

Set WrdRange = WordDoc.Paragraphs(11).Range
WordDoc.Tables.Add WrdRange, intNoOfRows, intNoOfColumns
Set WordTable = WordDoc.Tables(1)
WordTable.Borders.Enable = True


End Sub

UPDATE Based on the suggestions below, I've adapted the code and this results in the tables combining for some reason.更新根据下面的建议,我调整了代码,这导致表格由于某种原因合并。 The following code has been applied:已应用以下代码:

Set WordDoc = WordApp.Documents.Add

'Section 1

With WordApp.Selection
        
    .Paragraphs.Alignment = wdAlignParagraphLeft
    .Font.Size = 12
    .TypeText "Test Text 1"
    
End With
     
'Add table 1

Set WrdRange = WordDoc.Paragraphs.Last.Range
Set WordTable = WordDoc.Tables.Add(WrdRange, intNoOfRows, intNoOfColumns)
WordTable.Borders.Enable = True

'Section 2
With WordApp.Selection
   .TypeText "Test Text 2" & vbCrLf
       
End With

'Add table 1

Set WrdRange = WordDoc.Paragraphs.Last.Range
Set WordTable = WordDoc.Tables.Add(WrdRange, intNoOfRows, intNoOfColumns)
WordTable.Borders.Enable = True

Your first problem is that you have not reset error handling after getting the Word application.您的第一个问题是您在获得 Word 应用程序后没有重置错误处理。 That section of code should be:那段代码应该是:

On Error Resume Next
Set WordApp = GetObject(class:="Word.Application")
If WordApp Is Nothing Then Set WordApp = CreateObject(class:="Word.Application")

On Error GoTo 0

You are attempting to add a table to paragraph 11, but on my count your document only contains 8 paragraphs.您正在尝试向第 11 段添加表格,但据我所知,您的文档仅包含 8 个段落。 If you are trying to add the table to the end of the document then you can use either:如果您尝试将表格添加到文档的末尾,那么您可以使用:

Set WrdRange = WordDoc.Paragraphs.Last.Range

or:要么:

Set WrdRange = WordDoc.Characters.Last

The Add method is a function and will return the object added so you should make use of that, eg Add方法是一个函数,将返回添加的对象,因此您应该使用它,例如

Set WordTable = WordDoc.Tables.Add(WrdRange, intNoOfRows, intNoOfColumns)
WordTable.Borders.Enable = True

or:要么:

With WordDoc.Tables.Add(WrdRange, intNoOfRows, intNoOfColumns)
    .Borders.Enable = True
End With

You code also includes some undeclared variables.您的代码还包括一些未声明的变量。 You can avoid these errors by adding Option Explicit at the top of the code module.您可以通过在代码模块顶部添加 Option Explicit 来避免这些错误。 This will prevent your code from compiling when you have undeclared variables.当您有未声明的变量时,这将阻止您的代码编译。 To add this automatically to new modules open the VBE and go to Tools |要将其自动添加到新模块,请打开 VBE 并转到工具 | Options.选项。 In the Options dialog ensure that Require Variable Declaration is checked.在“选项”对话框中,确保选中“需要变量声明”。

工具|选项对话框的屏幕截图

EDIT:编辑:

Stop using Selection .停止使用Selection There is rarely any need to select anything when working with either Word or Excel.使用 Word 或 Excel 时很少需要选择任何内容。

Your tables are getting joined because they follow each other.您的表正在加入,因为它们彼此跟随。 This is because you are using Selection to add text, even though the current selection is before the table.这是因为您正在使用Selection来添加文本,即使当前选择在表格之前。

Change:改变:

With WordApp.Selection
   .TypeText "Test Text 2" & vbCrLf
       
End With 

To:到:

WordDoc.Paragraphs.Last.Range.Text = "Test Text 2" & vbCr

Make yourself familiar with the help tools you have in the VBE: Intellisense, the Object Browser and online help.熟悉 VBE 中的帮助工具:Intellisense、对象浏览器和在线帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM