PC Review


Reply
Thread Tools Rate Thread

Code to save Word-document do not work properly

 
 
jkrons
Guest
Posts: n/a
 
      6th May 2010
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
 
Reply With Quote
 
 
 
 
Jim Cone
Guest
Posts: n/a
 
      6th May 2010

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" <(E-Mail Removed)>
wrote in message news:cd96cf91-0124-4c7a-b6a7-(E-Mail Removed)...
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
 
Reply With Quote
 
Dave Ramage
Guest
Posts: n/a
 
      6th May 2010
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

"jkrons" wrote:

> 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
> .
>

 
Reply With Quote
 
Jan Kronsell
Guest
Posts: n/a
 
      6th May 2010
Thanks. It worked.

Jan

Dave Ramage wrote:
> 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
>
> "jkrons" wrote:
>
>> 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
>> .



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How do I get the document map to work properly? Mark12345 Microsoft Word Document Management 1 7th Feb 2009 01:40 PM
save Word document as bulletin board code isnms Microsoft Word Document Management 3 5th May 2008 02:11 PM
Has anyone got the code below to work? - How can I prevent users from editing the header of a document in Word 2000 or higher? robertadamharper@googlemail.com Microsoft Word Document Management 3 27th Jul 2007 07:56 AM
How can I get back work on a word document that I forgot to save =?Utf-8?B?QW5nZWw=?= Microsoft Word Document Management 2 18th Nov 2005 02:08 AM
how do I save a document as a work page in Word? =?Utf-8?B?QnJpYW4gTGVzbGll?= Microsoft Word New Users 1 5th Apr 2005 06:35 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 01:22 AM.