vba代码在vb6中运行没有报错但是只显示了个窗体出来

代码的味道 2017-08-07 10:07:06
我想把vba代码打包成exe文件,代码编译通过了但是运行的时候只显示了窗体没有运行代码,求大神们看看怎么回事


Sub word_format()

'新建打开对话框

Dim mydialog As FileDialog

Dim myselecteditem As Variant

Dim doc As Document

Dim yijititle As Paragraph

Dim erjititle As Paragraph

Dim mypicture As Shape

Dim myyemei As Range

Dim myyema As Range

Set mydialog = Application.FileDialog(msoFileDialogOpen) '定义文件选取对话框

With mydialog '设置对话框

.AllowMultiSelect = True '允许多选

.Filters.Clear '清除文件筛选条件

.Filters.Add "文本文件", "*.docx", 1 '添加文件的格式要求

If .Show = -1 Then '单击“确定”按钮,返回-1

Application.ScreenUpdating = False '关闭屏幕刷新

For Each myselecteditem In .SelectedItems

Set doc = Documents.Open(FileName:=myselecteditem, Visible:=False)

With doc '设置文档格式

With .PageSetup '页面设置

.Orientation = wdOrientPortrait '页面方向为纵向

.TopMargin = CentimetersToPoints(2.3) '上边距

.LeftMargin = CentimetersToPoints(2.3) '左边距

.RightMargin = CentimetersToPoints(2.3) '右边距

.BottomMargin = CentimetersToPoints(2.3) '下边距

.HeaderDistance = CentimetersToPoints(1.2) '页眉

.FooterDistance = CentimetersToPoints(1.2) '页脚

End With

With .Content.ParagraphFormat '设置段落格式

.LeftIndent = CentimetersToPoints(0) '左缩进0

.RightIndent = CentimetersToPoints(0) '右缩进0

.LineSpacingRule = wdLineSpaceMultiple

.LineSpacing = LinesToPoints(1.72) '行间距24磅

.Alignment = wdAlignParagraphJustify '对齐方式为两端对齐

.LineUnitAfter = 0 '段后0

.LineUnitBefore = 0 '段前0

.SpaceAfter = 0

.SpaceBefore = 0

.CharacterUnitFirstLineIndent = 2 '首行缩进2字符

End With

With .Content

With .Font '字符格式设置

.NameFarEast = "微软雅黑" '中文字体

.NameAscii = "Times New Roman" '西文字体

.Size = 10.5 '字体大小

.Color = wdColorBlack '字体颜色

End With

With .Paragraphs.First '设置首行题目字体格式

.Range.Font.Size = 22

.Range.Font.NameFarEast = "楷体"

.Range.Font.NameAscii = "Times New Roman"

.Range.Font.Color = wdColorBlack

.Range.Font.Bold = True

.Alignment = wdAlignParagraphCenter

End With

For Each yijititle In .Paragraphs '设置一级标题格式

If yijititle.OutlineLevel = wdOutlineLevel1 Then

With yijititle

.Range.Font.NameFarEast = "幼圆"

.Range.Font.NameAscii = "Times New Roman"

.Range.Font.Size = 18

.Range.Font.Bold = True

.Range.Font.Color = wdColorBlack

.CharacterUnitFirstLineIndent = 0

End With

End If

Next

For Each erjititle In .Paragraphs '设置二级标题格式

If erjititle.OutlineLevel = wdOutlineLevel2 Then

With erjititle

.Range.Font.NameFarEast = "幼圆"

.Range.Font.NameAscii = "Times New Roman"

.Range.Font.Size = 14

.Range.Font.Bold = True

.Range.Font.Color = wdColorBlack

.CharacterUnitFirstLineIndent = 0

End With

End If

Next


End With

Set myyemei = .Sections(1).Headers(wdHeaderFooterPrimary).Range '添加页眉

With myyemei

.Text = "华星在线教育"

.Font.NameFarEast = "楷体"

.Font.Size = 9

.Font.Color = wdColorBlueGray

.Paragraphs.Alignment = wdAlignParagraphCenter

.ParagraphFormat.Borders(3).LineStyle = wdLineStyleNone

End With


Set myyema = .Sections(1).Footers(wdHeaderFooterPrimary).Range '添加页码 第几页

With myyema

.Font.Size = 9

.Font.Name = "Times New Roman"

.Text = "—第 "

.Collapse wdCollapseEnd

.Fields.Add myyema, wdFieldEmpty, "Page"

Set myyema = .Sections(1).Footers(wdHeaderFooterPrimary).Range

myyema.Collapse wdCollapseEnd

myyema.Text = " 页—"

.Fields.Update

.ParagraphFormat.Alignment = wdAlignParagraphCenter

End With

' With .Sections(1) '只添加页码数字
'
' .Footers(wdHeaderFooterPrimary).PageNumbers.Add PageNumberAlignment:=wdAlignPageNumberCenter, FirstPage:=True
'
' End With

.Close True

End With '文档doc设置完成

Next

Application.ScreenUpdating = True

MsgBox "格式化文档操作设置完毕!", vbInformation

Else

MsgBox "已取消操作!", vbInformation '“取消”按钮

End If

End With

End Sub

...全文
345 7 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
代码的味道 2017-08-07
  • 打赏
  • 举报
回复
引用 5 楼 Chen8013 的回复:
[quote=引用 4 楼 qq_37535558 的回复:] [quote=引用 2 楼 qq_37535558 的回复:] 问题是第一次还生成代码运行了,后面几次生成代码都不运行啊
什么意思啊还不能直接用吗[/quote] 还“什么意思”………… 看来就是那么一回事了! 你这个“过程代码”根本没有被执行的机会,它如何“运行”? 你的程序中不是有“窗体”吗?你在那个窗体中再画一个“命令按钮”,然后双击它一下。  这时会弹出“代码编辑窗口”并添加了这个按钮的事件代码,  你只要在“光标”处输入这个就行了: word_format 然后运行程序,用鼠标点一下那个“按钮”,看看是什么结果…… [/quote] 不好意思我不是学vb的,领导叫我把vba代码封装成exe,之前没搞过见笑啦
舉杯邀明月 2017-08-07
  • 打赏
  • 举报
回复
引用 4 楼 qq_37535558 的回复:
[quote=引用 2 楼 qq_37535558 的回复:] 问题是第一次还生成代码运行了,后面几次生成代码都不运行啊
什么意思啊还不能直接用吗[/quote] 还“什么意思”………… 看来就是那么一回事了! 你这个“过程代码”根本没有被执行的机会,它如何“运行”? 你的程序中不是有“窗体”吗?你在那个窗体中再画一个“命令按钮”,然后双击它一下。  这时会弹出“代码编辑窗口”并添加了这个按钮的事件代码,  你只要在“光标”处输入这个就行了: word_format 然后运行程序,用鼠标点一下那个“按钮”,看看是什么结果……
代码的味道 2017-08-07
  • 打赏
  • 举报
回复
引用 2 楼 qq_37535558 的回复:
问题是第一次还生成代码运行了,后面几次生成代码都不运行啊
什么意思啊还不能直接用吗
舉杯邀明月 2017-08-07
  • 打赏
  • 举报
回复
你的VB代码中,调用这个“过程”了吗???
代码的味道 2017-08-07
  • 打赏
  • 举报
回复
问题是第一次还生成代码运行了,后面几次生成代码都不运行啊
代码的味道 2017-08-07
  • 打赏
  • 举报
回复
word和office的引用库也添加了
代码的味道 2017-08-07
  • 打赏
  • 举报
回复
谢谢楼上大哥咯,结贴
谈到宏,很多人想到的是宏病毒,其实如果有正版杀毒软件,打开了实时防护,设置好宏安全性,可以利用宏来完成许多办公自动化功能。   打开word软件,从文件主菜单“工具”->“宏”->“安全性”,设置安全性为,这样就可以选择执行宏。安装office的时候,应该定制安装,在“office 工具”里选上“Visual Basic帮助”,这样就能获得非常完善的关于VBA编程环境、VBA语言参考、VBA对象等的帮助文档。从“工具”->“宏”->“Visual Basic 编辑器”可以打开VBA编程环境——“Microsoft Visual Basic”。在编程环境打开“帮助”->“Microsoft Visual Basic帮助”,就可以看到帮助了。   很多人觉得学习Visual Basic没有用,事实上,Visual Basic系列的编程语言包括VB、VBA、VBScript等,可以写网页、办公自动化编程、应用编程。在一些其他公司的应用软件也是可以用VBA的,例如Corel Draw。这里举几个例子来说明VBA的使用。   如果有数十个文档需要合并为一个文档,逐个打开、拷贝是件麻烦事,可以用宏来完成。 例一:合并文档。新建一个窗体窗体上放一个按钮。工具箱可以从“视图”->“工具箱”打开。 '按钮事件 Private Sub CommandButton1_Click() Dim sPath As String 'sPath用来保存路径 Dim tem As String '间变量 Set newDoc = Documents.Add '用Documents.Add增加一个新文档 With newDoc .SaveAs FileName:="D:/txtal.doc" '新文档保存的完整路径 End With ActiveDocument.close '关闭新建的文档 With Dialogs(wdDialogFileFind) '设置文本查找对话框 .SortBy = 2 '文件按名称排列 .SearchName = "*.txt" '只列出文本文件 .Update '更新 End With If Dialogs(wdDialogFileFind).Show = -1 Then '如果按下打开,就执行 sPath = Dialogs(wdDialogFileFind).SearchPath '将查找对话框打开的路径附值给SPath Dialogs(wdDialogFileFind).Execute Else Exit Sub '如果取消或者关闭,就退出 End If With Application.FileSearch '设置应用程序的查找 .NewSearch '新查找 .LookIn = sPath '将新查找的路径设置为文件对话框的路径(sPath) .SearchSubFolders = True '查找子目录 .FileType = msoFileTypeAllFiles '查找所有文件类型,这里可以设置其他类型,具体见FileSearch对象的FileType属性 .FileName = "*.txt" '查找后缀为txt的文件 .Execute '执行文件查找 Documents.Open ("D:/txtal.doc") '打开"D:/txtal.doc" Set Range2 = ActiveDocument.Content '设置Range2对象为活动文档的内容 For i = 1 To .FoundFiles.Count '把找到的文件打开,拷贝文件内容,加到新建的D:/txtal.doc文件 Documents.Open (.FoundFiles(i)) '打开第i个找到的文件 ActiveDocument.Content.Select '全选活动文件的内容 Selection.Copy '拷贝选的内容 ActiveDocument.Close '关闭活动文档 Range2.Collapse Direction:=wdColla

2,503

社区成员

发帖
与我相关
我的任务
社区描述
VBA(Visual Basic for Applications)是Visual Basic的一种宏语言,是在其桌面应用程序中执行通用的自动化(OLE)任务的编程语言。
社区管理员
  • VBA
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧