Showing posts with label MS-Word. Show all posts
Showing posts with label MS-Word. Show all posts

Sunday, September 20, 2015

Add Comments in MS-Word using VBA

This VBA procedure (1) Searches words ("Str1", "Str2",..) and adds commentboxes to it and (2) changes the initials used in the box:

Sub CommentBox()

Dim range As range
Dim i As Long
Dim TargetList
Dim cm As Comment

TargetList = Array("Str1", "Str2")

For i = 0 To UBound(TargetList)

Set range = ActiveDocument.range

With range.Find
.Text = TargetList(i)
.Format = True
.MatchCase = False
.MatchWholeWord = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False

Do While .Execute(Forward:=True) = True

Set cm = range.Comments.Add(range:=range, Text:="Hallo")
cm.Author = "InternalName"
cm.Initial = "Initials"

Loop

End With

Next

With ActiveDocument.Styles("Kommentartext").Font
.Size = 12
.Bold = True
.Italic = True
.Color = wdColorBlue
End With

End Sub


Read more »

MS Word 2010, Find & Replace with Wildcards

Quite often I need to re-format citations in my texts, i.e., missing brackets around the year of a publication. In MS Word I simlpy solve such issues with the find and replace option using wildcards. I.e. for the issue with years without brackets:

short cut crtl-h
then put ([1-2][0-9]{3}) in the find-box and
(^&) in the replace box
hit 'replace' and you're done

The find string will search for any 4-digit number starting with 1 or 2 followed by 3 digits between 0 and 9 and will be replaced by the same number put inbetween round brackets. Note that the brackets in the find-string denote a token which then can be referenced by the string ^& in the replace step.
Read more »

Fast Correction of Typos in MS Word with VBA

..I use a macro to quickly fix misspelled words. More precisely, the misspelled word is replaced by the first suggestion from the spelling checker. The macro is called by hitting "strg+q" just after a typo occurred.

Download-Instructions:
To use this macro you need to download this word template and save it to your default Microsoft Office Startup folder (or wherever you store your customized templates, the path can be found in word in tools / options / save tab). If stored at the right place the keyboard shortcut macro should work whenever you open a Word file on this computer.

Code:

Sub FirstSugg()

'Author: Kay Cichini
'Date: 2012-01-17
'Info: The template was created with ms word 2003

Dim r As Range
Selection.MoveLeft Unit:=wdWord
Set r = Selection.GoToNext(wdGoToSpellingError)

With r.GetSpellingSuggestions()
  If .Count > 0 Then
    r.Text = .Item(1).Name
  Else: MsgBox ("Lacking Suggestion!")
  End If
End With

Selection.MoveRight Unit:=wdWord
End Sub
Read more »

Reproducible Research: Export Regression Table to MS Word

Here's a quick tip for anyone wishing to export results, say a regression table, from R to MS Word:


require(R2wd)

# install packages required
# install software RCOM, RDCOMClient
# (I had to restart the R-Session after the above step to get it work)

wdGet()

# Regression:
data(iris)
mod <- lm(Sepal.Length ~ Species, data = iris)
regr_tab <- data.frame(summary(mod)$coefficients)
colnames(regr_tab) <- colnames(summary(mod)$coefficients)
regr_tab[ ,4] <- ifelse(regr_tab[ ,4] < .001, "< 0.001",
ifelse(regr_tab[ ,4] < .01, "< 0.01",
round(regr_tab[ ,4], 3)))

# print table to doc in word-default format:
wdTable(format(regr_tab), autoformat = 1)

wdSave("Regression.doc") # save file
wdQuit() # close file

ps: Also check odfWeave
- this also seems to be a very useful resource!
Read more »

Match Words in MS-Word File with Words in another File and Apply New Format Using VBA

I present a macro that I wrote for re-formatting scientific species names (it is common to use italic fonts for that) in a Word file. Therefore I used a database of central European species names - this is compared with the words in my file and matches are re-formatted...

(download template)

Note: If you download the template you will need to use your own database and change the path in the script... Else you may contact me for the text-file with species names...

For your own word list: use a tab-delimited txt-file with unique strings without capitalization, like:

yourtextfile.txt:
-------------------------------
one
two
three
...
-------------------------------

Code:

'macro name: ReformatListMatches
'purpose: compares words from document with words from file
'author: kay cichini
'date: 2012-01-04
'licence: cc by-nc-sa

'specifications:
'before running the macro, add a commandbar called "mycombar" and assign the macro "ReformatListMatches" to it,
'run line 8 one time, then disable it, then save file to a template (.dot) and store it at your templates' folder.
'if you don't want a command bar, just skip the above part and don't run line 8!

Sub ReformatListMatches()

'CommandBars("mycombar").Controls(1).TooltipText = "calls procedure that re-formats words that match word list"
'this sets tooltip info, run this only once (!!), otherwise you will be asked to save changes to the dot file
'everytime you close a word doc.

time_start = Timer()

If MsgBox("Re-format matches?" & vbLf & " " & vbLf & "..may take some time" & vbLf & "..be patient! (the active window will be temporarily invisible to speed up process)", vbOKCancel + vbQuestion, "SpKursiv") = vbOK Then

Dim vntArrWords As Variant
Dim lngI As Long
Dim strText As String
Dim strPathFile As String
Dim lngFN As Long

strPathFile = "C:\LogoXP\SP_words_tab.txt"
'the database with names to compare

lngFN = FreeFile
Open strPathFile For Binary As lngFN
strText = Space(LOF(lngFN))
Get lngFN, 1, strText
Close lngFN

System.Cursor = wdCursorWait

vntArrWords = Split(strText, vbCrLf, -1, 1)

ActiveWindow.Visible = False

With ActiveDocument.Content.Find
.ClearFormatting
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Replacement.ClearFormatting
.Replacement.Text = "^&" 'replaces match with the original string (but with new format!)
.Replacement.Font.Italic = True 'here i determine the new format
For lngI = 0 To UBound(vntArrWords)
.Text = Trim(vntArrWords(lngI))
.Execute Replace:=wdReplaceAll
Next
End With

ActiveWindow.Visible = True

time_end = Timer()

MsgBox "finished!" & vbLf & "(calculation time (mm:ss) = " & time_end - time_start & ")"

Else: Exit Sub
End If

End Sub

BTW: just found a similar tutorial at http://www.garyradley.com/vbatutor/tut4.htm
Read more »