A
Ancient Brit
I have a very simple piece of code that refuses to work.
I'm missing something obvious, I know, but for the life of me I can't see
what it is.
Background: I have three different Word documents, the partial contents of
which are to be copied (minus the final pilcrow) to another document. The
distinction for each document type is in the main story, and so I simply need
to search each document for its identifier and act accordingly on the
different parts from each. One is copied from section 3 to the penultimate
section, one is copied from section 2 to the end, and the third is copied
from section 1 to the end (all minus the last pilcrow). Couldn't (in theory)
be simpler.
The main document contains the relevant macros, one of which allows the user
to designate and open a secondary document to be copied, and then checks to
see what kind of document was opened according to the identifiers.
The code so far:
'Get currently open document, which is the target document
M_TargetDoc = ActiveDocument
'Now get source document
Dim dlgOpen As FileDialog
Set dlgOpen = Application.FileDialog(FileDialogType:=msoFileDialogOpen)
With dlgOpen
.Title = "Select Your Source Document"
.Filters.Item (3) 'Preselects .doc files
.AllowMultiSelect = False
.Show
End With
If dlgOpen.SelectedItems.Count = 0 Then
'User pressed Cancel, so abandon task
Exit Sub
Else
M_SourceDoc = dlgOpen.SelectedItems.Item(1)
End If
'Open the file chosen above:
Documents.Open FileName:=M_SourceDoc 'This will become the active doct
'Begin by setting M_DocTypeFlag to 0 as a QC check
M_DocTypeFlag = 0
'Now scan the currently-active document to determine its probable type
Set M_DocTypeRange = ActiveDocument.Content
'Is it Type 1?
M_DocTypeRange.Find.Execute FindText:="Document Information Record", _
Forward:=True
If M_DocTypeRange.Find.Found = True Then M_DocTypeFlag = 1
etc.
The code fails on .Execute - claims the command is not available. So I set
up a watch and then examined the object's properties and it's true - Execute
is not associated with it. But I can't see a workaround.
The code is taken from Microsoft's own example in Help for the FIND object:
Set myRange = ActiveDocument.Content
myRange.Find.Execute FindText:="blue", Forward:=True
If myRange.Find.Found = True Then myRange.Bold = True
I realize that it could be in error. Anyone any thoughts?
I'm missing something obvious, I know, but for the life of me I can't see
what it is.
Background: I have three different Word documents, the partial contents of
which are to be copied (minus the final pilcrow) to another document. The
distinction for each document type is in the main story, and so I simply need
to search each document for its identifier and act accordingly on the
different parts from each. One is copied from section 3 to the penultimate
section, one is copied from section 2 to the end, and the third is copied
from section 1 to the end (all minus the last pilcrow). Couldn't (in theory)
be simpler.
The main document contains the relevant macros, one of which allows the user
to designate and open a secondary document to be copied, and then checks to
see what kind of document was opened according to the identifiers.
The code so far:
'Get currently open document, which is the target document
M_TargetDoc = ActiveDocument
'Now get source document
Dim dlgOpen As FileDialog
Set dlgOpen = Application.FileDialog(FileDialogType:=msoFileDialogOpen)
With dlgOpen
.Title = "Select Your Source Document"
.Filters.Item (3) 'Preselects .doc files
.AllowMultiSelect = False
.Show
End With
If dlgOpen.SelectedItems.Count = 0 Then
'User pressed Cancel, so abandon task
Exit Sub
Else
M_SourceDoc = dlgOpen.SelectedItems.Item(1)
End If
'Open the file chosen above:
Documents.Open FileName:=M_SourceDoc 'This will become the active doct
'Begin by setting M_DocTypeFlag to 0 as a QC check
M_DocTypeFlag = 0
'Now scan the currently-active document to determine its probable type
Set M_DocTypeRange = ActiveDocument.Content
'Is it Type 1?
M_DocTypeRange.Find.Execute FindText:="Document Information Record", _
Forward:=True
If M_DocTypeRange.Find.Found = True Then M_DocTypeFlag = 1
etc.
The code fails on .Execute - claims the command is not available. So I set
up a watch and then examined the object's properties and it's true - Execute
is not associated with it. But I can't see a workaround.
The code is taken from Microsoft's own example in Help for the FIND object:
Set myRange = ActiveDocument.Content
myRange.Find.Execute FindText:="blue", Forward:=True
If myRange.Find.Found = True Then myRange.Bold = True
I realize that it could be in error. Anyone any thoughts?