Execute a macro test in multiple Word documents? - vba

I want to remove all highlighted in multiple word documents. How can i do based on the below code for signle document. Thanks so much.
Sub ClearHighlightFromAllWords()
Dim StoryRange As Range
For Each StoryRange In ActiveDocument.StoryRanges
StoryRange.HighlightColorIndex = wdNoHighlight
Next StoryRange
End Sub

Related

Paste from Range Object to Word document

How do I paste without pulling from the clipboard?
(The code examples are excerpts from Googling and recording.)
My VBA code populates a section of my document from a form. Before loading the next form, it copies that section's contents to an object.
The code goes on to further populate the document. When done, the user can choose the "Next Page" control which generates a new page.
I want to paste the object I saved to the beginning of that new page.
My code up to the point of the paste.
Sub waiverCopy()
Dim objRange As Range
'Capture first section of document (waiver with names etc. populated)
Set objRange = ActiveDocument.Range(Start:=0, End:=ActiveDocument.Sections(1).Range.End)
Debug.Print objRange 'DOES PRINT contents in immediate window
' continue populating rest of current page '
Selection.EndKey Unit:=wdStory 'position to end of document (i.e. current page)
Selection.InsertBreak Type:=wdPageBreak 'start new page
' Nothing of the myriad of things I've tried will paste the contents of objRange on new page
End Sub
If you don´t want to use the clipboard, all you are missing is to insert the range object on the current Selection position (Selection.FormattedText = objRange) just after creating the Page Break.
Sub Copy()
Dim objRange As Range
'Capture first section of document (waiver with names etc. populated)
Set objRange = ActiveDocument.Range(Start:=0, End:=ActiveDocument.Sections(1).Range.End - 1)
' continue populating rest of current page '
Selection.EndKey Unit:=wdStory 'position to end of document (i.e. current page)
Selection.InsertBreak Type:=wdPageBreak 'start new page
Selection.FormattedText = objRange
' Don´t forget to clean memory
Set objRange = Nothing
End Sub
Please do not forget to clean memory with Set objRange = Nothing.

How can I remove all Hyperlinks from a Word document with a macro?

How can I remove all Hyperlinks from a Word document with a VBA macro?
In Excel I can select everything using VBA and use
Selection.Hyperlinks.Delete
Is there something similar in Word?
You can remove all Hyperlinks from a word document using the following VBA macro.
Sub ClearHyperlinks()
Dim oField As Field
For Each oField In ActiveDocument.Fields
If oField.Type = wdFieldHyperlink Then
oField.Unlink
End If
Next
Set oField = Nothing
End Sub

How to use macro to highlight inserted content in a word doc?

I want to highlight all the inserted/added content(from the tracked changes) in a word file. How am I suppose to use macro to find all the inserted/added content? For example, in the below screenshot:
the words blue yellow And amusing pink and hello should all be highlighted after running the macro. And the macro should also be able to work smoothly when running a relatively big Word file with all different tracked changes. Below is the macro content to highlight the content to be yellow, but I don't know how to find the inserted content as I'm not familiar with Macro. = =
The Sub Macro1()
'
' Macro1 Macro
'
'
Options.DefaultHighlightColorIndex = wdYellow
Selection.Range.HighlightColorIndex = wdYellow
End Sub
Thank you very much in advance!
You can try using the Revisions Object (Word)
Sub HighlightInsertedRevision()
Dim myRevision As Revision
Dim currentDoc As Word.Document
Set currentDoc = Application.ActiveDocument
'Set the selection range
currentDoc.ActiveWindow.Selection.HomeKey Unit:=wdStory
'Optimise for loop
Application.ScreenUpdating = False
'Loop all revisions in current document
For Each myRevision In currentDoc.Revisions
With myRevision
'Check if revision type is inserted revision
If .Type = wdRevisionInsert Then
.Range.HighlightColorIndex = wdYellow
End If
End With
Next
Application.ScreenUpdating = True
End Sub
More Information on MSDN: Revisions Object (Word)

Replace image inside Word bookmark from Excel

I have an open Word document with a bunch of bookmarks, each with an inline image of an Excel table previously exported from Excel.
Now, I need to update the tables in the Word document as they have changed in Excel.
The way I'm doing this is matching the table names in Excel with the bookmark names in Word. If they are equal than I want to replace the existing images in Word by the current ones.
This is my code so far:
Sub substituir()
Set WordApp = GetObject(class:="Word.Application")
Set DocumentoDestino = WordApp.ActiveDocument
For Each folha In ThisWorkbook.Worksheets
If folha.Visible Then
'loop all excel tables
For Each tabela In folha.ListObjects
tabela.Name = Replace(tabela.Name, " ", "")
nomeTabela = tabela.Name
For Each myBookmark In DocumentoDestino.Bookmarks
If Right(myBookmark.Name, 4) = "PGST" Then
'This is where I need help
If myBookmark.Name = nomeTabela Then
'code to clear the table already in myBookmark here
'then copy and paste tables in myBookmark
tabela.Range.Copy
myBookmark.Range.PasteSpecial link:=False, DataType:=wdPasteMetafilePicture, _
Placement:=wdInLine, DisplayAsIcon:=False
End If
End If
Next myBookmark
Next tabela
End If
Next folha
End Sub
I've tried lots of different approaches, from deleting the bookmark and adding it back again to others, but nothing seems to work.
In the comment: 'code to clear the table already in myBookmark here I need help.
In the following code, I have tried to include the syntax you might require for your project.
Private Sub TestMark()
Dim Mark As String
Dim Rng As Range
Dim ShpRng As Range
Mark = "Text1"
With ActiveDocument
If .Bookmarks.Exists(Mark) Then
Set Rng = .Bookmarks(Mark).Range
If Rng.InlineShapes.Count Then
Set ShpRng = Rng.InlineShapes(1).Range
With ShpRng
Debug.Print .Start, .End
End With
End If
End If
End With
End Sub
Of course once you know the Start and End of the range you can manipulate it, meaning delete and replace it.
It just occurs to me that you might use the InlineShape' Caption property to find and address it.

VBA Macro not running

I created the following macro in VBA but when I call it nothing happens. Any ideas why this might not work?
Sub RemoveWords(DeleteFromCol As Range, FindCol As Range)
Dim words As New Collection
Dim r As Range
Dim word As Variant
For Each r In FindCol
words.Add (r.Value2)
Next r
For Each r In DeleteFromCol
For Each word In words
r.Value2 = Replace(r.Value2, word, "")
Next word
Next r
End Sub
Sub Remove()
RemoveWords Range("A1:A233"), Range("B1:B5")
End Sub
Macro does not work because perhaps you are not passing mandatory arguments:
(DeleteFromCol As Range, FindCol As Range)
Is that correct?
Got it work now. It was due to having cells with multiple words in them. You have to split cells up to contain 1 word each and it runs fine.