How to make a macro ignore tables? - vba

I have the following code that deletes the paragraph following any entirely bold paragraphs (i.e. deletes blank lines following subheadings), but this has caused an issue with the part of my macro that converts text to tables, in that it deletes the content following a table header when the table has bold headers. Is there a way I can get this code to ignore tables?
Dim para As Paragraph
Dim searchRange As Range
Set searchRange = Selection.Range
searchRange.End = ActiveDocument.Content.End
For Each para In searchRange.Paragraphs
If para.Range.Font.Bold = True Then para.Next.Range.Delete
Next para

You can utilize the Information property of the Range.
Try something like:
Dim para As Paragraph
Dim searchRange As Range
Set searchRange = Selection.Range
searchRange.End = ActiveDocument.Content.End
For Each para In searchRange.Paragraphs
If para.Range.Font.Bold = True And Not para.Range.Information(wdWithInTable) Then para.Range.Next.Delete
Next para

Related

Justify All Text in Microsoft Word VBA

I am trying to create a word VBA that can justify all the text if the font size is 10, ignoring all the tables and shapes.
Some how, it doesn't work on large documents with thousands of paragraph as it will hang.
Anyway I can streamline this code to make it run more faster and efficient.
Sub JustifyAllTheText()
On Error Resume Next
Dim para As Paragraph
Dim searchRange As Range
Set searchRange = Selection.Range
searchRange.End = ActiveDocument.Content.End
For Each para In searchRange.Paragraphs
If para.Range.Font.Size = 10 And para.Range.Font.ColorIndex = wdBlack And Not para.Range.InlineShapes.count > 0 And Not para.Range.Information(wdWithInTable) Then
para.Range.ParagraphFormat.Alignment = wdAlignParagraphJustify
End If
Next para
Sub JustifyAllTheText()
On Error Resume Next
Dim para As Paragraph
Dim searchRange As Range
Set searchRange = Selection.Range
searchRange.End = ActiveDocument.Content.End
For Each para In searchRange.Paragraphs
If para.Range.Font.Size = 10 Then
If para.Range.Font.ColorIndex = wdBlack Then
If Not para.Range.InlineShapes.Count > 0 Then
If Not para.Range.Information(wdWithInTable) Then
para.Range.ParagraphFormat.Alignment = wdAlignParagraphJustify
End If
End If
End If
End If
Next para
End Sub

How to change format of current paragraph without using Selection

I have the code below without using Selection.
Sub Format paragraph()
Dim wdDoc As Document
With wdDoc.Range.Find
.Font.Size = 12
.Text = "?"
.Execute
End With
End Sub
When the character with font size = 12 is found, how can I change the format of the current paragraph? for example:
wdDoc.Paragraph(current).Font.Size = 14
wdDoc.Paragraph(current).Font.Color = wdBlue
Thanks for any help.
The trick is to work with a specific Range object, which can be used to access its "parent" paragraph. When Find.Execute is successful, the Range being searched contains the found item (same as the selection jumps to the found item). For example:
Sub Format paragraph()
Dim rng as Range, para as Paragraph
Dim wdDoc As Document
Set wdDoc = ActiveDocument. 'Missing in code in question...
Set rng = wdDoc.Content 'Content returns the Range
With rng.Find
.Font.Size = 12
.Text = "?"
If .Execute = True Then
Set para = rng.Paragraphs(1)
para.Font.Size = 14
para.Font.Color = wdBlue
End If
End With
End Sub

Highlight All Bookmarks in Word Doc Via VBA

I want to highlight all the bookmarks in my Word document. When I try to show the bookmarks, I only get the "I". And this code doesn't do anything.
Just like one of the commentators wrote, my bookmarks are 0 length. But even then how can I highlight say 2 spaces forward?
Sub BookMarks2Bold()
Dim bm As Bookmark
Dim tx As Range
Set tx = ActiveDocument.StoryRanges(wdMainTextStory)
For Each bm In tx.Bookmarks
bm.Range.HighlightColorIndex = wdYellow
Next
End Sub
If your bookmarks are zero range and you still want to highlight something in the document, you can extend the bookmark range, e.g. be the following character in the document:
Sub BookMarks2Bold()
Dim bm As Bookmark
Dim tx As Range
dim rng as Range
Set tx = ActiveDocument.StoryRanges(wdMainTextStory)
For Each bm In tx.Bookmarks
set rng = bm.Range
rng.MoveEnd wdCharacter ' extend by one character
' optionally, expand by one word
' rng.Expand wdWord
rng.HighlightColorIndex = wdYellow
Next
End Sub

How to find and copy a table below a specified text

I have a word document with multiple text labels followed by its respective tables.
I want to find a text and then copy the table immediately below it so i can paste it in other document.
Sub EWT()
Dim para As Paragraph
Dim textA As String
For Each para In ActiveDocument.Paragraphs
If Not para.Range.Information(wdWithInTable) Then
textA = para.Range.Text
Debug.Print textA
'How to get the table just below this and copy it?
End If
Next
End Sub
The following is a general way of finding tables. In short, make a new range, assign the start of where the text leaves off, and the end is the end of the document. It'd be the first table. As an additional suggestion, avoid the Information function. One reason is it's slow.
Sub EWT()
Dim para As Paragraph
Dim textA As String
For Each para In ActiveDocument.Paragraphs
If Not para.Range.Information(wdWithInTable) Then
textA = para.Range.Text
para.Range.Select 'Debug only
Dim myRange As Range
Set myRange = ActiveDocument.Range
myRange.Start = para.Range.End
myRange.End = ActiveDocument.Range.End
Dim myTable As Table
Set myTable = myRange.Tables(1)
myTable.Select 'Debug only
Debug.Print textA
'How to get the table just below this and copy it?
End If
Next End Sub
Then again, it looks like you're wanting to find every table in the document, since your Find doesn't look for any specific text, just text. If that's the case, this is quicker:
Sub FindTables()
Dim myDocument As Document
Set myDocument = ActiveDocument
Dim myTable As Table
For Each myTable In myDocument.Tables
myTable.Select
Next End Sub

How can I update MS Word fields based on contents of other fields within the document?

I am trying to find a way to lookup and replace contents within an MS Word Doc based on certain content within the same document. I have system generated Word Documents that are one page each in length, but the number of pages can vary from one to 100 (or more). Each document is formatted exactly the same. One phrase with each page of the document (such as "Type of Charge" may or may not vary from one page to the next. I need to be able to insert the actual amount of the charge on each page based on the type of charge reflected on that given page.
I was taking the approach of setting bookmark ranges that would be used to search for the phrase, and then setting a bookmark that would indicate where to insert the value. Here is what I have so far:
Sub bmAmtDue()
'
' bmAmtDue
'
'
Dim rng As Range
Dim iBookmarkSuffix As Integer
Dim strBookMarkPrefix
strBookMarkPrefix = "BM"
Set rng = ActiveDocument.Range
With rng.Find
.Text = "Please see fee chart, with additional requirements, on reverse side"
Do While .Execute
rng.Text = "" 'clear the "XXX" (optional)
iBookmarkSuffix = iBookmarkSuffix + 1
ActiveDocument.Bookmarks.Add strBookMarkPrefix & iBookmarkSuffix, rng
Loop
End With
End Sub
Sub bmStartPermitType()
'
' bmStartPermitType
'
'
Dim rng2 As Range
Dim iBookmarkSuffix As Integer
Dim strBookMarkPrefix
strBookMarkPrefix = "BMStartPermitType"
Set rng = ActiveDocument.Range
With rng.Find
.Text = "Type:"
Do While .Execute
iBookmarkSuffix = iBookmarkSuffix + 1
ActiveDocument.Bookmarks.Add strBookMarkPrefix & iBookmarkSuffix, rng
Loop
End With
End Sub
Sub bmEndPermitType()
'
' bmEndPermitType
'
'
Dim rng2 As Range
Dim iBookmarkSuffix As Integer
Dim strBookMarkPrefix
strBookMarkPrefix = "BMEndPermitType"
Set rng = ActiveDocument.Range
With rng.Find
.Text = "Amount due:"
Do While .Execute
iBookmarkSuffix = iBookmarkSuffix + 1
ActiveDocument.Bookmarks.Add strBookMarkPrefix & iBookmarkSuffix, rng
Loop
End With
End Sub
Bookmarks are OK, but might be "too flexible" - They can even start in the middle of a table cell and end in a the middle of another paragraph. I suggest you to try doing it with Content Controls - their appearance might also be more suitable for your scenario. Check this link.
If you can write a simple .NET application, there is mail merge toolkit that will make your task much more easy. It will allow you to create word document that will act as a template (it also uses Content Controls for tagging) which you will be able to populate with data from your .NET application. And it demands only couple of lines of code to write.