Code to save Word-document do not work properly

J

jkrons

I have this code:

Sub Flet()

Dim Wdapp As Object
Dim Navn As String


On Error Resume Next
Set Wdapp = GetObject(, "Word.application")
If Err.Number <> 0 Then
Set Wdapp = CreateObject("Word.Application")
End If

For Each c In Range("A2:A200")
Wdapp.Documents.Add "flet.dot"
If c.Value = "" Then Exit For Else
Navn = c.Value

.... A lot of code for replacing bookmarks

Navn = Navn & " " & Date & " " & Time
Wdapp.ActiveDocument.SaveAs Filename:="C:\test\" & Navn &
".docx"
Wdapp.ActiveDocument.Close
Next c

Wdapp.Visible = False
MsgBox "Merge has completede and the documents are saved in C:
\Test", vbOKOnly + vbInformation
Wdapp.Quit
Set Wdapp = Nothing


End Sub

The code is supposed to make a document for each used line in the
specified range, and save the document in C:\Test, and when all the
documents are saved, it should tell me so.

If i comment out :

Navn = Navn & " " & Date & " " & Time

it works as supposed, but as soon as I try to add a date-time stamp to
the name it starts to display the SaveAs Dialog for each document,
suggesting I save them in MyDocuments. And when I click Cancel to
these dialogs, it asks me if I want to close the document without
saving.

Any ideas what goes wrong?

Jan
 
J

Jim Cone

You may be exceeding the 31 character limit for a sheet name.
Date and Time plus the blanks uses 22 characters on my system.
--
Jim Cone
Portland, Oregon USA



"jkrons" <[email protected]>
wrote in message I have this code:

Sub Flet()
Dim Wdapp As Object
Dim Navn As String
On Error Resume Next
Set Wdapp = GetObject(, "Word.application")
If Err.Number <> 0 Then
Set Wdapp = CreateObject("Word.Application")
End If
For Each c In Range("A2:A200")
Wdapp.Documents.Add "flet.dot"
If c.Value = "" Then Exit For Else Navn = c.Value

.... A lot of code for replacing bookmarks

Navn = Navn & " " & Date & " " & Time
Wdapp.ActiveDocument.SaveAs Filename:="C:\test\" & Navn & ".docx"
Wdapp.ActiveDocument.Close
Next c
Wdapp.Visible = False
MsgBox "Merge has completede and the documents are saved in C:
\Test", vbOKOnly + vbInformation
Wdapp.Quit
Set Wdapp = Nothing
End Sub

The code is supposed to make a document for each used line in the
specified range, and save the document in C:\Test, and when all the
documents are saved, it should tell me so.
If i comment out :

Navn = Navn & " " & Date & " " & Time

it works as supposed, but as soon as I try to add a date-time stamp to
the name it starts to display the SaveAs Dialog for each document,
suggesting I save them in MyDocuments. And when I click Cancel to
these dialogs, it asks me if I want to close the document without
saving.
Any ideas what goes wrong?
Jan
 
D

Dave Ramage

Depending on your local settings, Date & " " & Time will add some illegal
characters into the file name. Try something like this:
Navn = Navn & " " & Format(Date + Time,"dd_mm_yy hh_mm")

Cheers,
Dave
 
J

Jan Kronsell

Thanks. It worked.

Jan

Dave said:
Depending on your local settings, Date & " " & Time will add some
illegal characters into the file name. Try something like this:
Navn = Navn & " " & Format(Date + Time,"dd_mm_yy hh_mm")

Cheers,
Dave
 

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