Open As Copy ... vba

T

tsract

I would like to duplicate the "Open As Copy" option using vba.

The methods in the Documents collection don't appear to have this option.

Any ideas?
 
G

Graham Mayor

How about

Sub OpenACopy()
Dim fDialog As FileDialog
Dim Newfile As String
Dim sTitle As String
Set fDialog = Application.FileDialog(msoFileDialogFilePicker)
sTitle = "Open a copy"
With fDialog
.Title = sTitle
.AllowMultiSelect = False
.InitialView = msoFileDialogViewList
If .Show <> -1 Then
MsgBox "Cancelled By User", , sTitle
Exit Sub
End If
Newfile = fDialog.SelectedItems.Item(1)
End With
Documents.Add Newfile
End Sub

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


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

Jean-Guy Marcil

Graham Mayor was telling us:
Graham Mayor nous racontait que :
How about

Sub OpenACopy()
Dim fDialog As FileDialog
Dim Newfile As String
Dim sTitle As String
Set fDialog = Application.FileDialog(msoFileDialogFilePicker)
sTitle = "Open a copy"
With fDialog
.Title = sTitle
.AllowMultiSelect = False
.InitialView = msoFileDialogViewList
If .Show <> -1 Then
MsgBox "Cancelled By User", , sTitle
Exit Sub
End If
Newfile = fDialog.SelectedItems.Item(1)
End With
Documents.Add Newfile
End Sub

Good one!

I would add this line to make it easier for the user:

.Filters.Add "Word Documents", "*.doc; *.docx", 1
 
T

Tony Jollans

To duplicate the built-in process, you should add a SaveAs after opening the
file:

Sub OpenACopy()
Dim fDialog As FileDialog
Dim Newfile As String
Dim NewPath As String, NewName As String, NewFullName As String
Dim NewNum As Long, NewPos As Long
Dim sTitle As String
Set fDialog = Application.FileDialog(msoFileDialogFilePicker)
sTitle = "Open a copy"
With fDialog
.Title = sTitle
.AllowMultiSelect = False
.InitialView = msoFileDialogViewList
If .Show <> -1 Then
MsgBox "Cancelled By User", , sTitle
Exit Sub
End If
Newfile = fDialog.SelectedItems.Item(1)
End With

NewPos = InStrRev(Newfile, Application.PathSeparator)
NewPath = Left$(Newfile, NewPos)
NewName = Mid$(Newfile, NewPos + 1)
NewNum = 0
Do
NewNum = NewNum + 1
NewFullName = NewPath & "Copy(" & NewNum & ")" & NewName
Loop Until Dir$(NewFullName) = ""

With Documents.Add(Newfile)
.SaveAs NewFullName
End With

End Sub
 
T

tsract

Thank you. It's a nice touch.

Jean-Guy Marcil said:
Graham Mayor was telling us:
Graham Mayor nous racontait que :


Good one!

I would add this line to make it easier for the user:

.Filters.Add "Word Documents", "*.doc; *.docx", 1
 
G

Graham Mayor

There's another potential snag relating to the opening of documents with
automacros. I would suggest disabling those eg

WordBasic.DisableAutoMacros 1
With Documents.Add(Newfile)
.SaveAs NewFullName
End With
WordBasic.DisableAutoMacros 0

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


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
T

tsract

Interesting. I never new about this option.

I'm doing office development using COM (sometimes COM/ATL) and I often find
that I get better answers to my programming questions from the VB/VBA
community.

It seems like when it comes to Office development Microsoft wants to take
away our C/C++ compilers. Once you know a couple of basic COM tricks
regarding Office, all you need do is find all your answers in the VB/VBA
world (even C#, though it's less straightfoward).

Thanks again
 

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