2017-04-14 73 views
2

我試圖從Excel表格插入一系列78個值到Word文檔中。這是爲了便於生成Word文檔。下面的代碼可以讓我插入:Word VBA插入書籤和格式化

Option Explicit 

Sub WriteExtension() 
' 
' WriteExtension Macro 
' 
' 
     copyFile 

     Dim nWord As New Document 
     word.Application.ScreenUpdating = False 

     Set nWord = Documents.Open("C:\target\file\here\targetfile", Visible:=False) 


     'initialize excel variables 
     Dim oExcel As Excel.Application 
     Dim oWorkbook As workbook 
     Dim oWorksheet As worksheet 

     'initialize excel object 
     Set oExcel = New Excel.Application 
     oExcel.ScreenUpdating = False 
     Set oWorkbook = oExcel.Workbooks.Open("source\spreadsheet\here\sourcespreadsheet.xlsx") 
     Set oWorksheet = oWorkbook.Worksheets(Sheets("Extensions").Index) 

     'setup loop variables 
     Dim tempString As String 
     Dim i As Long 
     Dim bkMark As Bookmark 

     'insert items from spreadsheet onto word document 
     Dim insertText As String 

     For i = 1 To 78 
      nWord.Bookmarks("BM" & i).Select 
      nWord.Bookmarks.Item("BM" & i).Range.InsertAfter (Cells(4, i + 6)) 
     Next i 

     Dim filePath As String 
     Dim fileName As String 
     Dim newName As String 

     'save the file as a PDF and close the PDF 
     filePath = "C:\target\path\here" 
     fileName = Cells(4, 13) & Cells(4, 12) & Cells(4, 79) & ".pdf" 
     newName = filePath & fileName 
     nWord.SaveAs2 fileName:=newName, FileFormat:=wdFormatPDF 

     'close things 
     nWord.Close False 
     ' oWorksheet. 
     oWorkbook.Close False 
     oExcel.Quit 
End Sub 

'function takes the current extension template which has this macro in it, and copies it to a new blank word document 
Function copyFile() 

    Dim fso As Object 
    Set fso = VBA.CreateObject("Scripting.FileSystemObject") 

    Dim sourceFile As String 
    Dim targetFile As String 

    sourceFile = "c:\source\file\here\sourcedocument.docx" 
    targetFile = "c:\target\file\here\targetfile" 

    fso.copyFile sourceFile, targetFile 

End Function 

概括地說這是什麼節目呢,是從某個電子表格需要的信息,並試圖在特定單元格中插入信息(或將做具體計算)文檔上的特定位置。爲了做到這一點,它首先需要一個示例文件(sourcefile),製作一個新文件(targetfile),然後將源文件複製到targetfile。這意味着文本,格式化和書籤的位置都完全複製。

然後它初始化一個新的excel對象,在那裏我將我想要的數據保存到文檔中。它將其打開,併爲78個書籤中的每一個運行78個單位的循環。它保存新文檔(以前稱爲targetfile),並根據excel電子表格中的值命名它。它將新文檔保存爲PDF。然後它關閉文檔,關閉excel,並關閉單詞。

我遇到的問題是格式化之一。基本上,我正在尋找在某種下劃線或邊界之上發生的插入,而不是取代該線。想象一下填充應用程序 - 你在線上寫而不是插在旁邊。它看起來並不像font.underline那樣工作,因爲它只是擁抱了文本,而不是讓下劃線看起來像。它可能,也許我沒有完全充實它,但我希望Stackoverflow的天才可以幫助我。

所以問題是:我如何在書籤旁邊插入東西,以便我可以將它插入到行而不是旁邊?換句話說,我如何使用書籤/頁面格式來使文本顯示爲#3,而不是#1或#2。大多數時候它顯示爲#1。

此代碼的工作

 Dim i As Long 
     Dim bkMark As Bookmark 

     'insert items from spreadsheet onto word document 
     Dim insertText As String 

     Dim startX As Long 
     Dim startY As Long 


    For i = 1 To 2 

     startX = ActiveDocument.Bookmarks.Item("BM" & i).Range.Information(wdHorizontalPositionRelativeToPage) 
     startY = ActiveDocument.Bookmarks.Item("BM" & i).Range.Information(wdVerticalPositionRelativeToPage) + 13 
     'Dim shp As Shape 
     With ActiveDocument.Shapes.AddLine(startX, startY, startX + 200, startY).Line 
      .ForeColor.RGB = RGB(0, 0, 0) 

     End With 
    Next i 
' 

enter image description here

+1

在您的示例屏幕截圖中,我假設「下劃線」是由shift和'-'鍵創建的模擬下劃線?我想你不得不從你的模板中刪除這些,而是​​插入一個位於文本下方的繪圖對象(線條)。 –

+0

是的,你對你的假設是正確的。好。我看看繪圖線。 – bdpolinsky

+0

是的,否則我不認爲它可以工作,因爲那些「下劃線」實際上是輸入到文檔中的字符,並且不能將它們與文本本身結合使用。它是一個或另一個,你將以#1或#2結束,取決於你插入的位置,但不會#3,因爲''Hello World''只會覆蓋/替換''______________「'。 –

回答

1

我最初的建議是手動添加的畫線在模板文件您正在使用,這將避免需要重新創建它們在每個你用這個VBA過程創建的複製版本,我仍然認爲這是可取的,但如果你由於某種原因不能修改模板文件,那麼你應該可以做下面的事情。

Dim objLine as Shape 'declare a Shape object to represent the drawing object lines you'll insert 

    For i = 1 To 2 

     startX = ActiveDocument.Bookmarks.Item("BM" & i).Range.Information(wdHorizontalPositionRelativeToPage) 
     startY = ActiveDocument.Bookmarks.Item("BM" & i).Range.Information(wdVerticalPositionRelativeToPage) + 13 
     ' at each iteration, assign the return from AddLine method to the objLine variable 
     Set objLine = ActiveDocument.Shapes.AddLine(beginX:=startX, beginy:=startY, endx:=startX + 200, endY:=startY) 
     ' assign the RGB color to the objLine.Line.ForeColor 
     objLine.Line.ForeColor.RGB = RGB(0, 0, 0) 
     ActiveDocument.Bookmarks.Item("BM" & i).Range.InsertAfter ("Bookmark" & i) 

    Next i 

我們在這裏所做的是使用Shape類型(這是繪畫般的線條,文本框等對象的對象類型的對象變量objLine您可能會插入到文檔中),並將AddLine方法的返回值分配給此形狀對象,每次迭代時都會返回該值。隨後,我們使用objLine並將RGB顏色分配給它的​​(行沒有Fill屬性)。

0

從你的描述聽起來好像你正試圖創建一個表單。

如果您在Excel中創建表單,則可以將每個包含響應的單元格格式化爲一行底部邊框。

現在將相同的思想轉化爲Word - 將表單的輸出格式設置爲帶有底部邊框的單元格。將您的書籤放在需要從Excel中添加數據的單元格中。

將值插入書籤時,不需要選擇它。所以,你可以簡化你的代碼的該部分:

For i = 1 To 78 
    nWord.Bookmarks("BM" & i).Range.InsertAfter (Cells(4, i + 6)) 
Next i