[英]How to Format Text Style to "Heading 1" while importing all Txt files into Word document
[英]Importing variables from TXT files to a Word document
长篇大论,感谢阅读:)
我正在尝试自动分析 AD 导出。 我向客户发送 PowerShell 脚本以将 AD 数据导出为 CSV。 他们给我发送了 CSV 文件,我将它们导入到一个名为 WeGalvanice ACL(简称 ACL)的分析工具中。 在 ACL 中,我能够过滤/匹配/比较以及我需要做的任何事情。
到目前为止一切都很好。 现在我想使用这些结果并将它们放在一个 .docx 文件中,但麻烦来了。 ACL 不能导出到 docx,只能导出 xlsx/txt/csv/html/json/del。
确定好我们可以变通解决(或所以我想),我做了一个template.docx
是内template.docx
我写的像占位符%certainvariable%
在 ACL 中,我创建了一个命令将我需要的所有变量写入名为%variablename%.txt
文件中,txt 中是我需要的值。
例如:
ACL 创建disabledusers.txt
。 如果你打开 txt 它只显示 10,这意味着我们有 10 个禁用用户。
然后我尝试创建一个 VBA 宏来导入 txt 文件并使用包含的值替换 Word 文档中相应的%...%
文本。
这一切看起来都很顺利,直到我注意到在运行脚本后我得到了20
后跟那个框,意思是“我不知道如何显示这个字符”。
在运行脚本之前:
运行脚本后:
我导入的 .txt 内容的图片:
最后但并非最不重要的是,我使用 word 宏管理器中的 VBS 代码(为方便起见,我只上传了 1 个导入和替换作业,但在我的脚本中,我只是为每个文件和变量复制了 30 次):
Sub ACL()
' ACL Macro
FilePath = "local folderpath\date.txt"
TextFile = FreeFile
Open FilePath For Input As TextFile
new_date = Input(LOF(TextFile), TextFile)
old_date = "%date%"
Close TextFile
With ActiveDocument.Content.Find
.Forward = True
.Wrap = wdFindStop
.Execute findtext:=old_date, replacewith:=new_date, Replace:=wdReplaceAll
End With
End Sub
当我date.txt
打开date.txt
并删除第二行时,它似乎一切正常。 但我希望它自动化。 ;)
我试过
split textfile,/n
和split new_date,/n
都与\\n\\r
和\\r
无济于事。我对任何编程语言的建议持开放态度(我对它们都很糟糕)我只是按照正确的顺序将所有内容排队以使其工作;)
提前谢谢大家! 当然,如果您需要更多信息,请询问
感谢 Tomalak 的更新,我得到了原理:
' removes CR & LF from the end of a string (not pretty but gets the job done)
Function TrimRight(InputStr As String) As String
While Right(InputStr, 1) = vbCr Or Right(InputStr, 1) = vbLf
InputStr = Left(InputStr, Len(InputStr) - 1)
Wend
TrimRight = Trim(InputStr)
End Function
' reads any text file and returns the content as-is
Function ReadFile(FilePath As String) As String
Dim filenum As Integer
If Dir(FilePath) > "" Then
filenum = FreeFile
Open FilePath For Input As filenum
ReadFile = Input(LOF(filenum), filenum)
Close filenum
Else
Debug.Print "File not found: " & FilePath
End If
End Function
' replaces all instances of a single word in the given document
Sub ReplaceAll(oldValue As String, newValue As String, doc As Document)
With doc.Content.Find
.Forward = True
.Wrap = wdFindStop
.Execute findtext:=oldValue, replacewith:=newValue, Replace:=wdReplaceAll
End With
End Sub
Sub ReplaceAllVariables()
Dim variables As Variant, variable As Variant
Dim placeholder As String, realValue As String
variables = Split("customer,c_adgroup,c_adgroup_Cannot_change_PW,c_adgroup_disabled,c_adgroup_enabled,c_adgroup_expire_and_disabled,c_adgroup_LockedOut,c_adgroup_Locked_and_disabled,c_adgroup_loginx,c_adgroup_loginx_disabled,c_adgroup_nolastpasswordset,c_adgroup_nologondate,c_adgroup_PWRQ_and_disabled,c_adgroup_PWx,c_adgroup_PWx_and_disabled,c_adgroup_PW_and_disabled,c_adgroup_PW_and_enabled,c_adgroup_PWRQ_and_enabled ,c_adgroup_PW_not_required,c_adgroup_PW_no_expire,c_adgroup_loginx_and_enabled,c_adusers_f_Cannot_change_PW,c_adusers_f_disabled,c_adusers_f_enabled,c_adusers_f_expire_and_disabled,c_adusers_f_LockedOut,c_adusers_f_Locked_and_disabled,c_adusers_f_loginx,c_adusers_f_loginx_disabled,c_adusers_f_nolastpasswordset,c_adusers_f_nologondate,c_adusers_f_PWRQ_and_disabled,c_adusers_f_PWx,c_adusers_f_PWx_and_disabled,c_adusers_f_PW_and_disabled,c_adusers_f_PW_not_required,c_adusers_f_PW_no_expire,c_adusers_f_loginx_and_enabled,date,c_adusers_f_PW_and_enabled,lastlogon", ",")
For Each variable In variables
placeholder = "%" & variable & "%"
realValue = ReadFile("C:\Users\jerryw\Documents\Klanten\ACL\AD analyse\var\" & variable & ".txt")
realValue = TrimRight(realValue)
ReplaceAll placeholder, realValue, ActiveDocument
Next
End Sub
问题似乎是换行符(CRLF)。 你必须摆脱它。
当我们在做的时候,让我们也摆脱“我已经复制了 30 次代码”这一点,这是解决这个问题的一种可怕的方式。 循环更好。
帮手:
' removes CR & LF from the end of a string (not pretty but gets the job done)
Function TrimRight(InputStr As String) As String
While Right(InputStr, 1) = vbCr Or Right(InputStr, 1) = vbLf
InputStr = Left(InputStr, Len(InputStr) - 1)
Wend
TrimRight = Trim(InputStr)
End Function
' reads any text file and returns the content as-is
Function ReadFile(FilePath As String) As String
Dim filenum As Integer
If Dir(FilePath) > "" Then
filenum = FreeFile
Open FilePath For Input As filenum
ReadFile = Input(LOF(filenum), filenum)
Close filenum
Else
Debug.Print "File not found: " & FilePath
End If
End Function
' replaces all instances of a single word in the given document
Sub ReplaceAll(oldValue As String, newValue As String, doc As Document)
With doc.content.Find
.Forward = True
.Wrap = wdFindStop
.Execute findtext:=oldValue, replacewith:=newValue, Replace:=wdReplaceAll
End With
End Sub
工人:
Sub ReplaceAllVariables()
Dim variables As Variant, variable As Variant
Dim placeholder As String, realValue As String
variables = Split("date,disabledusers,and,any,other,variable,you,have", ",")
For Each variable In variables
placeholder = "%" & variable & "%"
realValue = ReadFile("\local folderpath\" & variable & ".txt")
realValue = TrimRight(realValue)
ReplaceAll placeholder, realValue, ActiveDocument
Next
End Sub
TrimRight()
函数仅从给定的输入字符串中TrimRight()
最后的换行符。 保留在内容中间的任何换行符。 如果您的数据可能会发生这种情况,Word 将为它找到的每个LF
创建新段落。 它将为找到的每个CR
绘制框。
如果您确实有多行 txt 文件,但不想要新的段落,也不想要框,请在使用之前修改文本文件的内容(去掉 CR,用垂直制表符替换 LF ,这是 Word 表示“软中断”的方式,即与 Shift+Enter 得到的结果相同):
realValue = ReadFile("\local folderpath\" & variable & ".txt")
realValue = TrimRight(realValue)
realValue = Replace(Replace(realValue, vbCr, ""), vbLf, vbVerticalTab)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.