Mark inserted AutoText with bookmark VBA - vba

I want to insert AutoText (with styles and colours) in the document en mark this range as a bookmark with Word VBA.
I know how to do this with normal text, but I can't figure out how to mark the inserted Autotext.
Help would be very welcome and appreciated.
Kind regards,
Kem

When you insert AutoText, it returns a Range object representing the inserted AutoText. Assuming doc is your Document and you want to insert the AutoText item at the Selection:
' add AutoText item
Dim rng As Range
Set rng = doc.AttachedTemplate.AutoTextEntries.Item("MyAutoTextItem").Insert(Selection.Range, True)
' set it to be a Bookmark
Dim bkmk As Bookmark
Set bkmk = doc.Bookmarks.Add("MyBookmark", rng)
... obviously adjust "MyAutoTextItem" and "MyBookmark" for your requirements

Related

Select Range of DocProperty

I have endless Word-Documents, all with the same DocProperty somewhere in it.
Now I have to modify the font style of this specific DocProperty (e.g. make it bold), any other DocProperty has to been skipped.
How do I select this DocProperty with VBA?
I looked into the ActiveDocument.Range.Fields collection, but where is the name of the linked DocProperty? I'm only finding the Text, but that is the value of the actual CustomDocProperty
You were on the right track. In short you want: -
To look in Field.Type for a value of 85 (WdFieldDocProperty)
Then check the Field.Code for the property name/label
A sample of checking a document for it is below with comments to explain what is happening: -
Public Sub Sample()
Dim WdDoc As Word.Document
Dim Fld As Word.Field
'Connect to the Document
Set WdDoc = ThisDocument
'Only work if there are fields in the document to begin with
If WdDoc.Fields.Count > 0 Then
'Check each field
For Each Fld In WdDoc.Fields
'If the type is a DocProperty then we may have a match
If Fld.Type = wdFieldDocProperty Then
'If the code contains the name we are after then we have a match!
If InStr(1, Fld.Code, "Custom1") Then
'Select the field
Fld.Select
'Format the selection
Selection.Font.Bold = True
End If
End If
Next
End If
Set WdDoc = Nothing
End Sub
As a further tip (if you haven't done it all ready) would be to use the FileScriptingObject to help process all of your documents in one go using a loop. If you do try that and get stuck, start a new question with how far you got and what is not working and SO will help out I'm sure.

VBA: Replace text based on formatting

I have a table in a Word file A which contains a bunch of different Contents. Which I just copy using VBA into another Word or PowerPoint file B. So far that is not a problem.
However, since file A is a working sheet, people sometimes cross stuff out, which means: it should be removed, but for the record it stays in there first. In the final version it shouldnt be displayed, so in the process of copying everything in a different file, the crossed out text should be removed.
To break it down to the technical stuff:
I want to select text in a Word document, and then remove all text that has a certain formatting.
Maybe there is a special selection possibility or a way to iterate through all characters and test for formatting.
The best way to do this without suffering severe performance iterating characters or paragraphs in vba is to use find and replace.
You can do this in vba as follows, note I have wrapped all the actions in a custom undo record, then you can call your current vba routine with CopyDocumentToPowerPoint and the word document will be restored to the state it was before the macro ran (crossed out text remains in word, but is not pasted to powerpoint).
'wrap everything you do in an undo record
Application.UndoRecord.StartCustomRecord "Move to powerpoint"
With ActiveDocument.Range.Find
.ClearFormatting
.Font.StrikeThrough = True
.Text = ""
.Replacement.Text = ""
.Execute Replace:=wdReplaceAll
End With
'copy to powerpoint and whatever else you want
CopyDocumentToPowerPoint
Application.UndoRecord.EndCustomRecord
'and put the document back to where you started
ActiveDocument.Undo
It is possible to go character-by-character and remove those which have the strikethrough font enabled on them (the ones which are crossed out) in MS Word. However, as far as I know, there is no such possibility to detect a strike-through font in MS PowerPoint.
If you just need to delete the text which has the strikethrough font on it in the selected text only, you can use this Word macro:
Sub RemoveStrikethroughFromSelection()
Dim char As Range
For Each char In Selection.Characters
If char.Font.StrikeThrough = -1 Then
char.Delete
End If
Next
End Sub
If more integrated to copying a Word table to another Word document and PowerPoint presentation, the following code might be useful. It first pastes the table to a new Word file, then removes unnecessary characters, and after that pastes this new table to PowerPoint.
Sub CopyWithoutCrossedOutText()
Dim DocApp As Object: Set DocApp = CreateObject("Word.Application")
Dim PptApp As Object: Set PptApp = CreateObject("PowerPoint.Application")
Dim Doc As Object: Set Doc = DocApp.Documents.Add
Dim Ppt As Object: Set Ppt = PptApp.Presentations.Add
Dim c As Cell
Dim char As Range
DocApp.Visible = True
PptApp.Visible = True
'Copying Word table to the 2nd Word document
ThisDocument.Tables(1).Range.Copy
Doc.ActiveWindow.Selection.Paste
'In the 2nd Word document - removing characters having strikethrough font enabled on them
For Each c In Doc.Tables(Doc.Tables.Count).Range.Cells
For Each char In c.Range.Characters
If char.Font.StrikeThrough = -1 Then
char.Delete
End If
Next
Next
'Copying the table from the 2nd Word document to the PowerPoint presentation
Doc.Tables(1).Range.Copy
Ppt.Slides.Add(1, 32).Shapes.Paste
End Sub

VBA Macro to Replace Endnotes in Word

I am hoping to use a macro to replace the endnotes in a word document. Here is my situtation:
I have two word docs. Both documents have the exact same number of endnotes. One document is full of the correct body content, but has placeholders for the end notes. The other document has outdated content, but has the correct endnotes to to fill the placeholders in the first document.
I have setup a macro below that can loop through all of the endnotes in the correct file, and then opens the other document called "old.docx" below. I just dont know how to go about replacing the endnotes in old.docx with the value of ftstr (please see below).
Any help would be great!
Sub endnoteReplacer()
Dim ft As Endnote
Dim wrdApp As Object
Dim wrdDoc As Object
Dim r1 As Range, ftstr As String, mark
Set wrdApp = CreateObject("Word.Application")
wrdApp.Visible = False
Set wrdDoc = wrdApp.Documents.Open("C:\Desktop\old.docx")
For Each ft In Word.ActiveDocument.Endnotes
ftstr = ft.Range.Text
wrdDoc.Endnotes(ft.Index).Range.Text = ftstr
Next ft
End Sub
If I get you right you need this simple solution to add within your loop:
For Each ft In Word.ActiveDocument.Endnotes
ftstr = ft.Range.Text
'change value of corresponding footnote in old.docx to value of ftstr
'!! new line !!
wrdDoc.Endnotes(ft.Index).Range.Text = ftstr
Next ft
But I assumed that you need to change endnotes(1) to endnotes(1), 2 to 2, etc...

How can i make link with selected range in word?

I am using word2003 document for processing in my document i have to made link with two string variables (Not in the sense Footnotes and Endnotes)
{Page 1} Best quote from David Brinkley[1]
{Page 6}[1] A successful person is one who can lay a firm foundation with the bricks that others throw at him
I suppose to use Footnote/Endnote for the value [1] to link but it cause some changes while editing the the Footnote/Endnote. Is there any other way to make link between the selected string?
There are probably many ways to do this, but you could bookmark the text on page 6, then add a hyperlink on page 1 that points to the bookmark. This can be done without VBA code:
Select the text for the bookmark on page 6.
Insert/Bookmark
Name the bookmark and add it
Select the text for the bookmark on page 1.
Insert/Hyperlink
Click the Bookmark button and select the bookmark in the list
The equivalent in VBA:
Option Explicit
Sub AddHyperlinkToBookmark()
Dim oBookmark As Bookmark
Dim oHyperlink As Hyperlink
'***** Add code to select text for bookmark
Set oBookmark = ThisDocument.Bookmarks.Add("BookmarkName", Selection.Range)
'***** Add code to select text for link
Set oHyperlink = ThisDocument.Hyperlinks.Add(Selection.Range, "", "BookmarkName")
Set oBookmark = Nothing
Set oHyperlink = Nothing
End Sub

How do I read the value of a checkbox in a word (*.doc) file in VB.net using a Range object?

How do I read the value of a checkbox in a word (*.doc) file in VB.net using a range object?
This is what I have so far:
Dim app As New Word.Application
Dim doc As Document
doc = app.Documents.Open("C:\myDoc.doc")
dim chkBox as Bookmark
chkBox = doc.Bookmarks("MyCheckbox")
Dim rng as Range
rng = chkBox.Range
where "MyCheckbox" is the bookmark of the checkbox in the word document.
Any particular reason you're not reading the value of the checkbox using the name of the checkbox itself?
If the range defined by your bookmark contains a checkbox, then, depending upon how the checkbox is inserted, it will be found in either the InlineShapes collection (if checkbox inserted inline with the text) or the Shapes collection (if inserted as a floating object.)
You would then need to iterate through the collection of Shapes or InlineShapes looking for the checkbox in question.
Iterating through controls in InlineShapes collection
Dim ctl As InlineShape
For Each ctl In rng.InlineShapes
If ctl.Type = wdInlineShapeOLEControlObject Then
If ctl.OLEFormat.ClassType Like "Forms.CheckBox*" Then
'current version of ctl is a checkbox, do what you will with it.
End If
End If
Next ctl
...
This ought to get you closer, but if the name of the checkbox is predictable, it is better to address it directly by name.