Microsoft Office Forums Word VBA Capabilities

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 05-17-2019, 08:24 AM
Trillium1973 Trillium1973 is offline Word VBA Capabilities Windows 10 Word VBA Capabilities Office 2016
Novice
Word VBA Capabilities
 
Join Date: May 2019
Posts: 3
Trillium1973 is on a distinguished road
Smile Word VBA Capabilities

Hi there
I am working on a project and am doing research into the capabilities of Word VBA. I'm looking to see if Word VBA can do the following:



1) Find sections, and the corresponding paragraphs and delete them. Maybe using the title as the marker to locate the paragraphs and title to delete?
2) Be able to loop through a table and pick up a value that is in the 3rd column?

Do you think this is possible?
Much thanks
Reply With Quote
  #2  
Old 05-17-2019, 04:30 PM
eduzs eduzs is offline Word VBA Capabilities Windows 10 Word VBA Capabilities Office 2010 32bit
Competent Performer
 
Join Date: May 2017
Posts: 157
eduzs is on a distinguished road
Default

Yes, I think this is possible.
__________________
Backup your original file before doing any modification, test in a throwaway copy of your file.

Last edited by eduzs; 05-17-2019 at 06:46 PM.
Reply With Quote
  #3  
Old 05-18-2019, 01:26 AM
macropod's Avatar
macropod macropod is offline Word VBA Capabilities Windows 7 64bit Word VBA Capabilities Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 19,533
macropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to behold
Default

Depending on what you mean by 'sections', both are trivial undertakings for VBA.

In Word, except for the last Section in a document, a Section is something defined by a Section break. You seem to have a different concept, which you'll need to explain.
__________________
Cheers,
Paul Edstein
[MS MVP - Word]
Reply With Quote
  #4  
Old 05-18-2019, 08:46 AM
Trillium1973 Trillium1973 is offline Word VBA Capabilities Windows 10 Word VBA Capabilities Office 2016
Novice
Word VBA Capabilities
 
Join Date: May 2019
Posts: 3
Trillium1973 is on a distinguished road
Default

Hi Paul

Yes I mean Sections in the word document, as in using Section breaks. For example, I believe the text will be in Section 3, and the table I would need to reference (to return the value in the 3rd column of the second row) would always show up in Section 4. I thought it might be easier in the code to say something like "go to the table in section 4" as there is another table in Section 5.
Reply With Quote
  #5  
Old 05-18-2019, 12:38 PM
eduzs eduzs is offline Word VBA Capabilities Windows 10 Word VBA Capabilities Office 2010 32bit
Competent Performer
 
Join Date: May 2017
Posts: 157
eduzs is on a distinguished road
Default

Hi there.
Maybe you can start with something like:
(save/close any other files you're using before testing macros, backup your original file before doing any modification, test in a throwaway copy of your file)
Code:
Sub Find_paragraph_with_string()

Dim x As Integer, oDoc, y As Integer

Set oDoc = ActiveDocument

For x = oDoc.Sections.Count To 1 Step -1
    With oDoc.Sections(x).Range
        For y = 1 To .Paragraphs.Count
            If InStr(.Paragraphs(y).Range.Text, "Hello") > 0 Then MsgBox "Hello! I'm in the " & y & " paragraph of the " & x & " section!"
        Next y
    End With
Next x

End Sub

Sub Find_table_cell_with_string()

Dim x As Integer, oDoc, y As Integer, oCel

Set oDoc = ActiveDocument

For x = oDoc.Sections.Count To 1 Step -1
    With oDoc.Sections(x).Range
        If .Tables.Count > 0 Then
            For y = 1 To .Tables.Count
                For Each oCel In .Tables(y).Range.Cells
                    If Left(oCel.Range.Text, Len(oCel.Range.Text) - 2) = "Hi!" Then MsgBox "Found!"
                Next oCel
            Next y
        End If
    End With
Next x

End Sub
__________________
Backup your original file before doing any modification, test in a throwaway copy of your file.
Reply With Quote
  #6  
Old 05-18-2019, 03:40 PM
macropod's Avatar
macropod macropod is offline Word VBA Capabilities Windows 7 64bit Word VBA Capabilities Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 19,533
macropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to behold
Default

Depending on the complexity of your document vis--vis the page layouts (e.g. orientations, margins, page columns, headers & footers), the code might be as simple as:
Code:
Sub Demo()
Application.ScreenUpdating = False
Dim Tbl As Table, r As Long, bDel As Boolean
bDel = False
With ActiveDocument
  Set Tbl = .Sections(4).Range.Tables(1)
    For r = Tbl.Rows.Count To 1 Step -1
      If Split(Tbl.Cell(r, 3).Range.Text, vbCr)(0) = "Delete" Then
        If r = 4 Then bDel = True Else .Sections(r).Range.Delete
      End If
    Next
    If bDel = True Then Tbl.Range.Sections(1).Range.Delete
End With
Application.ScreenUpdating = True
End Sub
The above code looks at the first table in Section 4, using the presence of 'Delete' in column 3 of a given row to delete the corresponding Section.
__________________
Cheers,
Paul Edstein
[MS MVP - Word]
Reply With Quote
  #7  
Old 05-21-2019, 06:00 AM
Trillium1973 Trillium1973 is offline Word VBA Capabilities Windows 10 Word VBA Capabilities Office 2016
Novice
Word VBA Capabilities
 
Join Date: May 2019
Posts: 3
Trillium1973 is on a distinguished road
Default

Wow! Thank you all for your advice and to help me get started!!! I'll report back when I've given this a go!
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Word VBA Capabilities OneNote Software Capabilities hils0590 OneNote 3 12-31-2014 10:35 AM
Word VBA Capabilities Outlook 2010 to CRM capabilities Iain Outlook 1 02-20-2012 10:30 PM


All times are GMT -7. The time now is 04:56 AM.


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