Save As to new location

T

Tom

Am using Word 2002, using vb I would like to open up an existing word
document, paste same text into it and then save that file to a new location
and different file name.

Any guidance & suggestions how to do this would be appreciated

Thanks in advance

Tom
 
H

Helmut Weber

Hi Tom,
if you are really talking of VB not VBA, a VB group would be more
appropriate. Anyway, maybe this can get you started:
Private Sub Command1_Click()
Dim oWrd As Word.Application
Set oWrd = New Word.Application
With oWrd
.Documents.Open "c:\test\test.doc"
.Selection.TypeText Text:="asolkdfjoasldfjkaolfjk"
.ActiveDocument.SaveAs "c:\test\test2.doc"
.ActiveDocument.Close
End With
oWrd.Quit
Set oWrd = Nothing
End Sub
You need a reference to the word library.
A reference to the office library is advisable, too.
"Project, References, ..."
 
T

Tom

Hi Helmut

Apologies I meant VBA. The code is nearly there. If I can
clarify a bit further, from within MSAccess2002 we go to
form and create a new record and a reference number is
created e.g. T1, and a new folder, using that number, is
created in the Data folder, so we now have C:\Data\T1.

A word template document, Master1, is held in a folder
e.g. C:\Data\Masters

Data is then added to a memofield. On clicking a command
button that data, the Master1 template document is
opened, that data is pasted into that document and then
that document is saved as T1 into the C:\Data\T1 folder.

It is the ability to save to the new folder which is not
working

Thanks for your assistance

Tom
 
H

Helmut Weber

Hi Tom,
do you get an error message?
How does your code look like?
By the way, i would not open a template,
fill in data, and save it as doc,
but create a new document from the template,
create the folder, and the e.g.
form access:
objectWord.activedocument.saveas "c:\data\t1\t1.doc"
 
T

Tom

Hi Helmut

Code used is as folows:
Dim MyDb As DAO.Database
Dim wordobj As Word.Application
Dim wordDoc As Object
Dim MyForm As Access.Form
Dim sRTF As String, str As String, stDocName As String
Dim blRet As Boolean
Dim s As Variant

Set MyDb = CurrentDb()
Set MyForm = Forms!Property
Set wordobj = CreateObject("word.Application")
Set wordDoc = ordobj.Documents.Add "C:\Temp\Test.doc")

'below needed to copy RTF text from Access memo field
'to clipboard
CF_RTF = RegisterClipboardFormat(RTF)
CF_RTFNOOBJS = RegisterClipboardFormat(RTFRTFNOOBJS)
CF_RETEXTOBJ = RegisterClipboardFormat(RETEXTOBJ)
blRet = ClipBoard_SetRTFText(Me.[RTF2Description])

'make new sub folder, in temp folder, named as value in
txtReference field on Access form

str = "C:\Temp\" & Forms!Property![txtReference]
MkDir str

With wordobj

.Visible = 0
.Selection.Text = s
.Selection.Paste
.ActiveDocument.SaveAs FileName:=str & ".html",
_FileFormat:=wdFormatHTML 'HTML
.ActiveDocument.Close

End With

wordobj.Quit

Set wordDoc = Nothing
Set wordobj = Nothing
No error messages are produced and in principle the code
works fine - the line that is the problem is:
..ActiveDocument.SaveAs FileName:=str & ".html",
_FileFormat:=wdFormatHTML 'HTML

as the new file is saved in the C:\Temp folder and not in
the new folder just created.

Tom
 
H

Helmut Weber

Hi Tom,
str = "C:\Temp\" & Forms!Property![txtReference]
MkDir str
"str" is the fullname of a folder (Path & Name) !!!
.ActiveDocument.SaveAs FileName:=str & ".html"
You are trying to concatenate the folder's name
and what is supposed to be the filename's extension.
I don't think, I have to give you an example,
on how to it right. But if you like...
 
T

Tom

Hi Helmut

I take your point but changing the code to:

Dim MyDb As DAO.Database
Dim wordobj As Word.Application
Dim wordDoc As Object
Dim MyForm As Access.Form
Dim sRTF As String, str As String, stDocName As String
Dim blRet As Boolean
Dim s As Variant, str2 As Variant

Set MyDb = CurrentDb()
Set MyForm = Forms!Property
Set wordobj = CreateObject("word.Application")
Set wordDoc = wordobj.Documents.Add
("C:\Temp\Test.doc")

CF_RTF = RegisterClipboardFormat(RTF)
CF_RTFNOOBJS = RegisterClipboardFormat(RTFRTFNOOBJS)
CF_RETEXTOBJ = RegisterClipboardFormat(RETEXTOBJ)

' description to clipboard
blRet = ClipBoard_SetRTFText(Me.[RTF2Description])
str = "C:\Temp\" & Forms!Property![txtReference]
str2 = Forms!Property![txtReference]

MkDir str

With wordobj

.Visible = 0
.Selection.Text = s
.Selection.Paste
.ActiveDocument.SaveAs FileName:=str2 & ".html",
FileFormat:=wdFormatHTML 'HTML

.ActiveDocument.Close

End With

wordobj.Quit

Set wordDoc = Nothing
Set wordobj = Nothing

This is a route already tried. It saves the new file to
the default Word file folder of D:\Data not to the new
folder created in C:\temp. The first code posted at least
put the saved file into C:\Temp - but not into the new
folder just created. It would appear that within the
saveAs line there should be a reference to a path location

Some guidance please

Tom
-----Original Message-----
Hi Tom,
str = "C:\Temp\" & Forms!Property![txtReference]
MkDir str
"str" is the fullname of a folder (Path & Name) !!!
.ActiveDocument.SaveAs FileName:=str & ".html"
You are trying to concatenate the folder's name
and what is supposed to be the filename's extension.
I don't think, I have to give you an example,
on how to it right. But if you like...
---
Greetings from Bavaria, Germany
Helmut Weber, MVP
"red.sys" & chr(64) & "t-online.de"
Word 2002, Windows 2000
.
 
H

Helmut Weber

Hi Tom,
let's take your first attempt and let's say
Forms!Property![txtReference] returns "T1"
then
str = "C:\Temp\" & Forms!Property![txtReference]
MkDir str
creates the folder "C:\Temp\T1"
.ActiveDocument.SaveAs FileName:=str & ".html"
sets the filename to "C:\Temp\T1.html"!
You are therefore creating a file in the folder
"C:\Temp" with name "T1.html", the beforehand
created folder "T1" is not accessed.
Try to set the filename to:
str & "\" & Forms!Property![txtReference] & ".html"
 
T

Tom

Hi Helmut

It works!!!!!!!!!!! - thank you so much - its a case of
not seeing the wood for the trees.

Re-looking at that code I would like to refine so that
the word text is updated it will overwrite the existing
file in the same folder.

Something like:

If c:\Temp\T1 exists

file overwrites existing

elseif C:\Temp\T1 does not exist

use existing code

endIf

Could you suggest some code please

Thanks again for your help

Tom
-----Original Message-----
Hi Tom,
let's take your first attempt and let's say
Forms!Property![txtReference] returns "T1"
then
str = "C:\Temp\" & Forms!Property![txtReference]
MkDir str
creates the folder "C:\Temp\T1"
.ActiveDocument.SaveAs FileName:=str & ".html"
sets the filename to "C:\Temp\T1.html"!
You are therefore creating a file in the folder
"C:\Temp" with name "T1.html", the beforehand
created folder "T1" is not accessed.
Try to set the filename to:
str & "\" & Forms!Property![txtReference] & ".html"
---
Greetings from Bavaria, Germany
Helmut Weber, MVP
"red.sys" & chr(64) & "t-online.de"
Word 2002, Windows 2000



.
 
H

Helmut Weber

Hi Tom,
in a very simple way, only to show the principle:
---
Dim sDocName As String
Dim sHTMName As String
Dim sDirName As String
sDirName = "c:\temp\T1"
sDocName = "c:\temp\T1\T1.doc"
sHTMName = "c:\temp\T1\T1.html"
If Dir(sDirName, vbDirectory) = "" Then
MsgBox sDirName & " does not exist"
Else
MsgBox sDirName & " does exist"
End If
If Dir(sDocName, vbNormal) = "" Then
MsgBox sDocName & " does not exist"
Else
MsgBox sDocName & " does exist"
End If
If Dir(sHTMName, vbNormal) = "" Then
MsgBox sHTMName & " does not exist"
Else
MsgBox sHTMName & " does exist"
End If
 

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