2017-08-06 89 views
1

我想使用VBA從Excel內打開Word文檔,並將一些表插入到文檔中。這些表格填充了來自Excel的信息。表似乎被覆蓋

每個表似乎覆蓋了前一個。

這是一個更新,我需要確保表格不在表格內形成。 THis is an example of what Is going on

我該如何避免這種情況?

Dim intNoOfRows 
    Dim intNoOfColumns 
    Dim objWord 
    Dim objDoc 
    Dim objRange 
    Dim objTable 

    intNoOfRows = 5 
    intNoOfColumns = 3 

    Set objWord = CreateObject("Word.Application") 
    objWord.Visible = True  

    Set objDoc = objWord.Documents.Add  

    Set objRange = objDoc.Range 

    objDoc.Tables.Add objRange, intNoOfRows, intNoOfColumns  

    Set objTable = objDoc.Tables(1) 

    objTable.Borders.Enable = True 

    For i = 1 To intNoOfRows 
     For j = 1 To intNoOfColumns 

     Next 
    Next 
End Function 
+1

也許您將n是否需要摺疊您正在使用的範圍?但是沒有一些代碼就不可能知道。 –

+0

拍攝,沒有像我想的那樣粘貼,一時刻 – user8426180

+0

http://excel-macro.tutorialhorizo​​n.com/vba-excel-add-table-and-fill-data-to-the-word-document/ – user8426180

回答

0

Word對象模型具有Range的概念;您可以將其視爲您想在程序中使用的當前文本塊。

這條線:

objDoc.Tables.Add objRange, intNoOfRows, intNoOfColumns 

增加了一個新的表給定的範圍(objRange)。

documentation for the Add method望着參數列表中,我們可以看到下面的(強調):

名稱:範圍

說明:您希望該表的範圍內出現。 如果範圍未摺疊,則表格將替換範圍。

看你的代碼的上一行:

Set objRange = objDoc.Range 

的範圍擴展到整個文檔。這可能不是您想要的,因爲它不僅會替換任何其他表格,還會替換文檔中的任何其他文本。

爲了縮小範圍,請致電Collapse method。範圍可以通過兩種方式摺疊:範圍的末尾應該移動到範圍的開始位置,或範圍的開始應該移動到範圍的末尾。此行爲是由wdCollapseStartwdCollapseEnd傳遞給Collapse方法,或同等值來控制:

'objRange.Collapse wdCollapseEnd 
objRange.Collapse 1 

一旦你已經把調查範圍,那麼你可以添加新表。

objDoc.Tables.Add objRange, intNoOfRows, intNoOfColumns 

注:你的代碼使用了後期綁定,這意味着(除其他外)編輯器不知道你正在使用的各種對象的形狀。

爲用戶提供這一信息的編輯,添加引用(工具 - >引用...)到Microsoft Word對象模型(或類似的東西)。

然後,當你聲明變量,可以指定其類型:

Dim objDoc As Word.Document 

現在,當你寫oDoc.,編輯可以告訴你,這可用於Word文檔的方法和屬性。


這段代碼還有另一個問題。一旦插入表格,表格上就會有幾行處理。但處理決不會在文檔中最新添加的表進行,而是總是在文檔中的第一個表:

objDoc.Tables.Add objRange, intNoOfRows, intNoOfColumns 
Set objTable = objDoc.Tables(1) 'reference to the first table in the document 

解決這個問題的最簡單的方法,是使用參考從添加返回表:

'we need the parentheses now because we're using the returned value 
Set objTable = objDoc.Tables.Add(objRange, intNoOfRows, intNoOfColumns) 

最終的代碼可能是這個樣子:

Dim objWord As New Word.Application 

'in production, keeping the application invisible is often more performant 
'but while debugging, it's useful to see what's going on in the application 
objWord.Visible = True 

Dim objDoc As Word.Document 
Set objDoc = wdApp.Documents.Add 

Dim objRange As Word.Range 
Set objRange = objDoc.Range 
objRange.Collapse wdCollapseEnd 

Dim intNoOfRows As Integer 
intNoOfRows = 5 
Dim intNoOfColumns As Integer 
intNoOfColumns = 3 
Dim objTable As Word.Table 
Set objTable = objDoc.Tables.Add(objRange, intNoOfRows, intNoOfColumns) 

'Fill the table here 

'Collapse the range again (not sure this is needed) 
objRange.Collapse wdCollapseEnd 
'Now you can add a new table: 
Set objTable = objDoc.Tables.Add(objRange, intNoOfRows, intNoOfColumns) 
+0

感謝您的支持!有幾個問題......我是否在轉換中使用摺疊方法來製作其他表格,並使用我後張貼的相同代碼行? – user8426180

+0

一旦我崩潰,我仍然不確定如何將表格放在我製作的上一張表格下。 – user8426180

+0

@ user8426180 RE _other其他代碼行:是的。將新表插入文檔後,您不再需要範圍,因爲您有對錶本身的引用。爲什麼你需要這個範圍的唯一原因是爲了告訴Word你要在文檔中插入表的位置。 –