2017-03-17 1 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. 변수 끝에 따라 문서의 끝에 삽입을 수행하는 함수 호출.

내가보기에 문제 : 1. 나는 약 50 가지 형식 (무차별 방식)으로 반복해서 코드를 반복했다. 2. 일부 문서는 세로 및 가로 방향입니다. (저는 이것이 변수 일 수 있다고 가정합니다.) 3. Function (내가 여러 번 링크를 읽었습니다.)의 개념을 이해하는 동안, 어떻게 시작해야할지 모르겠습니다. 나는 그것이 쉬워야한다는 것을 안다.

다시 한 번 배우고 있습니다. 친절하시기 바랍니다. 나를 올바른 방향으로 보내야한다면.

는 랜디

답변

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 

두 번째 절차는 주로 이미 갖고있는 절차입니다. Selection 개체보다는 Range 개체를 사용하도록 변경했습니다. 당신은 (거의) 그것을 조작하기 위해 아무것도 선택할 필요가 없습니다. 모든 것이 예상대로 수행되면 True를 반환하는 함수로 만들었습니다.

이 절차에서는 파일 이름과 방향이라는 두 가지 인수를 사용합니다. 프리젠 테이션 파일을 세로 그림으로 가져 오거나 v.v. 파일을 가져 오는 경우 문제가 많기 때문에 방향이 논리적 오류라고 생각합니다. 따라서 오리엔테이션은 수신 문서에 의해 결정되며 논점은 전혀 도움이되지 않습니다. 삽입 할 문서를 열고 방향을 확인한 다음 수행 할 작업을 결정해야합니다. 그러나 이것은 당신의 생각이었습니다.

코드를 공부할 때 발견 할 수있는 몇 가지 사항을 추가했습니다. 행운을 빕니다!

+0

대단히 감사합니다. 지금 코드를 소화하고 있습니다. 완전히 이해하고 무언가를 배웁니다. – RandyB

+0

.paragraphs.spacebefore와 .spaceafter는 들어오는 파일에 영향을주지 않는 이유는 무엇입니까? 내 방법은 들어오는 파일에서 단락 서식을 제거했습니다. 이것은 선택에서 범위로 변경된 결과입니까? 그렇다면이 능력을 유지할 수있는 방법이 있습니까? – RandyB