2012-11-27 9 views
0

많은 오래된 .DOC 파일을 PDF 형식 또는 RTF 형식으로 변환하려고합니다. 지금까지 나는 후자 (RTF 로의 변환)를 수행하는 것을 찾았지만 오래된 Word 응용 프로그램의 서식은 여전히 ​​문서에 남아 있습니다. Microsoft Word (2010을 사용 중입니다)를 열고 파일> 열기를 클릭하면 "모든 파일의 텍스트 복구 (.)"를 선택할 수있는 드롭 다운 메뉴가 나타납니다. .DOC 문서의 서식 지정 데이터를 필터링하기 위해 변환 프로세스에서이 기능을 사용할 수 있습니까?"모든 파일의 텍스트 복구"모드에서 .doc을 여는 VBA

이 하나가 근무하고있다 그것은 단지 파일의 끝에 .RTF 추가보다는 형식 변경 것으로 나타납니다하지만 :

Sub SaveAllAsDOCX() 
Dim strFilename As String 
Dim strDocName As String 
Dim strPath As String 
Dim oDoc As Document 
Dim fDialog As FileDialog 
Dim intPos As Integer 
Set fDialog = Application.FileDialog(msoFileDialogFolderPicker) 
With fDialog 
    .Title = "Select folder and click OK" 
    .AllowMultiSelect = False 
    ..InitialView = msoFileDialogViewList 
    If .Show <> -1 Then 
     MsgBox "Cancelled By User", , "List Folder Contents" 
     Exit Sub 
    End If 
    strPath = fDialog.SelectedItems.Item(1) 
    If Right(strPath, 1) <> "\" Then strPath = strPath + "\" 
End With 
If Documents.Count > 0 Then 
    Documents.Close SaveChanges:=wdPromptToSaveChanges 
End If 
If Left(strPath, 1) = Chr(34) Then 
    strPath = Mid(strPath, 2, Len(strPath) - 2) 
End If 
strFilename = Dir$(strPath & "*.doc") 
While Len(strFilename) <> 0 
    Set oDoc = Documents.Open(strPath & strFilename) 
    strDocName = ActiveDocument.FullName 
    intPos = InStrRev(strDocName, ".") 
    strDocName = Left(strDocName, intPos - 1) 
    strDocName = strDocName & ".docx" 
    oDoc.SaveAs FileName:=strDocName, _ 
     FileFormat:=wdFormatDocumentDefault 
    oDoc.Close SaveChanges:=wdDoNotSaveChanges 
    strFilename = Dir$() 
Wend 
End Sub 
을 아래 내가 현재 수정하려고 스크립트의 몇 가지 예를

이 사람은 어떤 전환이 지금까지 성공하지 :

Option Explicit 
Sub ChangeDocsToTxtOrRTFOrHTML() 
'with export to PDF in Word 2007 
    Dim fs As Object 
    Dim oFolder As Object 
    Dim tFolder As Object 
    Dim oFile As Object 
    Dim strDocName As String 
    Dim intPos As Integer 
    Dim locFolder As String 
    Dim fileType As String 
    On Error Resume Next 
    locFolder = InputBox("Enter the folder path to DOCs", "File Conversion", "C:\myDocs") 
    Select Case Application.Version 
     Case Is < 12 
      Do 
       fileType = UCase(InputBox("Change DOC to TXT, RTF, HTML", "File Conversion", "TXT")) 
      Loop Until (fileType = "TXT" Or fileType = "RTF" Or fileType = "HTML") 
     Case Is >= 12 
      Do 
       fileType = UCase(InputBox("Change DOC to TXT, RTF, HTML or PDF(2007+ only)", "File Conversion", "TXT")) 
      Loop Until (fileType = "TXT" Or fileType = "RTF" Or fileType = "HTML" Or fileType = "PDF") 
    End Select 
    Application.ScreenUpdating = False 
    Set fs = CreateObject("Scripting.FileSystemObject") 
    Set oFolder = fs.GetFolder(locFolder) 
    Set tFolder = fs.CreateFolder(locFolder & "Converted") 
    Set tFolder = fs.GetFolder(locFolder & "Converted") 
    For Each oFile In oFolder.Files 
     Dim d As Document 
     Set d = Application.Documents.Open(oFile.Path) 
     strDocName = ActiveDocument.Name 
     intPos = InStrRev(strDocName, ".") 
     strDocName = Left(strDocName, intPos - 1) 
     ChangeFileOpenDirectory tFolder 
     Select Case fileType 
     Case Is = "TXT" 
      strDocName = strDocName & ".txt" 
      ActiveDocument.SaveAs FileName:=strDocName, FileFormat:=wdFormatText 
     Case Is = "RTF" 
      strDocName = strDocName & ".rtf" 
      ActiveDocument.SaveAs FileName:=strDocName, FileFormat:=wdFormatRTF 
     Case Is = "HTML" 
      strDocName = strDocName & ".html" 
      ActiveDocument.SaveAs FileName:=strDocName, FileFormat:=wdFormatFilteredHTML 
     Case Is = "PDF" 
      strDocName = strDocName & ".pdf" 

      ' *** Word 2007 users - remove the apostrophe at the start of the next line *** 
      'ActiveDocument.ExportAsFixedFormat OutputFileName:=strDocName, ExportFormat:=wdExportFormatPDF 

     End Select 
     d.Close 
     ChangeFileOpenDirectory oFolder 
    Next oFile 
    Application.ScreenUpdating = True 
End Sub 

답변

1

난 당신이 원하는 일을하려면 VBA 스크립트를 사용하여, 하나의 방법을 다룰 것입니다, 사용하지 않고 Word의 기본 "에서 텍스트를 복구 모든 파일 "모드 기능.

한 디렉터리의 모든 .doc/.docx를 .txt로 변환하지만 상위 응용 프로그램에서 지원하는 다른 형식으로 변환하는 데 사용할 수 있습니다 (Word 2010으로 테스트 됨). 다음과 같이

'------------ VBA script start ------------- 
Sub one1() 
Set fs = CreateObject("Scripting.FileSystemObject") 
Set list1 = fs.GetFolder(ActiveDocument.Path) 
For Each fl In list1.files 
    If InStr(fl.Type, "Word") >= 1 And Not fl.Path = ActiveDocument.Path & "\" & ActiveDocument.Name Then 
    Set wordapp = CreateObject("word.Application") 
    Set Doc1 = wordapp.Documents.Open(fl.Path) 
    'wordapp.Visible = True 
    Doc1.SaveAs2 FileName:=fl.Name & ".txt", fileformat:=wdFormatText 
    wordapp.Quit 
    End If 
Next 
End Sub 
'------------ VBA script start ------------- 

은, 말하자면, PDF로 저장

Doc1.SaveAs2 FileName:=fl.Name & ".pdf", fileformat:=wdFormatPDF 

대신

이 RTF로 저장하는 데 사용,

Doc1.SaveAs2 FileName:=fl.Name & ".rtf", fileformat:=wdFormatRTF 

대신

를 사용하거나하기 HTML :

Doc1.SaveAs2 FileName:=fl.Name & ".html", fileformat:=wdFormatHTML 

등등. 그들은 해가되지 않는 때문에 내가 검사를 귀찮게하지 않았다

일부 단점 :

  • 오류 메시지가 팝업 실행의 끝에서

    하지만, 아무런 결과와.

  • 문서 자체 내부의 VBA 스크립트이기 때문에 자체적으로 열려고 시도하며 문서 열기 스크립트입니다. 그리고 나서 메시지가 나타날 때 읽기 전용으로 mannally 열도록 'him'에게 지시해야합니다.

  • 모든 문서를 C : \ users \ username \ Documents에 저장합니다 (실행되는 곳이 아닌 대부분의 경우 더 좋을 것입니다).

  • 느린 프로세스는 대부분의 일반 개인용 컴퓨터에서 2-3 문서/초의 속도를 기대합니다.