save as cancel event

M

Mike

I use the following code to use bookmarks to put the save as filename
in the save as dialog box together. It works perfectly fine until I
cancel the save dialog. So I think I'm missing something to handle
this event. Does someone know where and what to include in the code to
not get error messages in case the cancel button is pressed?

Sub filesaveas()

Dim oStory As Range
Dim pFileName As String
Dim docTitle As String
Dim docNumber As String
Dim docRev As String

docTitle = ActiveDocument.Bookmarks("Document_title").Range.Text
docNumber = ActiveDocument.Bookmarks("document_number").Range.Text
docRev = ActiveDocument.Bookmarks("revision").Range.Text

pFileName = docNumber & "-" & docRev & "-" & docTitle

With Application.Dialogs(wdDialogFileSaveAs)

.Name = pFileName
.Show

End With

ActiveDocument.save
For Each oStory In ActiveDocument.StoryRanges
oStory.Fields.Update
If oStory.StoryType <> wdMainTextStory Then
While Not (oStory.NextStoryRange Is Nothing)
Set oStory = oStory.NextStoryRange
oStory.Fields.Update
Wend
End If
Next oStory
Set oStory = Nothing


End Sub

By the way, thanks for all the great help I already got from this
group.
 
H

Helmut Weber

Hi Mike,

With Application.Dialogs(wdDialogFileSaveAs)
.Name = "1yxyx"
If .Show = -1 Then
' continue with your code
Else
Stop ' or exit or whatever
End If
End With

If you don't check the return-value of .show
the macro continues executing,
or at least, tries it.

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 
M

Mike

Hi Mike,

With Application.Dialogs(wdDialogFileSaveAs)
.Name = "1yxyx"
If .Show = -1 Then
' continue with your code
Else
Stop ' or exit or whatever
End If
End With

If you don't check the return-value of .show
the macro continues executing,
or at least, tries it.

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"

Hmmm. Right now I only get error messages like end with without with
and such.
I don't really know where exactly to place your code in mine. Could
you explain a little more?

Thanks.
 
H

Helmut Weber

Hi Mike,
end with without with

should not be that difficult to trap,
if you have formatted your code properly
regarding indents.

Usually, I'd say, there is an "end if"
corresponding to an "if" missing.

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 
M

Mike

Hi Mike,


should not be that difficult to trap,
if you have formatted your code properly
regarding indents.

Usually, I'd say, there is an "end if"
corresponding to an "if" missing.

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"

Great, I've got it. Just to make it complete, here the code for
others:

Sub filesaveas()

Dim oStory As Range
Dim pFileName As String
Dim docTitle As String
Dim docNumber As String
Dim docRev As String

docTitle = ActiveDocument.Bookmarks("Document_title").Range.Text
docNumber = ActiveDocument.Bookmarks("document_number").Range.Text
docRev = ActiveDocument.Bookmarks("revision").Range.Text

pFileName = docNumber & "-" & docRev & "-" & docTitle

With Application.Dialogs(wdDialogFileSaveAs)

.Name = pFileName
If .Show = -1 Then

ActiveDocument.save
For Each oStory In ActiveDocument.StoryRanges
oStory.Fields.Update
If oStory.StoryType <> wdMainTextStory Then
While Not (oStory.NextStoryRange Is Nothing)
Set oStory = oStory.NextStoryRange
oStory.Fields.Update
Wend
End If
Next oStory
Set oStory = Nothing

End If

End With

End Sub

Now it's prompting me once, and if I hit cancel, it just cancels
without errors.
The only thing I didn't understand is, that I don't need the Else.
But at least it works.

Thanks for the help. I really appreciate it.
 
R

Russ

Mike,
Great, I've got it. Just to make it complete, here the code for
others:

Sub filesaveas()

Dim oStory As Range
Dim pFileName As String
Dim docTitle As String
Dim docNumber As String
Dim docRev As String

docTitle = ActiveDocument.Bookmarks("Document_title").Range.Text
docNumber = ActiveDocument.Bookmarks("document_number").Range.Text
docRev = ActiveDocument.Bookmarks("revision").Range.Text

pFileName = docNumber & "-" & docRev & "-" & docTitle

With Application.Dialogs(wdDialogFileSaveAs)

.Name = pFileName
If .Show = -1 Then

ActiveDocument.save
For Each oStory In ActiveDocument.StoryRanges
oStory.Fields.Update
If oStory.StoryType <> wdMainTextStory Then
While Not (oStory.NextStoryRange Is Nothing)
Set oStory = oStory.NextStoryRange
oStory.Fields.Update
Wend
End If
Next oStory
Set oStory = Nothing

End If

End With

End Sub

Now it's prompting me once, and if I hit cancel, it just cancels
without errors.
The only thing I didn't understand is, that I don't need the Else.

The Else was to do something, if the user cancelled. But since, the
'if..end if' was the last thing in your subroutine, if the user cancelled
then then stuff in between 'if..end if' was not executed and the next
commands were End With and End Sub which didn't cause any problems in this
case.
 

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