I am new to the community and to programming in general. I mainly have dabbled and used the web to brute force accomplish what I need. I am trying to clean up some code by consolidating like routines into a function (similar to the process detailed here。)但我不知道如何开始。现在编写的代码反复重复,以在文档的末尾插入不同的表单。
Sub InsertQA17()
If ActiveDocument.Bookmarks.Exists("\EndOfDoc") = True Then
ActiveDocument.Bookmarks("\EndOfDoc").Select
Selection.Collapse Direction:=wdCollapseEnd
Selection.Range.InsertBreak (wdSectionBreakNextPage)
Selection.PageSetup.Orientation = wdOrientPortrait
Selection.Style.ParagraphFormat.SpaceAfter = 0
Selection.Style.ParagraphFormat.SpaceBefore = 0
'OQE Form below needs to be changed to reflect the sub called
Selection.InsertFile FileName:=OQEPath & _
"\QA17.doc", Range:="", ConfirmConversions:=False, Link:=False, Attachment:=False
UpdateThisFormsFields
Else
MsgBox "Bookmark ""\EndOfDoc"" does not exist!"
End If
End Sub
Sub InsertWELDRECORD()
If ActiveDocument.Bookmarks.Exists("\EndOfDoc") = True Then
ActiveDocument.Bookmarks("\EndOfDoc").Select
Selection.Collapse Direction:=wdCollapseEnd
Selection.Range.InsertBreak (wdSectionBreakNextPage)
Selection.PageSetup.Orientation = wdOrientLandscape
Selection.Style.ParagraphFormat.SpaceAfter = 0
Selection.Style.ParagraphFormat.SpaceBefore = 0
'OQE Form below needs to be changed to reflect the sub called
Selection.InsertFile FileName:=OQEPath & _
"\WELDRECORD.doc", Range:="", ConfirmConversions:=False, Link:=False, Attachment:=False
UpdateThisFormsFields
Else
MsgBox "Bookmark ""\EndOfDoc"" does not exist!"
End If
End Sub
我认为我想要清理代码是 1.一个函数,它包含接收带状按钮时传递给它的变量的代码的内容。 2.根据变量调用函数,完成文档末尾的插入。
我看到的问题: 我已经一遍又一遍地重复了50个表格(蛮力方法)。 有些文件是肖像和一些风景。 (我认为这可能是一个变量。) 3.虽然我理解一个函数的概念(我多次阅读该链接),但我不知道如何开始。我知道应该很容易。
同样,我正在学习,所以请保持温柔。如果你必须向我发送正确的方向。
谢谢你, 好色
答案 0 :(得分:0)
行。这是您请求的代码。有两个程序。请注意,第一个是Public,因为您可能希望从键盘调用它。另一个是私有的,因为除了第一个之外它永远不会被调用。将两者放在普通代码模块中(默认为“Module1”)。
Sub Call_InsertFile()
Dim Done As Boolean
Dim Ffn As String ' full file name (incl path)
Dim Orient As WdOrientation
Ffn = "D:\My Documents\STO 170112 Topo's Recipe.docx"
Orient = wdOrientPortrait
If Len(Dir(Ffn)) Then
Done = InsertFile(Ffn, Orient)
If Not Done Then
MsgBox "Sorry, the insert failed.", vbCritical, "Document error"
End If
Else
MsgBox "A file by the name of" & vbCr & _
Ffn & vbCr & _
"doesn't exist.", vbInformation, "Invalid file info"
End If
End Sub
Private Function InsertFile(ByVal Ffn As String, _
ByVal Orient As WdOrientation) As Boolean
' 17 Mar 2017
Dim Rng As Range
With ActiveDocument
If .Bookmarks.Exists("\EndOfDoc") Then
Set Rng = .Bookmarks("\EndOfDoc").Range
' this range has zero length: start and end are the same
With Rng
.PageSetup.Orientation = Orient
With .Paragraphs(1)
.SpaceBefore = 0
.SpaceAfter = 0
End With
On Error Resume Next
' doesn't insert a page break if document is blank
If .End > 0 Then .InsertBreak wdSectionBreakNextPage
.InsertFile Ffn
InsertFile = Not CBool(Err.Number)
End With
Else
MsgBox "Bookmark ""\EndOfDoc"" does not exist!"
End If
End With
End Function
第二个程序很大程度上是你已经拥有的。我将其更改为使用Range对象而不是Selection对象。你(几乎)不需要选择任何东西来操纵它。我把它变成了一个函数,如果一切按预期完成,它将返回True。
此过程采用两个参数:文件名和方向。我认为方向是一个逻辑错误,因为有很多问题,大概是,如果你将一个横向文件导入一个纵向文档,或v.v.因此,方向由接收文件决定,参数根本没有帮助。您应该打开要插入的文档,查看其方向,然后决定要执行的操作。但这是你的主意。
我添加了一些你在学习代码时会发现的细节。祝你好运!