英文:
Modifying text using Word VBA
问题
以下是您提供的脚本的修改部分,用于将文件中所有粗体文本替换为"HEADING:"加上原始文本:
Sub GetTextFromWord()
Dim fso As FileSystemObject
Dim oWd As Object, oDoc As Object
Const wdFormatText As Long = 2, wdCRLF As Long = 0
Set fso = New FileSystemObject
Set oWd = CreateObject("word.application")
Set oDoc = oWd.Documents.Open("C:\temp\PDFs\New folder\Test")
Dim filePath As String: filePath = "C:\temp\PDFs\New folder\" & "TEST" & ".txt" '文件名
Debug.Print filePath
With oDoc.Content.Find
.ClearFormatting
.Font.Bold = True
.Text = ""
.Replacement.Text = "HEADING: ^&"
.Format = True
.Execute Replace:=wdReplaceAll
End With
oDoc.SaveAs2 Filename:=filePath, _
FileFormat:=wdFormatText, LockComments:=False, Password:="", _
AddToRecentFiles:=True, WritePassword:="", ReadOnlyRecommended:=False, _
EmbedTrueTypeFonts:=False, SaveNativePictureFormat:=False, SaveFormsData _
:=False, SaveAsAOCELetter:=False, Encoding:=1252, InsertLineBreaks:=False _
, AllowSubstitutions:=True, LineEnding:=wdCRLF, CompatibilityMode:=0
oDoc.Close False
oWd.Quit
End Sub
这个修改后的脚本应该会将粗体文本替换为"HEADING:"加上原始文本并保存到文件中。确保您已保存并运行了这个修改后的脚本。
英文:
I have the following working script which extract the text from a Word file:
Sub GetTextFromWord()
Dim fso As FileSystemObject
Dim oWd As Object, oDoc As Object
Const wdFormatText as Long = 2, wdCRLF as Long = 0
Set fso = New FileSystemObject
Set oWd = CreateObject("word.application")
Set oDoc = oWd.Documents.Open("C:\temp\PDFs\New folder\Test")
Dim filePath As String: filePath = "C:\temp\PDFs\New folder\Test" & "TEST" & ".txt" 'filename
Debug.Print filePath
oDoc.SaveAs2 fileName:=filePath, _
FileFormat:=wdFormatText, LockComments:=False, Password:="", _
AddToRecentFiles:=True, WritePassword:="", ReadOnlyRecommended:=False, _
EmbedTrueTypeFonts:=False, SaveNativePictureFormat:=False, SaveFormsData _
:=False, SaveAsAOCELetter:=False, Encoding:=1252, InsertLineBreaks:=False _
, AllowSubstitutions:=True, LineEnding:=wdCRLF, CompatibilityMode:=0
oDoc.Close False
oWd.Quit
End Sub
I wish to modify this so that for every instance of Bold text in the file this is replaced with "HEADING:" original text...
Attempt so Far:
Sub GetTextFromWord()
Dim fso As FileSystemObject
Dim oWd As Object, oDoc As Object
Const wdFormatText As Long = 2, wdCRLF As Long = 0
Set fso = New FileSystemObject
Set oWd = CreateObject("word.application")
Set oDoc = oWd.Documents.Open("C:\temp\PDFs\New folder\Test")
Dim filePath As String: filePath = "C:\temp\PDFs\New folder\" & "TEST" & ".txt" 'filename
Debug.Print filePath
With oDoc.Content.Find
.ClearFormatting
.Font.Bold = True
.Execute FindText:="", ReplaceWith:="HEADER: ^&", _
Format:=True, Replace:=wdReplaceAll
End With
oDoc.SaveAs2 Filename:=filePath, _
FileFormat:=wdFormatText, LockComments:=False, Password:="", _
AddToRecentFiles:=True, WritePassword:="", ReadOnlyRecommended:=False, _
EmbedTrueTypeFonts:=False, SaveNativePictureFormat:=False, SaveFormsData _
:=False, SaveAsAOCELetter:=False, Encoding:=1252, InsertLineBreaks:=False _
, AllowSubstitutions:=True, LineEnding:=wdCRLF, CompatibilityMode:=0
oDoc.Close False
oWd.Quit
End Sub
This generates the TEST file and shows no error but checking the file the changes have not been made.
答案1
得分: 2
以下是代码的中文翻译部分:
Sub Tester()
Dim doc As Object
Set doc = ActiveDocument
PrependBoldText doc, "HEADER: "
End Sub
Sub PrependBoldText(doc As Object, prefix As String)
Dim rng As Object
Set rng = doc.Content
With rng.Find
.ClearFormatting
.Format = True
.Font.Bold = True
.Wrap = 0 'wdFindStop
Do While .Execute
'rng is now the range for the found text...
rng.Text = prefix & rng.Text
Loop
End With
End Sub
希望这有所帮助。
英文:
Tested in Word:
Sub Tester()
Dim doc As Object
Set doc = ActiveDocument
PrependBoldText doc, "HEADER: "
End Sub
Sub PrependBoldText(doc As Object, prefix As String)
Dim rng As Object
Set rng = doc.Content
With rng.Find
.ClearFormatting
.Format = True
.Font.Bold = True
.Wrap = 0 'wdFindStop
Do While .Execute
'rng is now the range for the found text...
rng.Text = prefix & rng.Text
Loop
End With
End Sub
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论