Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 01-17-2018, 12:57 PM
puff puff is offline How to express "or" in VBA? (code included) Windows 7 64bit How to express "or" in VBA? (code included) Office 2013
Advanced Beginner
How to express "or" in VBA? (code included)
 
Join Date: Apr 2017
Posts: 60
puff is on a distinguished road
Default How to express "or" in VBA? (code included)

Hi all. I have a document that has some empty lines start with "uppercase letter + ." like this:
2018-01-17_114845.png
Or simply start with "." like this:
2018-01-17_114818.png

My code so far can only remove the 2nd case
Code:
Sub DeleteLinesStartWith()

Dim oPara As Paragraph
Dim oRng As Range
Dim i As Integer
    For Each oPara In ActiveDocument.Paragraphs
        Set oRng = oPara.Range.Words(1)
        If LCase(Trim(oRng.Text)) = "." Then
            oPara.Range.Delete
        End If
    Next oPara
End Sub
1. The doc actually has other lines that I want to delete starting with some other signs, like "!". So how can I create a list/collection in the judging line for the macro?


If LCase(Trim(oRng.Text)) = "." Then

2. Some signs cannot be written in the VBA, like "•". In this case then, how can I delete an empty line start with "•"? Use the Unicode somehow?

Thank you very much.
Reply With Quote
  #2  
Old 01-17-2018, 03:40 PM
macropod's Avatar
macropod macropod is offline How to express "or" in VBA? (code included) Windows 7 64bit How to express "or" in VBA? (code included) Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

You don't need an OR test for what you've described; you need a LIKE test:
Code:
Sub DeleteLinesStartWith()
Dim i As Long
With ActiveDocument
  For i = .Paragraphs.Count To 1 Step -1
    With .Paragraphs(i).Range
      If LCase(Trim(.Words.First.Text)) Like "[!.•]" Then .Delete
    End With
  Next i
End With
End Sub
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #3  
Old 01-17-2018, 04:01 PM
puff puff is offline How to express "or" in VBA? (code included) Windows 7 64bit How to express "or" in VBA? (code included) Office 2013
Advanced Beginner
How to express "or" in VBA? (code included)
 
Join Date: Apr 2017
Posts: 60
puff is on a distinguished road
Default

Quote:
Originally Posted by macropod View Post
You don't need an OR test for what you've described; you need a LIKE test:
Code:
Sub DeleteLinesStartWith()
Dim i As Long
With ActiveDocument
  For i = .Paragraphs.Count To 1 Step -1
    With .Paragraphs(i).Range
      If LCase(Trim(.Words.First.Text)) Like "[!.•]" Then .Delete
    End With
  Next i
End With
End Sub
I tried your code, and it doesn't work on empty lines start with "letter." like "A." although I have changed it to Like "[.1.2.3.4.A.B.C.D.©.!.•]. I changed it because I want to keep the original real empty lines--without anything in the front.

And also, I want to delete empty lines start with letters, like "B". In that case then how could VBA tell the difference between "A." and "A" if you use Like?
Reply With Quote
  #4  
Old 01-17-2018, 04:30 PM
macropod's Avatar
macropod macropod is offline How to express "or" in VBA? (code included) Windows 7 64bit How to express "or" in VBA? (code included) Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Perhaps you should spend a few moments studying how the Like command works (it's there, in the VBA help file) instead of cobbling together whatever comes out of a thought bubble then complaining the code doesn't work. It works for what you described in your first post's second screenshot and the numbered paragraphs, which isn't what you're now saying it doesn't work with. Quite obviously, if you want to delete paragraphs like your first screenshot, another test is required. For example:
If LCase(Trim(.Words.First.Text)) Like "[A-Z][!.•]" Then .Delete

In any event, if what you're trying to do is to delete paragraphs with fewer than, say, 3 characters, an entirely different approach is called for. I'd be inclined to do it with a simple wildcard Find/Replace, which would be far faster than looping through all the paragraphs.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
"Internet Connection Needed ... Error Code: 0-9" When Trying to use Online Repair millardjd Office 0 01-22-2015 07:54 AM
How to express "or" in VBA? (code included) How to get "Track Changes" to ignore "Field Code Changed"? Joey Cheung Word 2 10-31-2014 07:55 PM
Cross Reference Heading Number with the word "Section" included bblouin Word 5 12-20-2012 05:27 PM
How to express "or" in VBA? (code included) code "0020" appearing instead of space chracter in word docm, file radleyyeldar Word 1 07-30-2012 05:13 AM
How to express "or" in VBA? (code included) How to choose a "List" for certain "Heading" from "Modify" tool? Jamal NUMAN Word 2 07-03-2011 03:11 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 05:33 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft