PDA

View Full Version : Automatic editing



lMerlo
02-11-2020, 02:38 AM
Hi, this mounth I'll Have to edit some text and I Would like not to do this manually so i decided to write a vba code to do the job but i had some trobles.
What i would like to have is :







Dim shp As Shape
For Each shp In ActiveDocument.Shapes
shp.Delete
Next shp



I'm importing a pdf in word so some figure will be imported as a lot of text boxes. so i would like all the figure and text boxes to be remouved. But the code doesn't do this action always



Dim numPara As Long
numPara = ActiveDocument.Paragraphs.Count
Dim aRange As Range
For i = numPara To 1 Step -1
Set aRange = ActiveDocument.Paragraphs(1).Range
If Len(aRange) <= 1 Then oPara.Delete
End If


This code wold have to remouve the paragraph if i have something like


But i think there is a problem with the use of len applied to a paragraph




Dim numPara As Integer
numPara = ActiveDocument.Paragraphs.Count
For i =numPara to 1 Step -1
If (ActiveDocument.Paragraphs(i).Format.Style Like "List Paragraph") Then
ActiveDocument.Paragraphs(i).Range.ListFormat.ConvertNumbersToText
End If
Next i



I wold like to obtain at the end something like

1 Heading 1
1.1 Heading 2
1.1.1 Heading 3
1.1.1.1 heading 4
1.1.1.1.1 Normal, or body text
For me it is important that even if i modefy the tytle structure, the number like 1.1.1.1.1 won't change
Actually this the only part that function



Dim numPara As Integer
Dim final As Integer
Dim str As String
Dim cond1 As Boolean
Dim cond2 As Boolean
Dim head1 As Style, head2 As Style, head3 As Style, head4 As Style
numPara = ActiveDocument.Paragraphs.Count
final = numPara - 500
For i = numPara To final Step -1
str = ActiveDocument.Paragraphs(i).Range.ListFormat.ListString
Set head1 = ActiveDocument.Styles("Heading 1")
Set head2 = ActiveDocument.Styles("Heading 2")
Set head3 = ActiveDocument.Styles("Heading 3")
Set head4 = ActiveDocument.Styles("Heading 4")
cond1 = ActiveDocument.Paragraphs(i).Format.Style Like "Normal"
cond2 = ActiveDocument.Paragraphs(i).Format.Style Like "Body Text"
cond3 = Not cond1
cond4 = Not cond2
If cond1 Or cond2 Then
Debug.Print i
If str Like "#[.]#" Then ActiveDocument.Paragraphs(i).Style = wdStyleHeading2
If str Like "#[.]#[.]#" Then ActiveDocument.Paragraphs(i).Style = wdStyleHeading3
If str Like "#[.]#[.]#[.]#" Then ActiveDocument.Paragraphs(i).Style = wdStyleHeading4
End If
Next i

When the pdf is impoted it may happen that the heading level are matched to the wrong Heading so i want to check the type of index and change the heading.
Do you have and idea of what to do if i find a document where the title are not even heading ?
Thanks for your help

gmaxey
02-12-2020, 09:04 AM
Here is a start.


Sub ScratchMacro()
'A basic Word macro coded by Greg Maxey
Dim oPar As Paragraph
Dim lngIndex As Long
Dim oILS As InlineShape
Dim oShp As Shape
For lngIndex = ActiveDocument.Paragraphs.Count - 1 To 1 Step -1
Set oPar = ActiveDocument.Paragraphs(lngIndex)
If Len(oPar.Range.Text) < 2 Then oPar.Range.Delete
Next lngIndex
For lngIndex = ActiveDocument.InlineShapes.Count To 1 Step -1
ActiveDocument.InlineShapes(lngIndex).Delete
Next lngIndex
For lngIndex = ActiveDocument.Range.ShapeRange.Count To 1 Step -1
ActiveDocument.Range.ShapeRange(lngIndex).Delete
Next lngIndex
lbl_Exit:
Exit Sub
End Sub

I have no idea what you are talking about after that point.

Now, I am going to go here: http://www.vbaexpress.com/forum/showthread.php?66774-Automatic-editing and warn anyone who might see your cross post not to waste their time trying to answer something already partially answered here.