ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

搜索
EH技术汇-专业的职场技能充电站 妙哉!函数段子手趣味讲函数 Excel服务器-会Excel,做管理系统 Excel Home精品图文教程库
HR薪酬管理数字化实战 Excel 2021函数公式学习大典 Excel数据透视表实战秘技 打造核心竞争力的职场宝典
300集Office 2010微视频教程 数据工作者的案头书 免费直播课集锦 ExcelHome出品 - VBA代码宝免费下载
用ChatGPT与VBA一键搞定Excel WPS表格从入门到精通 Excel VBA经典代码实践指南
查看: 1190|回复: 5

[讨论] wordVBA 代码在VB中运行不了

[复制链接]

TA的精华主题

TA的得分主题

发表于 2018-2-20 20:15 | 显示全部楼层 |阅读模式
本帖最后由 cunfu2010 于 2018-2-21 19:39 编辑

Sub 宏1()
    ActiveDocument.Shapes.AddConnector(msoConnectorStraight, 50, 300, 228, 0#).Select
    With Selection.ShapeRange.Line
        .Weight = 1.75
        .Visible = msoTrue
        .Style = msoLineSingle
        .ForeColor.RGB = RGB(255, 0, 0)
        .Visible = msoTrue
    End With
    ActiveDocument.Shapes.AddConnector(msoConnectorStraight, 320, 300, 228, 0#).Select
    With Selection.ShapeRange.Line
        .Weight = 1.75
        .Visible = msoTrue
        .Style = msoLineSingle
        .ForeColor.RGB = RGB(255, 0, 0)
        .Visible = msoTrue
    End With
    ActiveDocument.Shapes.AddShape(msoShape5pointStar, 285, 282, 30, 30).Select
    With Selection.ShapeRange
        .Fill.ForeColor.RGB = RGB(255, 0, 0)
        .Fill.Visible = msoTrue
        .Fill.Solid
        .Line.ForeColor.RGB = RGB(255, 0, 0)
        .Line.Visible = msoTrue
    End With
End Sub

上述代码能够在word中运行,但放到VB6中就提示错误了,如何修改?

TA的精华主题

TA的得分主题

发表于 2018-2-21 04:07 | 显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助
假设VB6中,声明的Word对象为objWD

  1. objWD.ActiveDocument...
  2. objWD.Selection...
复制代码

TA的精华主题

TA的得分主题

 楼主| 发表于 2018-2-21 19:02 | 显示全部楼层
本帖最后由 cunfu2010 于 2018-2-21 19:05 编辑
taller 发表于 2018-2-21 04:07
假设VB6中,声明的Word对象为objWD

谢谢版主,按照你说的修改代码后再试试。

TA的精华主题

TA的得分主题

 楼主| 发表于 2018-2-21 19:29 | 显示全部楼层
taller 发表于 2018-2-21 04:07
假设VB6中,声明的Word对象为objWD

Private Sub Command1_Click()
    Dim wordApp As Word.Application
    Dim myDoc As Word.Document
    Set wordApp = CreateObject("Word.Application")
    wordApp.Visible = True
    Set myDoc = wordApp.Documents.Add()

    With wordApp.ActiveDocument.Styles(wdStyleNormal).Font
        .NameFarEast = "仿宋_GB2312"
        .Size = 16
    End With
    With wordApp.Selection.PageSetup
        .Orientation = wdOrientPortrait
        .TopMargin = CentimetersToPoints(2)
        .BottomMargin = CentimetersToPoints(2)
        .LeftMargin = CentimetersToPoints(2.5)
        .RightMargin = CentimetersToPoints(2)
        .Gutter = CentimetersToPoints(0)
        .HeaderDistance = CentimetersToPoints(1.5)
        .FooterDistance = CentimetersToPoints(1.75)
        .PageWidth = CentimetersToPoints(21)
        .PageHeight = CentimetersToPoints(29.7)
        .SectionStart = wdSectionNewPage
        .OddAndEvenPagesHeaderFooter = False
        .DifferentFirstPageHeaderFooter = False
        .VerticalAlignment = wdAlignVerticalTop
        .SuppressEndnotes = False
        .MirrorMargins = False
        .BookFoldRevPrinting = False
        .BookFoldPrintingSheets = 1
        .GutterPos = wdGutterPosLeft
        .LayoutMode = wdLayoutModeLineGrid
    End With

    With wordApp.Selection
        .Font.Name = "仿宋"
        .Font.Size = 16
        .TypeText Text:="公司党支〔" & Year(Date) & "〕  号"
        .ParagraphFormat.Alignment = wdAlignParagraphCenter
        .TypeParagraph
        .MoveUp Unit:=wdLine, Count:=1
        .Paragraphs(1).SpaceBefore = 200   '值设定范围195-200
        .MoveDown Unit:=wdLine, Count:=1
        .TypeParagraph
        .MoveDown Unit:=wdLine, Count:=1
        .TypeParagraph
        .MoveDown Unit:=wdLine, Count:=1
        .TypeParagraph
        .MoveUp Unit:=wdLine, Count:=1
        .Font.Name = "方正小标宋简体"
        .Font.Size = 22
        .MoveDown Unit:=wdLine, Count:=1
        .TypeParagraph
        .ParagraphFormat.Alignment = wdAlignParagraphLeft
    End With
    wordApp.ActiveDocument.Shapes.AddConnector(msoConnectorStraight, 50, 300, 228, 0#).Select
    With wordApp.Selection.ShapeRange.Line
        .Weight = 1.75
        .Visible = msoTrue
        .Style = msoLineSingle
        .ForeColor.RGB = RGB(255, 0, 0)
        .Visible = msoTrue
    End With
    wordApp.ActiveDocument.Shapes.AddConnector(msoConnectorStraight, 320, 300, 228, 0#).Select
    With wordApp.Selection.ShapeRange.Line
        .Weight = 1.75
        .Visible = msoTrue
        .Style = msoLineSingle
        .ForeColor.RGB = RGB(255, 0, 0)
        .Visible = msoTrue
    End With
    wordApp.ActiveDocument.Shapes.AddShape(msoShape5pointStar, 285, 282, 30, 30).Select
    With wordApp.Selection.ShapeRange
        .Fill.ForeColor.RGB = RGB(255, 0, 0)
        .Fill.Visible = msoTrue
        .Fill.Solid
        .Line.ForeColor.RGB = RGB(255, 0, 0)
        .Line.Visible = msoTrue
    End With
    wordApp.Selection.Collapse
    wordApp.Selection.MoveDown Unit:=wdLine, Count:=3
    myDoc.SaveAs "C:\Documents and Settings\Administrator\桌面\123.doc"
    myDoc.Close True
    wordApp.Quit
    Set myDoc = Nothing
    Set wordApp = Nothing
End Sub
上述代码能够运行,但保存的文档中设置的格式不对,如何手动保存结果是对的,但自动保存的结果就不对了,原因不明。请指点,谢谢!

TA的精华主题

TA的得分主题

发表于 2018-2-22 00:03 | 显示全部楼层
word中运行代码没有问题,理论上来说在VB6中应该就是相同的效果,除非是你的电脑中有多个版本的Word

TA的精华主题

TA的得分主题

 楼主| 发表于 2018-2-22 09:15 | 显示全部楼层
taller 发表于 2018-2-22 00:03
word中运行代码没有问题,理论上来说在VB6中应该就是相同的效果,除非是你的电脑中有多个版本的Word

谢谢版主,问题解决了,好像是电脑的问题,换了台新电脑没有问题了。运行速度慢好像不行。
您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

手机版|关于我们|联系我们|ExcelHome

GMT+8, 2024-3-28 20:05 , Processed in 0.044647 second(s), 8 queries , Gzip On, Redis On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

沪公网安备 31011702000001号 沪ICP备11019229号-2

本论坛言论纯属发表者个人意见,任何违反国家相关法律的言论,本站将协助国家相关部门追究发言者责任!     本站特聘法律顾问:李志群律师

快速回复 返回顶部 返回列表