VBScript 不会在特定单词后将复制的 Excel 插入 Word [英] VBScript Doesn't insert copied Excel into Word after specific word

查看:34
本文介绍了VBScript 不会在特定单词后将复制的 Excel 插入 Word的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试在特定单词后插入复制的 Excel 部分.不幸的是它不起作用.

I tried to insert a a copied Excel part after a specific word. Unfortunately it's not working.

        Set ObjXl = CreateObject("Excel.Application")
        Set ObjWkBk = ObjXl.Workbooks.Open("C:\test.xlsx")
        
        Set ObjWd = CreateObject("Word.Application")
        Set ObjDoc = ObjWd.Documents.Open("C:\test.docx")
        
        ObjWkBk.Worksheets("Sheet1").Range("A1:D4").Copy

        #############Here I have Problems############
        ObjDoc.Range.InsertAfter("Ending").Paste
        #############################################

        ObjDoc.SaveAs ("C:\test1.docx")
        
        ObjDoc.Close: ObjWd.Quit: ObjWkBk.Close: ObjXl.Quit
        Set ObjDoc = Nothing: Set ObjWd = Nothing: Set ObjWkBk = Nothing: Set ObjXl = Nothing

你能帮忙吗?

推荐答案

Excel Range 与 Word Range 不同.首先为每个声明变量,例如 ...

An Excel Range is a different animal from a Word Range. Start by declaring variables for each, like ...

Dim xlRange as Excel.Range
Dim wdRange as Word.Range

现在您可以将 Excel 范围分配给 xlRange.

Now you would be able to assign the Excel range to xlRange.

Set xlRange =  ObjWkBk.Worksheets("Sheet1").Range("A1:D4")

您可以对 Word 范围执行相同的操作.

And you could do the same for your Word range.

Set wdRange = ObjDoc.Range("Ending")

你可能会看到这看起来不太对劲,实现这个判断就是这个练习的目的.在 Word 中,范围由其在文档中的第一个和最后一个字符的位置定义.如果范围已命名,则称为书签.因此,如果您有一个名为 Ending" 的书签,那么正确的分配应该是...

You can probably see that this doesn't look right, and to enable this judgment is the object of this exercise. In Word a range is defined by the position of its first and last characters in the document. If the range is named, it's called a Bookmark. So, if you have a bookmark by the name of "Ending" then the correct assignment would look like ...

Set wdRange = ObjDoc.Bookmarks("Ending").Range

假设该范围包括字符 1111 和 1122 之间的文本,然后将该范围折叠到其末尾将创建 Range(1122, 1123) 并且此时您可以插入 text(因为它是 Word 文档)通过多种方法.

Assuming that this range includes the text between character 1111 and 1122, then collapsing that range to its end would create Range(1122, 1123) and at that point you can insert text (because it's a Word document) by a number of methods.

问题在于您的 xlRange 不是文本.它是一个由 16 个 Excel 单元格组成的数组,每个单元格都具有许多属性.根据您将此范围复制到 Word 中的方式,您可能会强制将 Excel 单元格转换为 Word 表格.请记住,Word 表格不具有 Excel 表格或 Excel 单元格数组的所有属性.它的结构不同,并非所有属性都可以转让,但您会得到一张表格.

The problem is that your xlRange isn't text. It's an array of 16 Excel cells, each of them endowed with many properties. Depending upon how you copy this range into Word, you may force the conversion of Excel cells to a Word table. Bear in mind that a Word table doesn't have all the properties of an Excel table or an Excel array of cells. It's differently structured and not all properties are transferrable but you will get a table.

另一方面,如果您打算从 xlRange 中获取文本并将其作为文本插入 Word 中,那么您必须首先从每个单元格中提取文本,创建一个字符串,然后然后将该字符串粘贴到 Word 文档中 wdRange 定义的位置.在这方面,值得注意的是 VBA Strings 是可以互换的.您可以通过从 Excel 单元格中提取文本并将该字符串粘贴到 Word 文档来创建字符串,而无需担心 Strings 的 Excel 和 Word 类型.

On the other hand, if your intention is to take the text from xlRange and insert it into Word as text then you would have to first extract the text from each cell, create a string, and then paste that string into the Word document at the location defined by wdRange. In this regard it's noteworthy that VBA Strings are interchangeable. You can create a string by extracting text from Excel cells and paste that same string to a Word document without worrying about Excel and Word types of Strings.

这篇关于VBScript 不会在特定单词后将复制的 Excel 插入 Word的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆