【发布时间】:2017-04-18 00:03:31
【问题描述】:
我正在尝试将 excel 表中的一系列 78 个值插入到 word 文档中。这是为了方便生成word文档。以下代码允许我插入:
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
简而言之,该程序的作用是从电子表格中获取信息,并尝试在文档特定位置的特定单元格中插入信息(或将进行特定计算)。为此,它首先获取一个示例文档(源文件),创建一个新文件(目标文件),然后将源文件复制到目标文件。这意味着文本的位置、格式和书签都被完全复制。
然后它初始化一个新的 excel 对象,我在其中保存我想要输入文档的数据。它打开它,并为 78 个书签中的每一个运行一个 78 个单位长的循环。它保存新文档(以前称为目标文件)并根据 excel 电子表格中的值对其进行命名。它将新文档保存为 PDF。然后关闭文档,关闭excel,关闭word。
我遇到的问题是格式化问题之一。基本上,我正在寻找插入发生在某种下划线或边框上方,而不是取代线。想象一下填写一个应用程序 - 您在该行上写而不是在它旁边插入。看起来 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 和
-键创建的模拟下划线?我认为您必须从模板中删除它们,然后插入位于文本下方的绘图对象(线)。 -
是的,您的假设是正确的。好的。生病看看画线。
-
是的,否则我认为它不起作用,因为那些“下划线”实际上是输入到文档中的字符,您不能将它们与文本本身结合使用。这是一个或另一个,你最终会得到#1或#2,具体取决于您插入的位置,但永远不会#3,因为
"Hello World"只会覆盖/替换"______________"。 -
用文本框替换您的书签。相对于边距和段落定位它们,并在没有框架的情况下设置它们的格式。这样,您可以保留文本中的下划线,并根据需要从其中删除插入内容。
-
@DavidZemens 感谢您的提示。我添加了有效的代码,现在唯一的问题是使其宽度合适,并将颜色设置为黑色。宽度应该不是太大的问题(除了每个书签一个书签浏览文档很费时间);我添加的用于更改线条颜色的代码不起作用。它会自动将其添加为蓝线 - 我想要黑色。更正?