如何在Excel中嵌入的文档中修改Word表 [英] How to modify a Word table in a document embedded in Excel

查看:175
本文介绍了如何在Excel中嵌入的文档中修改Word表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

到目前为止,我已经能够激活嵌入在Excel中的Word文档,但无法修改表格的preferredwidth.如何通过Excel VBA访问此属性?

I have so far been able to activate a Word document embedded in Excel, but I'm not able to modify the table's preferredwidth. How can I access this property through Excel VBA?

这是我到目前为止所拥有的

Here's what i have so far

Sub ok()

Set WDObj = Sheets("Sheet1").OLEObjects("Object 4")

WDObj.Activate
WDObj.ActiveDocument.Tables(1).PreferredWidthType = wdPreferredWidthPercent
WDObj.ActiveDocument.Tables(1).PreferredWidth = 95

End Sub

推荐答案

要修改嵌入式Word文档的内容,首先需要访问嵌入式应用程序,然后是文档本身.一旦代码访问了OLEObject,它便需要实际的Object,通过它可以寻址应用程序和文档.

In order to modify the content of an embedded Word document it's first necessary to access the embedded application, then the document itself. Once the code has accessed the OLEObject it then needs the actual Object, through which the application and the document can be addressed.

请注意,为OLE对象分配一个名称而不是依靠Excel生成的名称可能是方便的.如果添加了其他对象或删除了其他对象,则此类名称会发生​​变化的讨厌趋势.明确分配的任何名称都将保持不变.只需在工作表上运行一次(基于完整的示例代码):

Note that it might be expedient to assign the OLE object a name, rather than relying on the one generated by Excel. Such names have a nasty tendency to change if additional objects are added or others deleted. Any name assigned explicitly will remain untouched. This needs to be run only one time on the worksheet (bases on the full sample code):

ws.OLEObjects("Object 4").Name = "WordDoc" 'for example, can be any string you prefer

然后,如果您决定执行此操作,则在完整代码中为ws.OLEObjects("WordDoc")

Then, if you decide to do this, in the full code, it would be ws.OLEObjects("WordDoc")

在过程结束时,行ws.Cells(1, 1).Select通过在图纸上选择一个单元格来停用嵌入的对象.如果要让Word文档处于活动状态以供用户使用,只需删除该行即可.

At the end of the procedure the line ws.Cells(1, 1).Select deactivates the embedded object by selecting a cell on the sheet. If you want to leave the Word document active for the user to work with, simply remove that line.

Sub ok()
    Dim wb As Excel.Workbook
    Dim ws As Excel.Worksheet
    Dim doc As Object 'Word.Document
    Dim wdObj As Object, wdTable As Object 'Word.Table

    Set wb = ActiveWorkbook
    Set ws = wb.Sheets("Sheet1")
    Set wdObj = ws.OLEObjects("Object 4")
    wdObj.Activate
    Set doc = wdObj.Object.Application.ActiveDocument
    Set wdTable = doc.Tables(1)
    wdTable.PreferredWidthType = 2 ' Word.WdPreferredWidthType.wdPreferredWidthPercent
    wdTable.PreferredWidth = 95
    ws.Cells(1, 1).Select
End Sub

这篇关于如何在Excel中嵌入的文档中修改Word表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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