Use bookmark as filename in save as dialog

M

Mike

I've been reading several usegroups for hours now and couldn't find a
solution that works.
I have a document with bookmark field called "Document_title".
I want this field to be words suggestion for the filename when the
file first gets saved as.

I also have a little more code to update other bookmarks in the
document on save and save as:

The following part updates the fields / bookmarks on a save:

Sub filesave()
Dim oStory As Range
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

This part is supposed to update the fields / bookmarks of the document
and suggest the "Document_title" content as the save as filename:

Sub filesaveas()
Dim oStory As Range
Dialogs(wdDialogFileSaveAs).Show
ActiveWindow.Caption = ActiveDocument.FullName
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

Dim FileName As String
FileName = ActiveDocument.Bookmarks("Document_title")
With Application.Dialogs(wdDialogFileSaveAs)
.Name = FileName
.Show
End With

End Sub

All of this is in the "This document" Word objects.
I tried several other things, all pretty similar to this one.
I'm using word 2003.

Can someone help?
 
H

Helmut Weber

Hi Mike,

probably:

Dim FileName As String
FileName = ActiveDocument.Bookmarks("Test").Range.Text
With Application.Dialogs(wdDialogFileSaveAs)
.Name = FileName
.Show
End With


--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

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

Mike

Hi Mike,

probably:

Dim FileName As String
FileName = ActiveDocument.Bookmarks("Test").Range.Text
With Application.Dialogs(wdDialogFileSaveAs)
.Name = FileName
.Show
End With

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

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

Hi Helmut,

I changed the "arrangement" of my code and now it works. It looks as
if I need to have the correct sequence for the code.
The only problem I get is that I now get a "object variable or With
block variable not set" on the save as command.

Here's the code:

Sub filesave()
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
docNumber = ActiveDocument.Bookmarks("document_number").Range
docRev = ActiveDocument.Bookmarks("revision").Range

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
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
docNumber = ActiveDocument.Bookmarks("document_number").Range
docRev = ActiveDocument.Bookmarks("revision").Range

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

With Application.Dialogs(wdDialogFileSaveAs)

.Name = pFileName
.Show

End With

ActiveWindow.Caption = ActiveDocument.FullName
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

Nice to hear from someone German. How is the weather in Bavaria?
Here in North Carolina we have approx. 33 °C and high humidity.
 
H

Helmut Weber

Hi Mike,

I wonder about assigning a range to a string.

Maybe

docTitle = ActiveDocument.Bookmarks("Document_title").Range.text

etc. will help.

Frankly speaking, I don't know whats wrong.


--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

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

Mike

Hi Mike,

I wonder about assigning a range to a string.

Maybe

docTitle = ActiveDocument.Bookmarks("Document_title").Range.text

etc. will help.

Frankly speaking, I don't know whats wrong.

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

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

I compared the save and save as part of my code. I replaced

ActiveWindow.Caption = ActiveDocument.FullName
with
ActiveDocument.save

Now I don't get the error message any more and all other stuff in my
document works, too.

Thanks for all the help.
 

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