2014-09-19 2 views
-1

필자가 작성한이 코드에 문제가 있습니다. 매크로를 실행할 때 "런타임 오류 '424': 필요한 개체"가 표시됩니다.VBA 런타임 오류 424 : Word 문서에 필요한 개체 SaveAs

ActiveDocument.SaveAs filename:="C:\test\test.docx", _ 
FileFormat:=wdFormatXMLDocument, AddToRecentFiles:=False 

줄에 매크로가 나타납니다.

나는 이것이 문서 및 단어 창 상황과 관련이 있다고 가정하지만, 어떻게 해결해야할지 모르겠다.

도움이 될 것입니다. (언급 된 줄 뒤에 나는 어쩌면 사람도 그와 함께 도움이 될뿐만 아니라, 워드 문서를 닫을 필요?)

Dim objword As Object 
Dim objDoc As Object 
Dim fNameAndPath As Variant 
Dim fNameAndPath2 As Variant 

fNameAndPath = "C:\test" 
fNameAndPath2 = "C:\test2" 

i = 2 
While Not IsEmpty(Cells(i, 3)) ' or whevever you want to start 
    If Cells(i, 9) = "End of Probation Per" Then 
     Set objword = CreateObject("Word.Application") 
     objword.Visible = True 
     objword.Documents.Open (fNameAndPath) 
     objword.Activate 
     With objword.ActiveDocument 
      .Bookmarks("EmpName").Range.Text = Cells(i, 2).Value 
      .Bookmarks("EndDate").Range.Text = Cells(i, 11).Value 
       ActiveDocument.SaveAs filename:="C:\test\test.docx", _ 
FileFormat:=wdFormatXMLDocument, AddToRecentFiles:=False  
     End With 
     Else: Cells(i, 9).Font.Italic = True 
    End If 
    i = i + 1 
Wend 

End Sub 

답변

1

당신은 완전히 객체 자격을해야합니다

objWord.ActiveDocument.SaveAs filename:="C:\test\test.docx", _ 
FileFormat:=wdFormatXMLDocument, AddToRecentFiles:=False  

는 또한 경우에주의를 당신은 Word 개체 라이브러리에 대한 참조를 설정하지 않은 상수 wdFormatXMLDocument 값이없는, 그래서 당신은 그것을 정의한다 :

Const wdFormatXMLDocument As Long = 12 

을 마지막 코멘트 다시, 당신은 Fi를위한 셀 값을 포함 할 수 있습니다 파일 이름 :

objWord.ActiveDocument.SaveAs filename:="C:\test\" & cells(i, 2).value & ".docx", _ 
FileFormat:=wdFormatXMLDocument, AddToRecentFiles:=False 
+0

이 경우 FileFormat의 이름을 어떻게 지정 하시겠습니까? 난 그냥 docx 문서를 원한다. – Casz146

+0

'FileFormat : = 12'을 사용하거나, 코드 상단에 상수를 추가하십시오 :'Const wdFormatXMLDocument As Long = 12' – Rory

+0

고마워요! 다른 질문을하겠습니다. 각 행에 개인 파일 이름을 만들 수 있도록 Cells (i, 2)의 값을 새 파일 이름에 배치하려면 코드가 어떻게됩니까? 책갈피 값을 사용하거나 오히려 Excel 값을 사용해야합니까? – Casz146

관련 문제