adding content to word files.

J

jen11

I am trying to add text content to a batch of word files. Existing text in
the files is not being replaced. I use" microsoft word 2007". Do I use xml
shema or other progamming code, and would I have to install word 2003 to
create the xml shema files to add to the libaries.? Any information would be
appreciated
 
G

Graham Mayor

What text are you trying to add and to where in the document? The basic
batch code in vba is:

Sub BatchProcess()
Dim strFileName As String
Dim strPath As String
Dim oDoc As Document
Dim fDialog As FileDialog
Set fDialog = Application.FileDialog(msoFileDialogFolderPicker)

With fDialog
.Title = "Select folder and click OK"
.AllowMultiSelect = False
.InitialView = msoFileDialogViewList
If .Show <> -1 Then
MsgBox "Cancelled By User", , _
"List Folder Contents"
Exit Sub
End If
strPath = fDialog.SelectedItems.Item(1)
If Right(strPath, 1) <> "\" _
Then strPath = strPath + "\"
End With

If Documents.Count > 0 Then
Documents.Close SaveChanges:=wdPromptToSaveChanges
End If
If Left(strPath, 1) = Chr(34) Then
strPath = Mid(strPath, 2, Len(strPath) - 2)
End If
strFileName = Dir$(strPath & "*.doc")

While Len(strFileName) <> 0
Set oDoc = Documents.Open(strPath & strFileName)
'********************************************
'Do what you want with oDoc (the opened document) here
'********************************************
oDoc.Close SaveChanges:=wdSaveChanges
strFileName = Dir$()
Wend
End Sub

http://www.gmayor.com/installing_macro.htm
--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP

My web site www.gmayor.com

<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
J

jen11

Hi,

Thanks for the VBA code above, I have no programming knowledge yet, but
will try use it later . I am trying to add text paragraphs and sub headings
to different parts the main page in the document. I have installed the VBA
find and replace dialog, but receive runtime error "52" when trying to
create "a predefined word list", runtime error "5156" after selecting "a
quick list to create to create now", and errors when trying to close it. I
got the auto text to run on a directory of some word files but after it has
run they remained unchanged. I am trying to get it to work, and would much
appreciate any support with this.
 
G

Graham Mayor

I don't see how your comments relate to my code which is a generic code for
sequentially opening all the documents in a folder. Instead you appear to be
referring to some other macro? Perhaps if you could tell us *exactly* what
it is that you want the macro to do...? What is the relevance of the word
list? How would the macro know where to insert the paragraphs and sub
headings and what to insert?

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP

My web site www.gmayor.com

<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
J

jen11

--
jen11


Graham Mayor said:
I don't see how your comments relate to my code which is a generic code for
sequentially opening all the documents in a folder. Instead you appear to be
referring to some other macro? Perhaps if you could tell us *exactly* what
it is that you want the macro to do...? What is the relevance of the word
list? How would the macro know where to insert the paragraphs and sub
headings and what to insert?

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP

My web site www.gmayor.com

<>>< ><<> ><<> <>>< ><<> <>>< <>><<>




Hi,

The VBA find and replace program I'm referring to is by Greg Maxey, I will
contact separately later for that issue. I have a directory of files on art
projects made out over the years. I would like to be able to manage them
better
by opening the directory and selecting all or certain files to add content
to, and also to be able to edit the added content later. Example of added
content would be a aims & ojectives paragraph and list at the top of the page
and adding on a new project elsewhere in the docuement.

Regards,

Jen11
 
G

Graham Mayor

jen11 said:
The VBA find and replace program I'm referring to is by Greg Maxey, I
will contact separately later for that issue. I have a directory of
files on art projects made out over the years. I would like to be
able to manage them better
by opening the directory and selecting all or certain files to add
content to, and also to be able to edit the added content later.
Example of added content would be a aims & ojectives paragraph and
list at the top of the page and adding on a new project elsewhere in
the docuement.

Regards,

Jen11

There was no reference to Greg's code in the thread, but there was the macro
I posted. I'm not sure how I was supposed to know you were talking about
something entirely different. However, the macro I posted could be used to
add text to the start of each document in a folder opened by the macro. Type
the paragraph(s) you wish to add in another document and save it.

Then you will need a couple of extra lines to open that document and append
its content at the start of each document opened by the macro:

Sub BatchProcess()
Dim strFileName As String
Dim strPath As String
Dim oDoc As Document
Dim sourceDoc As Document
Dim fDialog As FileDialog
Set fDialog = Application.FileDialog(msoFileDialogFolderPicker)
With fDialog
.Title = "Select folder and click OK"
.AllowMultiSelect = False
.InitialView = msoFileDialogViewList
If .Show <> -1 Then
MsgBox "Cancelled By User", , _
"List Folder Contents"
Exit Sub
End If
strPath = fDialog.SelectedItems.Item(1)
If Right(strPath, 1) <> "\" _
Then strPath = strPath + "\"
End With

If Documents.Count > 0 Then
Documents.Close SaveChanges:=wdPromptToSaveChanges
End If
'Open the document with the typed paragraphs
Set sourceDoc = Documents.Open("D:\My Documents\Word
Documents\DocWithNewPara.docx")
If Left(strPath, 1) = Chr(34) Then
strPath = Mid(strPath, 2, Len(strPath) - 2)
End If
strFileName = Dir$(strPath & "*.doc")

While Len(strFileName) <> 0
Set oDoc = Documents.Open(strPath & strFileName)
'
'Insert the content of the source document at the start of the opened
document
oDoc.Range.InsertBefore sourceDoc.Range
'
oDoc.Close SaveChanges:=wdSaveChanges
strFileName = Dir$()
Wend
End Sub
 
J

jen11

--
jen11


Graham Mayor said:
There was no reference to Greg's code in the thread, but there was the macro
I posted. I'm not sure how I was supposed to know you were talking about
something entirely different. However, the macro I posted could be used to
add text to the start of each document in a folder opened by the macro. Type
the paragraph(s) you wish to add in another document and save it.

Then you will need a couple of extra lines to open that document and append
its content at the start of each document opened by the macro:

Sub BatchProcess()
Dim strFileName As String
Dim strPath As String
Dim oDoc As Document
Dim sourceDoc As Document
Dim fDialog As FileDialog
Set fDialog = Application.FileDialog(msoFileDialogFolderPicker)
With fDialog
.Title = "Select folder and click OK"
.AllowMultiSelect = False
.InitialView = msoFileDialogViewList
If .Show <> -1 Then
MsgBox "Cancelled By User", , _
"List Folder Contents"
Exit Sub
End If
strPath = fDialog.SelectedItems.Item(1)
If Right(strPath, 1) <> "\" _
Then strPath = strPath + "\"
End With

If Documents.Count > 0 Then
Documents.Close SaveChanges:=wdPromptToSaveChanges
End If
'Open the document with the typed paragraphs
Set sourceDoc = Documents.Open("D:\My Documents\Word
Documents\DocWithNewPara.docx")
If Left(strPath, 1) = Chr(34) Then
strPath = Mid(strPath, 2, Len(strPath) - 2)
End If
strFileName = Dir$(strPath & "*.doc")

While Len(strFileName) <> 0
Set oDoc = Documents.Open(strPath & strFileName)
'
'Insert the content of the source document at the start of the opened
document
oDoc.Range.InsertBefore sourceDoc.Range
'
oDoc.Close SaveChanges:=wdSaveChanges
strFileName = Dir$()
Wend
End Sub


Hi,

Firstly, sorry for confusion, I initially thought the vba find & repace
program was one main piece of software for the vba find & replace tasks, and
understand now there are many. Many thanks for the macro above. I will use
this to add content to files.

Jen11
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top