首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >在Excel VBA中创建和格式化Word表格时遇到问题

在Excel VBA中创建和格式化Word表格时遇到问题
EN

Stack Overflow用户
提问于 2019-06-19 21:06:22
回答 2查看 276关注 0票数 0

我是MS Word VBA的新手,在处理Excel中的Word文档时遇到了麻烦。

到目前为止,最大的问题是:在Word VBA中工作的代码在Excel中不能工作。非常奇怪和令人沮丧。

代码如下:

代码语言:javascript
复制
Sub abc()
Dim MSWordApp As Object, MSWordDoc As Object

    Set MSWordApp = CreateObject("Word.Application")
    Set MSWordDoc = MSWordApp.Documents.Add
    MSWordApp.Visible = True
    With MSWordDoc
        With .PageSetup
            .TopMargin = Application.CentimetersToPoints(0.51)
            .BottomMargin = Application.CentimetersToPoints(0.51)
            .LeftMargin = Application.CentimetersToPoints(0.51)
            .RightMargin = Application.CentimetersToPoints(0.51)
        End With
        .Tables.Add Range:=.Range(0, 0), NumRows:=3, NumColumns:=2
        With .Tables(1)
            .Rows.Alignment = wdAlignRowCenter
            .Rows.HeightRule = wdRowHeightExactly
            .Rows.Height = Application.CentimetersToPoints(9.55)
            .Columns.PreferredWidthType = wdPreferredWidthPoints
            .Columns.PreferredWidth = Application.CentimetersToPoints(9.9)
        End With
    End With

    MSWordApp.Activate
    Set MSWordApp = Nothing
    Set MSWordDoc = Nothing
End Sub

这些代码在MS Word中工作得很好(当然,当我在MS Word中使用它们时,我已经更改了object等的名称)。

然而,在Excel中发生了一些奇怪的事情:

1) ".Rows.Alignment = wdAlignRowCenter“根本不起作用。Word表的Rows.Alignment仍然是默认的。

2) ".Columns.PreferredWidthType = wdPreferredWidthPoints“导致Excel错误。它在Word中运行良好;尽管在Excel中,每次我调用此属性时都会弹出一个空的错误消息。不知道为什么..。

EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56668383

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档