将文本从excel复制到单词作为没有单元格边框的句子 [英] To copy text from excel to word as sentence without cell borders

查看:107
本文介绍了将文本从excel复制到单词作为没有单元格边框的句子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个excel,它的特定列在每个单元格中的多行中都有文本.我想将多行文本作为单词以句子格式复制到Word文档中.我尝试了几种代码,但是所有代码都复制了excel范围,并以完全相同的方式将其粘贴到excel中(包括单元格边框).我不要细胞边界.我希望将文本复制为句子/段落.有人可以帮我吗?

I have an excel with a specific column having text in multiple lines within each cells. I want to copy that multi-line text into word document as sentence in word format. I tried several codes but all that copies the excel range and pastes in word exactly the same way it is in excel including the cell borders. I do not want cell borders. I want text to be copied as a sentence/paragraph. Can someone please help me with this.

这是我正在使用的代码:

Here is the code i'm using:

Public Sub CommandButton1_Click()
    On Error GoTo ErrorHandler

Dim wApp As Word.Application
Dim wDoc As Word.Document

Set wApp = CreateObject("Word.Application.8")
wApp.Visible = True
Const strPath1 As String = "D:\Template.docx"
Set wDoc = wApp.Documents.Add(Template:=strPath1, NewTemplate:=False, DocumentType:=0)

Worksheets("Sheet2").Range(ActiveCell, ActiveCell.End(xlDown)).Copy

With wDoc.Paragraphs(wDoc.Paragraphs.Count).Range
         .Paste
End With

ErrorHandler:
        Resume Next
End Sub

推荐答案

您不想使用复制和粘贴,需要使用Excel VBA对象模型来引用所需的单元格,然后使用单元格访问文本值.

You don't want to use copy and paste, you need to use the Excel VBA object model to reference the cell you want and then access the text using cell.Value.

您必须已经在使用Excel.Application,并且由于已经复制并粘贴了该值,因此您必须已经引用了该单元格,因此必须将要复制和粘贴的代码与要使用cell.value的代码交换.

You must already be using Excel.Application, and you must already be referencing the cell since you copy and paste the value, so swap the code you have to copy and paste with the code to use the cell.value.

不幸的是,我不在我的电脑上,因此无法测试,但是您想用类似的代码替换您的代码

Unfortunately, I'm not at my computer so I can't test this, but you want to replace your code with something like

Public Sub CommandButton1_Click()
    On Error GoTo ErrorHandler

Dim wApp As Word.Application
Dim wDoc As Word.Document

Set wApp = CreateObject("Word.Application.8")
wApp.Visible = True
Const strPath1 As String = "D:\Template.docx"
Set wDoc = wApp.Documents.Add(Template:=strPath1, NewTemplate:=False, DocumentType:=0)

Dim currentcell as variant
Dim result as string

For each currentcell in Worksheets("Sheet2").Range(ActiveCell, ActiveCell.End(xlDown))

    Result =result & currentcell.value & vbcrlf

Next currentcell

With wDoc.Paragraphs(wDoc.Paragraphs.Count).Range
         .Text= result
End With

ErrorHandler:
        Resume Next
End Sub

这篇关于将文本从excel复制到单词作为没有单元格边框的句子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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