Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 09-24-2018, 10:44 AM
ashalles135 ashalles135 is offline Word macro to insert text at the beginning of paragraph but skip tables Windows 7 64bit Word macro to insert text at the beginning of paragraph but skip tables Office 2010 64bit
Novice
Word macro to insert text at the beginning of paragraph but skip tables
 
Join Date: Sep 2018
Posts: 4
ashalles135 is on a distinguished road
Default Word macro to insert text at the beginning of paragraph but skip tables

Hi,



I am trying to modify the following macro to skip all tables in a document.

I tried to add "tables" with the rest of the text that is ignored, but it still puts a "(U) " in each cell.

Our text gets dumped out of another program and everything is styled "Normal"... including the tables. If I style the table text, then all the dollar amounts become left justified, which is time-intensive to fix.

Does anyone have any suggestions?


Code:
With ActiveDocument.Content.Find
    .ClearFormatting
    .Style = wdStyleNormal
    Do While .Execute(Forward:=True, Format:=True) = True
      With .Parent
  If Left(.Text, 1) = vbCr Or Left(.Text, 1) = " " Or Left(.Text, 1) = chr(12) Or (wdWithInTable) Then
          'do nothing
        Else
          .InsertBefore "(U) "
        End If
        
        If .End = ActiveDocument.Content.End Then
          Exit Do
        Else
          .Move unit:=wdParagraph, Count:=1
        End If
      End With
    Loop
  End With
End Sub

Last edited by macropod; 09-24-2018 at 05:17 PM. Reason: Added code tags & formatting
Reply With Quote
  #2  
Old 09-24-2018, 05:28 PM
macropod's Avatar
macropod macropod is offline Word macro to insert text at the beginning of paragraph but skip tables Windows 7 64bit Word macro to insert text at the beginning of paragraph but skip tables 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

It's not exactly clear what you're trying to do, since nothing about your code has anything to do with justification vis-à-vis "all the dollar amounts become left justified". Try:
Code:
Sub Demo()
Application.ScreenUpdating = False
With ActiveDocument.Range
  With .Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Text = "[!^13]@^13"
    .Replacement.Text = ""
    .Forward = True
    .Wrap = wdFindStop
    .Format = False
    .MatchWildcards = True
    .Execute
  End With
  Do While .Find.Found
    If .Information(wdWithInTable) = False Then
      Select Case .Characters.First
        Case Chr(32), vbCr
        Case Else: .InsertBefore "(U) "
      End Select
    Else
      .End = .Tables(1).Range.End
    End If
    .Collapse wdCollapseEnd
    .Find.Execute
  Loop
End With
Application.ScreenUpdating = True
End Sub
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #3  
Old 09-25-2018, 05:00 AM
ashalles135 ashalles135 is offline Word macro to insert text at the beginning of paragraph but skip tables Windows 7 64bit Word macro to insert text at the beginning of paragraph but skip tables Office 2010 64bit
Novice
Word macro to insert text at the beginning of paragraph but skip tables
 
Join Date: Sep 2018
Posts: 4
ashalles135 is on a distinguished road
Default Thanks, but not doing what I was hoping for

I apologize for not being clear. I added more description, but that seems to have made it more confusing. We have a separate macro that formats the tables, so I don't want this to format the tables.

The macro I posted adds a "(U) " to everything styled "normal" which includes every cell in every table.

I was hoping for code that would skip anything in a table.

I tried the macro you posted, and was patient for a while, but it seemed to just be locked up. So I ended the task and when I opened up the test file again, it had hundreds of "(U) "s in the table of contents. But nothing in the text.

I can't send the Word file, but I attached one with a screen shot.
Attached Files
File Type: docx Us in toc.docx (65.1 KB, 7 views)
Reply With Quote
  #4  
Old 09-25-2018, 05:27 AM
ashalles135 ashalles135 is offline Word macro to insert text at the beginning of paragraph but skip tables Windows 7 64bit Word macro to insert text at the beginning of paragraph but skip tables Office 2010 64bit
Novice
Word macro to insert text at the beginning of paragraph but skip tables
 
Join Date: Sep 2018
Posts: 4
ashalles135 is on a distinguished road
Default

YES!! So, I kept trying things this morning and eventually got the macro working.

Thank you! I was able to find the piece I needed from within your code.

I added:
Or .Information(wdWithInTable) = True
to the statement that ignores certain things in the document.

Complete macro below:


With ActiveDocument.Content.Find
.ClearFormatting
.Style = wdStyleNormal
Do While .Execute(Forward:=True, Format:=True) = True
With .Parent
If Left(.Text, 1) = vbCr Or Left(.Text, 1) = " " Or Left(.Text, 1) = chr(12) Or .Information(wdWithInTable) = True Then
'do nothing
Else
.InsertBefore "(U) "
End If

If .End = ActiveDocument.Content.End Then
Exit Do
Else
.Move unit:=wdParagraph, Count:=1
End If
End With
Loop
End With
End Sub
Reply With Quote
  #5  
Old 09-25-2018, 02:25 PM
macropod's Avatar
macropod macropod is offline Word macro to insert text at the beginning of paragraph but skip tables Windows 7 64bit Word macro to insert text at the beginning of paragraph but skip tables 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

Quote:
Originally Posted by ashalles135 View Post
I tried the macro you posted, and was patient for a while, but it seemed to just be locked up. So I ended the task and when I opened up the test file again, it had hundreds of "(U) "s in the table of contents. But nothing in the text.
You didn't mention a Table of Contents before. Moreover, what you did say was
Quote:
Originally Posted by ashalles135 View Post
Our text gets dumped out of another program and everything is styled "Normal"... including the tables.
which rules out the presence of a Table of Contents; The presence of a Table of Contents also suggests your document has Heading Styles - which you also didn't mention...

Try:
Code:
Sub Demo()
Application.ScreenUpdating = False
With ActiveDocument.Range
  With .Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Text = "[!^13]@^13"
    .Replacement.Text = ""
    .Forward = True
    .Wrap = wdFindStop
    .Format = True
    .Style = wdStyleNormal
    .MatchWildcards = True
    .Execute
  End With
  Do While .Find.Found
    If .Information(wdWithInTable) = False Then
      Select Case .Characters.First
        Case Chr(32), vbCr
        Case Else: .InsertBefore "(U) "
      End Select
    Else
      .End = .Tables(1).Range.End
    End If
    .Collapse wdCollapseEnd
    .Find.Execute
  Loop
End With
Application.ScreenUpdating = True
End Sub
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #6  
Old 09-26-2018, 09:49 AM
ashalles135 ashalles135 is offline Word macro to insert text at the beginning of paragraph but skip tables Windows 7 64bit Word macro to insert text at the beginning of paragraph but skip tables Office 2010 64bit
Novice
Word macro to insert text at the beginning of paragraph but skip tables
 
Join Date: Sep 2018
Posts: 4
ashalles135 is on a distinguished road
Default

I'm sorry. Building macros is a side thing for me and I'm just learning as I go. I didn't realize that any of that would affect it. My bad for not knowing.

The original code I posted works wonderfully with the one addition. So we've been using that...when I get a chance I will try your updated macro. I'm sure it's perfect now that you have more information.

Thank you for your time. I learn something new every time I work on macros, but unfortunately it's not my main task.
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Word macro to insert text at the beginning of paragraph but skip tables Convert particular blocks of text within a word document to tables using macro benbob Word VBA 5 07-15-2018 03:20 AM
Word macro to insert text at the beginning of paragraph but skip tables word macro To insert text at the beginning and at end of paragraph ArieH Word VBA 20 09-10-2017 04:23 PM
Word macro to insert text at the beginning of paragraph but skip tables Can't insert space at the beginning of a line in Word p89.schneider Word 6 03-24-2016 11:38 PM
Word macro to insert text at the beginning of paragraph but skip tables Macro to Insert text into the beginning on specific paragraphs unless the paragraph is blank caboy Word VBA 2 04-01-2015 07:00 AM
Looping macros to add text to beginning and end of a paragraph pachmarhi Word VBA 0 02-16-2009 06:57 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 01:39 AM.


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