2017-03-17 82 views
0

我是社區和編程的新手。我主要是涉獵和使用網絡蠻力完成我所需要的。我試圖通過將類似例程整合到一個函數中來清理一些代碼(類似於詳細的流程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.基於變量a調用函數,在文檔末尾完成插入。

我看到的問題: 1.我將代碼重複一遍又一遍地重複使用〜50個形式(暴力方法)。 2.一些文件是肖像和一些風景。 (我認爲這可以是一個變量。) 3.雖然我理解函數的概念(我多次閱讀鏈接),但我不知道如何開始。我知道這應該很容易。

再次,我正在學習,所以請溫柔。如果你必須送我正確的方向。

謝謝 蘭迪

回答

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.因此,這個方向是由接收文件決定的,這個論點根本沒有幫助。您應打開要插入的文檔,查看其方向,然後決定要執行的操作。但這是你的想法。

我在學習代碼時會發現更多的細節。祝你好運!

+0

非常感謝。我現在正在消化這些代碼,以確保我完全理解它並從中學到一些東西。 – RandyB

+0

爲什麼.paragraphs.spacebefore和.spaceafter不影響傳入文件,因爲它用於?我的方法從傳入的文件中刪除了段落格式。這是從選擇變爲範圍的結果嗎?如果有,是否有辦法保留這種能力? – RandyB