简体   繁体   English

使用VBA Makro在Word中从Excel打印数据

[英]Print Data from Excel in Word with VBA Makro

I'm trying to print Data from my Excelsheet to a table that already exists in an already existing word File. 我正在尝试将数据从我的Excelsheet打印到已经存在的Word文件中已经存在的表中。

My Data in excel is a simple question like: "What is the weather like?" 我在excel中的数据是一个简单的问题,例如:“天气如何?”

Now I want to print this question in to a table in word. 现在,我想将此问题打印到单词表中。 The Word File and the table is already existing. Word文件和表已存在。

First I'm opening the word file from my excel makro. 首先,我从excel makro打开word文件。

Dim AppWD As Object

Set AppWD = CreateObject("Word.Application") 'Word als Objekt starten

AppWD.Visible = True
AppWD.documents.Open "file location"

doc.Unprotect

This works perfectly fine, but then I'm first of all trying to remove a Row in that table as a test, i do that like this: 这工作得很好,但是然后我首先尝试删除该表中的行作为测试,我这样做是这样的:

AppWD.Tables.Item(1).Rows(2).Delete

But here's the problem, this doesnt work. 但这是问题,这不起作用。 I always get the Run-time error '424': Object required 我总是会收到运行时错误“ 424”:所需对象

I think the problem is that my makro doesn't know that it has to write in to word and not into my excelsheet. 我认为问题在于我的makro不知道它必须写到单词中,而不是写到我的excelsheet中。

It's because you are not creating and using your objects correctly. 这是因为您没有正确创建和使用对象。 is this what you are trying? 这是您要尝试的吗? ( UNTESTED )

Dim AppWD As Object, doc As Object

Set AppWD = CreateObject("Word.Application")

AppWD.Visible = True

Set doc = AppWD.documents.Open("file location")

doc.Unprotect

doc.Tables.Item(1).Rows(2).Delete

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

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