Change display text of all hyperlinks in document - vba

Im pretty much void of visual basic knowledge but I know this can be done some how.
I have a 144 page document where I need to have the url be displayed.
For example: where an inserted link displaying "Google" will now display: "Google (www.google.com)"
There are some 200+ links in this document (originally was suppose to just be an electronic document) but now need to have it so that if someone had a printed copy in hand they would know the URL. I'm open to all sorts of ideas :(
One thought I had was to do Alt F9 to view the field codes and then somehow do a find replace and input some sort of code to have the display text and the url show?
Earlier I had someone who had some visual basic knowledge trying to help with who found this which I couldn't get to run for me... Is it my lack of understanding on how to make it run?
'Private Declare Function GetTickCount Lib "kernel32" () As Long
Public Sub GetHyperlinks()
Dim myDoc As Document
Dim wombat As Hyperlink
' Dim starttime As Long
Dim CurrentDoc As Document
Applicationhttp://images.intellitxt.com/ast/adTypes/icon1.png.ScreenUpdating = False
Set CurrentDoc = ActiveDocument
Set myDoc = Application.Documents.Add()
' starttime = GetTickCount
For Each wombat In CurrentDoc.Hyperlinks
myDoc.Range.InsertAfter wombat.TextToDisplay & vbTab & wombat.Address & vbCrLf
Next
' Debug.Print GetTickCount - starttime
Application.ScreenUpdating = True
myDoc.Range.ParagraphFormat.TabStops.Add CentimetersToPoints(7.5), wdAlignTabLeft, wdTabLeaderSpaces 'basic formatting
End Sub

This should work for you (tested in Word 2010):
Sub UpdateDocLinks()
Dim link As Hyperlink
For Each link In ActiveDocument.Hyperlinks
link.TextToDisplay = link.TextToDisplay & " (" & link.Address & ")"
Next link
End Sub
This will append the URL in parenthesis to every active hyperlink in the document:
Google becomes Google (http://www.google.com)

Related

Automatisation of macros

I have a Word document and I want to do following with it:
Select some part of it when I open a Word doc (let´s say from page 40 to 45).
Reverse text in selected area.
Get text reversed again as it was before opening, when I close document.
I have this code, that reverses the text:
Sub ReverseSelectedWords()
Dim i As Integer
Dim oWords As Words
Dim oWord As Range
Set oWords = Selection.Range.Words
For i = 1 To oWords.Count Step 1
Set oWord = oWords(i)
Do While oWord.Characters.Last.Text = " "
Call oWord.MoveEnd(WdUnits.wdCharacter, -1)
Loop
Debug.Print "'" & oWord.Text & "'"
oWord.Text = StrReverse(oWord.Text)
Next i
End Sub
For what you've described as being your goal, it would make far more sense to apply a password for opening to the document and provide only the intended reader(s) with that password. No code required.

Create/Update footer after SaveAs in Word VBA

I want to generate an automatic footer when I save a new MS Word file, and update the footer if I SaveAs the file.
The code below used to work well with an old Word. With the latest Word it only works if I press F12 on the keyboard. Any help would be greatly appreciated!
Sub FileSaveAs()
Dialogs(wdDialogFileSaveAs).Show
Dim i As Long
Dim ThisPath As String
Dim pName As String
Dim TextInFooter As String
Dim FullName As String
ThisPath = ActiveDocument.Path
pName = ActiveDocument.Name
FullName = ThisPath & "\" & pName
TextInFooter = "This file was saved in: " & FullName & " on the " & Now
For i = 1 To ActiveDocument.Sections.Count
With ActiveDocument.Sections(i)
.Footers(wdHeaderFooterPrimary).Range.Text = TextInFooter
End With
Next
End Sub
As you noticed, the new version triggers the FileSaveAs only on F12. Not sure if this is bug or a feature.
If it is only important that the document shows the information in print or on open - my suggested workaround:
You could avoid the insertion into the footer on save and insert it using fields, the document already has the information you are inserting. You simply need to make it visible. The footer would be then:
This file was saved as { FILENAME \p } the { SAVEDATE \# "dd.MM.yyyy HH:mm:ss"}
Adjust the Date/Time format as needed. You have to force the update of the fields - this is where the auto macros come into it.
Sub AutoOpen()
' set fields to update before printing (if saved as and printed while open)
Options.UpdateFieldsAtPrint = True
' Update all current fields in just opened document
ActiveDocument.Fields.Update
End Sub
Sub AutoClose()
' update fields when closing
ActiveDocument.Fields.Update
End Sub
The only difference would be, that you have the full path including file name and extension there. Additionally, there might be times, when the file is saved but not yet opened/closed/printed and has also not updated the fields.
In theory, you could insert the footer into the document with the AutoOpen macro as well (activedocument.fields.add).

MS Word does not unlink fields for one document but does for another

I have some VBA code (In Excel) that will open a Word document, break the links, and save that Word document elsewhere.
All automated and transport to the user, all they will see is a file appear in a folder.
My problem is it works for the below:
Private Sub Button1_Click()
Dim objWord As Object
Dim docWord As Object
Const wdDoNotSaveChanges As Long = 0
Set objWord = CreateObject("Word.Application")
Set docWord = objWord.Documents.Open(ThisWorkbook.Path & "\A folder\Myfile.doc")
objWord.ActiveDocument.Fields.Unlink
objWord.ActiveDocument.SaveAs ThisWorkbook.Path & "\" + Cells(1, 7) + "myfile.doc"
objWord.ActiveDocument.Close SaveChanges:=wdDoNotSaveChanges
objWord.Quit
End Sub
but not for the below which is supposed to perform the same function on a different Word document:
Private Sub Button2_Click()
Dim objWord As Object
Dim docWord As Object
Const wdDoNotSaveChanges As Long = 0
Set objWord = CreateObject("Word.Application")
Set docWord = objWord.Documents.Open(ThisWorkbook.Path & "\A Folder\Myfile2.doc")
objWord.ActiveDocument.Fields.Unlink
objWord.ActiveDocument.SaveAs ThisWorkbook.Path & "\" + Cells(1, 7) + "MyFile2.doc"
objWord.ActiveDocument.Close SaveChanges:=wdDoNotSaveChanges
objWord.Quit
End Sub
When I open the first document generated (outcome of the first code), it doesn't ask me to update the links and the links are broken, as intended.
When I open the second document generated (Outcome of the second code), it asks me to update the links.
EDIT:
Both sections of code above have been merged and put into a single sub that has variables forwarded to it - Still same outcome of one document updating and unlinking as intended, the other document asking the user on opening if they want to update links.
So now most likely a problem with the Word documents.
Both Word documents are linked to Excel via Paste Special > Paste Link.
Both only linked to the one Excel document.
The only difference I can see is one document has quite a few more links than the other, but surely quantity of links shouldn't matter?
Upon further investigation, the problem is confined to the header of the document. My code will unlink all fields in the body of the document, but will not do this for the header. As a result, the header remains linked and it asks the user if they would like to update.
I eventually found my solution here:
VBA Excel - Unlink headers & footers in Word

Removing internal link to Word-templates via VBA

I'm trying to create a small VB-application that removes the internal link in Word Documents, to their templates.
I have found this guide
http://word.tips.net/Pages/T001437_Batch_Template_Changes.html
and am trying to modify it, to use with VBA instead of Macro programming inside of Office.
However, I'm getting stuck on how to get the Document.Open to work. Any help is appreciated.
This is supposed to run as a free-standing application, and not runt from within Word.
I'm looking for a way to perform what the Macro does, but not from within Word.
There are two pieces of bad news to give here.
1) A document has to have a template. You cannot remove it, only change it to something else.
2) Changing a template does nothing anyway. See this page.
I am wonder if the problem with the Open method is that you are trying to open ".doc" extension files, not the modern ".docx" extension files. The VBA subroutine you linked to only does ".doc" files. This VBA code does both:
Function StringEndsWith( _
ByVal strValue As String, _
CheckFor As String) As Boolean
Dim sCompare As String
Dim lLen As Long
lLen = Len(CheckFor)
If lLen > Len(strValue) Then Exit Function
sCompare = Right(strValue, lLen)
StringEndsWith = StrComp(sCompare, CheckFor, vbTextCompare) = 0
End Function
Sub ChangeTemplates()
Dim strDocPath As String
Dim strTemplateB As String
Dim strCurDoc As String
Dim docCurDoc As Document
' set document folder path and template strings
strDocPath = "C:\tmp\"
' get first doc - only time need to provide file spec
strCurDoc = Dir(strDocPath & "*.doc*")
' ready to loop (for as long as file found)
Do While strCurDoc <> ""
If (StringEndsWith(strCurDoc, ".doc") Or StringEndsWith(strCurDoc, ".docx")) Then
' open file
Set docCurDoc = Documents.Open(FileName:=strDocPath & strCurDoc)
' change the template back to Normal
docCurDoc.AttachedTemplate = ""
' save and close
docCurDoc.Close wdSaveChanges
End If
' get next file name
strCurDoc = Dir
Loop
MsgBox "Finished"
End Sub
long time between answers but may be useful to others. If you have access to the VBE of the Word document [Alt F11], and you want to remove the reference then go to "Tools/References" [top menu] and deselect it from the list of reference files. I had a similar issue where template no longer existed, but it was still being 'referenced' in the Project window, so I did the above.

Software Requirements Reviews with MS Word - How do I get metrics in an automated way?

Let's say I have a requirements document in MS Word, and someone else reviews it an provides a list of issues found using the "Track Changes" feature.
Is there a way to extract "how many major/minor issues were found during the review?" using an automated script - for metrics purposes?
I see CodeCollaborator has some MS Word integration, but it doesn't seem to know how to look inside Word to extract the tracked changes data. It just launches the document.
I did once write a Word macro that extracts the comments to a separate document, you are welcome to try adapting this to your purposes, if you have trouble then reply here and I can give you a hand with making changes.
Public Sub PROCESS_COMMENTS()
Dim strReplaceText As String
Dim myPar As Paragraph
Dim strCurrentColumn As String
Dim i As Integer
Dim Com As Comment
Application.ScreenUpdating = False
' set the input and output docs.
Set inDoc = ActiveDocument
' check we have comments to process in the original document
If inDoc.Comments.Count < 1 Then
MsgBox "No comments in the document"
Exit Sub
End If
' comments exist so create new document
Set outDoc = Documents.Add
Set outRange = outDoc.Content
outDoc.Range.InsertAfter "List of Comments:"
outDoc.Paragraphs(outDoc.Paragraphs.Count).Style = outDoc.Styles("Heading 1")
outDoc.Range.InsertParagraphAfter
' cycle through comments, inserting them in the new document
' display the new document and refresh
outDoc.Activate
Application.ScreenRefresh
For Each Com In inDoc.Comments
outRange.InsertAfter "[" & Com.Author & " - " & Com.Initial & Com.Index & "] "
outDoc.Paragraphs(outDoc.Paragraphs.Count).Range.Font.Bold = True
outDoc.Range.InsertParagraphAfter
outRange.InsertAfter Com.Range.Text
outDoc.Paragraphs(outDoc.Paragraphs.Count).Range.Font.Bold = False
Set myRange = Com.Scope
outDoc.Range.InsertParagraphAfter
outDoc.Range.InsertParagraphAfter
Next
Application.ScreenUpdating = True
Set outDoc = ActiveDocument
End Sub