如何使用VBA代码将文本文件intp pdf文件转换为文件夹 [英] how to Convert Text file intp pdf file in a folder using VBA code

查看:134
本文介绍了如何使用VBA代码将文本文件intp pdf文件转换为文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Sub convertToWord()

    Dim MyObj As Object,MySource As Object,file As Variant

    file = Dir(" C:\ Text"&" * .txt")'txt path

    Do While(文件<>"")

    ChangeFileOpenDirectory =" C:\ Text2"

  &NBSP; &NBSP; &NBSP; &NBSP; Documents.Open Filename:= file,ConfirmConversions:= False,ReadOnly:= _

  &NBSP; &NBSP; &NBSP; False,AddToRecentFiles:= False,PasswordDocument:="",PasswordTemplate:= _

  &NBSP; &NBSP; &NBSP; "",Revert:= False,WritePasswordDocument:="",WritePasswordTemplate:="",_

  &NBSP; &NBSP; &NBSP;格式:= wdOpenFormatAuto,XMLTransform:=""

  &NBSP; ChangeFileOpenDirectory =" C:\ Text2" '保存路径pdf

  &NBSP; ActiveDocument.SaveAs2文件名:=替换(文件,"。txt","。pdf""),FileFormat:= wdFormatXMLDocument _

  &NBSP; &NBSP; &NBSP; ,LockComments:= False,密码:="",AddToRecentFiles:= True,_

  &NBSP; &NBSP; &NBSP; WritePassword:="",ReadOnlyRecommended:= False,EmbedTrueTypeFonts:= False,_

  &NBSP; &NBSP; &NBSP;   SaveNativePictureFormat:= False,SaveFormsData:= False,SaveAsAOCELetter:= _

  &NBSP; &NBSP; &NBSP;错误,兼容性模式:= 15

  &NBSP; ActiveDocument.Close

  &NBSP;   file = Dir

   循环

结束次要

Sub convertToWord()
   Dim MyObj As Object, MySource As Object, file As Variant
   file = Dir("C:\Text" & "*.txt") 'txt path
   Do While (file <> "")
   ChangeFileOpenDirectory = "C:\Text2"
          Documents.Open Filename:=file, ConfirmConversions:=False, ReadOnly:= _
        False, AddToRecentFiles:=False, PasswordDocument:="", PasswordTemplate:= _
        "", Revert:=False, WritePasswordDocument:="", WritePasswordTemplate:="", _
        Format:=wdOpenFormatAuto, XMLTransform:=""
    ChangeFileOpenDirectory = "C:\Text2" 'path for saving pdf
    ActiveDocument.SaveAs2 Filename:=Replace(file, ".txt", ".pdf"), FileFormat:=wdFormatXMLDocument _
        , LockComments:=False, Password:="", AddToRecentFiles:=True, _
        WritePassword:="", ReadOnlyRecommended:=False, EmbedTrueTypeFonts:=False, _
         SaveNativePictureFormat:=False, SaveFormsData:=False, SaveAsAOCELetter:= _
        False, CompatibilityMode:=15
    ActiveDocument.Close
     file = Dir
   Loop
End Sub

推荐答案

如果要保存PDF,则需要使用适当的保存格式 - 这不是wdFormatXMLDocument!尝试:

If you want to save a PDF, then you need to use the appropriate save format - which isn't wdFormatXMLDocument! Try:

Sub ConvertToPDF()
Dim file As Variant, wdDoc As Document
file = Dir("C:\Text" & "*.txt") 'txt path
Do While (file <> "")
  Set wdDoc = Documents.Open(FileName:="C:\Text" & file, ReadOnly:=True, _
    AddToRecentFiles:=False, Format:=wdOpenFormatAuto, Visible:=False)
  wdDoc.SaveAs2 FileName:="C:\Text2" & Replace(file, ".txt", ".pdf"), _
    FileFormat:=wdFormatPDF, AddToRecentFiles:=False
  wdDoc.Close False
  file = Dir
Loop
End Sub


这篇关于如何使用VBA代码将文本文件intp pdf文件转换为文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆