简体   繁体   English

插入文本之前的 Excel VBA 空嵌入 Word 文档书签

[英]Excel VBA null embedded word document bookmarks before inserting text

I have Excel workbook from where I am inserting data to embedded (inside my workbook) Word file.我有 Excel 工作簿,我将数据插入到嵌入的(在我的工作簿中)Word 文件中。 I have predefined bookmarks.我有预定义的书签。 I am inserting bookmark text from Excel workbook cells.我正在从 Excel 工作簿单元格插入书签文本。 Everything works fine except for deleting imported data from bookmarks.除了从书签中删除导入的数据外,一切正常。 The problem is that with my code, after several runs keeps recording data to bookmarks.问题是,对于我的代码,经过多次运行后,仍然将数据记录到书签中。 So, for example, after 3 runs I have "SwedenSwedenSweden".因此,例如,在运行 3 次后,我得到了“SwedenSwedenSweden”。

I would like to null bookmarks before inserting data objWord.Bookmarks.Item("Country").Range = "" does not seems to work.我想在插入数据之前objWord.Bookmarks.Item("Country").Range = ""书签objWord.Bookmarks.Item("Country").Range = ""似乎不起作用。 With this command I am trying to null bookmarks before entering new ones and after exiting my Template Word document.使用此命令,我尝试在输入新书签之前和退出模板 Word 文档之后清空书签。 Any good solutions?有什么好的解决办法吗?

Sub testInsertBookmark()
Const wdFormatDocument = 0
Dim sh As Shape
Dim objWord As Object ''Word.Document
Dim objOLE As OLEObject
Dim wSystem As Worksheet
Dim BMRange As Range
On Error Resume Next

Set wSystem = Worksheets("Templates")
''The shape holding the object from 'Create from file'
''Object 2 is the name of the shape
Set sh = wSystem.Shapes("Object 1")
''Activate the contents of the object
sh.OLEFormat.Activate
''The OLE Object contained
Set objOLE = sh.OLEFormat.Object
''This is the bit that took time
Set objWord = objOLE.Object

 objWord.Bookmarks.Item("Name").Range = ""
 objWord.Bookmarks.Item("Title").Range = ""
 objWord.Bookmarks.Item("Telephone").Range = ""
 objWord.Bookmarks.Item("Company").Range = ""
 objWord.Bookmarks.Item("Address").Range = ""
 objWord.Bookmarks.Item("Postcode").Range = ""
 objWord.Bookmarks.Item("City").Range = ""
 objWord.Bookmarks.Item("Country").Range = ""

 objWord.Bookmarks.Item("Name").Range.Text = ThisWorkbook.Sheets("MAIN").Range("D5").Value
 objWord.Bookmarks.Item("Title").Range.Text = ThisWorkbook.Sheets("MAIN").Range("D6").Value
 objWord.Bookmarks.Item("Telephone").Range.Text = ThisWorkbook.Sheets("MAIN").Range("D7").Value
 objWord.Bookmarks.Item("Company").Range.Text = ThisWorkbook.Sheets("MAIN").Range("D8").Value
 objWord.Bookmarks.Item("Address").Range.Text = ThisWorkbook.Sheets("MAIN").Range("D9").Value
 objWord.Bookmarks.Item("Postcode").Range.Text = ThisWorkbook.Sheets("MAIN").Range("D10").Value
 objWord.Bookmarks.Item("City").Range.Text = ThisWorkbook.Sheets("MAIN").Range("D11").Value
 objWord.Bookmarks.Item("Country").Range.Text = ThisWorkbook.Sheets("MAIN").Range("D12").Value

objWord.Application.Visible = True

''Easy enough
    objWord.SaveAs2 ActiveWorkbook.Path & "\" & Sheets("Other Data").Range("AN2").Value & ", " & Sheets("Other Data").Range("AN7").Value & "_" & Sheets("Other Data").Range("AN8").Value & "_" & Sheets("Other Data").Range("AX10").Value & ".pdf", 17

 objWord.Bookmarks.Item("Name").Range = ""
 objWord.Bookmarks.Item("Title").Range = ""
 objWord.Bookmarks.Item("Telephone").Range = ""
 objWord.Bookmarks.Item("Company").Range = ""
 objWord.Bookmarks.Item("Address").Range = ""
 objWord.Bookmarks.Item("Postcode").Range = ""
 objWord.Bookmarks.Item("City").Range = ""
 objWord.Bookmarks.Item("Country").Range = ""

sh.OLEFormat.Delete

ThisWorkbook.Worksheets("MAIN").Activate

End Sub

Writing data to a bookmark that marks a position (rather than contains content) will yield the result you describe.将数据写入标记位置(而不是包含内容)的书签将产生您描述的结果。 The way to get this to work is to use a bookmark that contains content - at least after the first insertion.使其工作的方法是使用包含内容的书签 - 至少在第一次插入之后。 When writing to such a bookmark it is deleted when the content is replaced, so it's necessary to recreate the bookmark, as well.当写入这样的书签时,它会在内容被替换时被删除,因此也需要重新创建书签。 For example:例如:

Dim wdRange as Object 'Word.Range
Set wdRange = objWord.Bookmarks.Item("Name").Range
wdRange.Text = ThisWorkbook.Sheets("MAIN").Range("D5").Value
objWord.Bookmarks.Add "Name", wdRange 

This recreates the bookmark around the new content.这将围绕新内容重新创建书签。 There's no need to delete the content / set it to "" as it will be replaced.无需删除内容/将其设置为“”,因为它将被替换。

My suggestion would be to put this in a separate procedure that can be called from the main code.我的建议是将它放在一个可以从主代码中调用的单独过程中。 Pass in objWord, the bookmark name and the Excel Range or its data.传入 objWord、书签名称和 Excel 范围或其数据。

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

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