![]() |
|
|
|
#1
|
|||
|
|||
|
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 |
|
#2
|
|||
|
|||
|
Yes, I think this is possible.
Last edited by eduzs; 05-17-2019 at 06:46 PM. |
|
#3
|
||||
|
||||
|
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 [Fmr MS MVP - Word] |
|
#4
|
|||
|
|||
|
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. |
|
#5
|
|||
|
|||
|
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
|
|
#6
|
||||
|
||||
|
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
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
|
#7
|
|||
|
|||
|
Wow! Thank you all for your advice and to help me get started!!! I'll report back when I've given this a go!
|
|
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
OneNote Software Capabilities
|
hils0590 | OneNote | 3 | 12-31-2014 10:35 AM |
Outlook 2010 to CRM capabilities
|
Iain | Outlook | 1 | 02-20-2012 10:30 PM |