我試圖從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
'
在您的示例屏幕截圖中,我假設「下劃線」是由shift和'-'鍵創建的模擬下劃線?我想你不得不從你的模板中刪除這些,而是插入一個位於文本下方的繪圖對象(線條)。 –
是的,你對你的假設是正確的。好。我看看繪圖線。 – bdpolinsky
是的,否則我不認爲它可以工作,因爲那些「下劃線」實際上是輸入到文檔中的字符,並且不能將它們與文本本身結合使用。它是一個或另一個,你將以#1或#2結束,取決於你插入的位置,但不會#3,因爲''Hello World''只會覆蓋/替換''______________「'。 –