Solved

WORD VBA QUESTION

Posted on 2001-06-17
11
222 Views
Last Modified: 2008-03-06
The following code returns an error 4198 - which says that the document failed to close - I'm a little light headed and feeling silly tonite - so why the error ?

With ActiveDocument
  .SaveAs FileName:=szSavedFileName, FileFormat:=wdWordDocument, AddToRecentFiles:=False
  .Close
End With

KhrisE
0
Comment
Question by:KhrisE
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
11 Comments
 
LVL 1

Expert Comment

by:underground712
ID: 6199728
did you try
   Application.Quit
0
 
LVL 13

Expert Comment

by:cri
ID: 6200151
Most probably the document is a fresh one, i.e. never ever saved.

Sub TestSaveAs()
  Dim szSavedFileName As String
 
  szSavedFileName = "testsave.doc"
  With ActiveDocument
   .SaveAs FileName:=szSavedFileName, FileFormat:=wdWordDocument, AddToRecentFiles:=False
   .Close
  End With
End Sub

All other possible errors like invalid paths, unitialized filename, read-only attributes etc did not give a 4198.
0
 
LVL 13

Expert Comment

by:cri
ID: 6201194
Under Windows NT 4 SP5 the Office version (97 SR-2) runs your code nicely, if 'szSavedFileName' does not contain the path then the default/LastUsed directory is passed automatically. Will check whether Windows 98 SE really behaves differently.
0
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
LVL 1

Author Comment

by:KhrisE
ID: 6201733
This is being run on WIN2000 Professional Edition (Desktop Version I think it is known as) using Office 97 - now I dont know if I ever installed service pack 2 - I'll check

>>Most probably the document is a fresh one, i.e. never ever saved.

Correct

I also tried your coding - same error - so I'll check the service pack

KhrisE
0
 
LVL 13

Expert Comment

by:cri
ID: 6201945
Try passing the intended filename with the path. Also check whether you have a default Document directory set in Tools|Options|FileLocations.
 
0
 
LVL 1

Author Comment

by:KhrisE
ID: 6201992
OK - I've found the error

szSavedFileName  is made up with a date and time string at the end of it formatted at follows :

szFileDateTime = Format(Now, "mm-dd-yyyy - hh:mm:ss")

when I changed ":" to :

szFileDateTime = Format(Now, "mm-dd-yyyy - hh-mm-ss")

the whole function worked fine

How can I include the time formated with ":" between the time parts - I have tried enclosing the whole string in double quotes and it does not work.

I cant understand this I had the same function working fine in VB but not in VBA ... ah



Many thanks in advance

KhrisE
0
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 50 total points
ID: 6202066
use the escape caracter (which is \ by default):

szFileDateTime = Format(Now, "mm-dd-yyyy - hh\:mm\:ss")

i suggest that you might also "escape" the rest:
szFileDateTime = Format(Now, "mm\-dd\-yyyy \- hh\:mm\:ss")

Cheers

0
 
LVL 1

Author Comment

by:KhrisE
ID: 6202116
OK - points go to each of Cri and AngelIII - Cri - collect your points as a question posted for you - I am alway greatful for the assistance and in this instance everyone mentioned helped greatly

KhrisE
0
 
LVL 13

Expert Comment

by:cri
ID: 6203998
KhrisE, I can not duplicate the error 4198 on my home PC. Therefore forget the "never ever saved". And, "my code" was only to show how I tested it, somehow it gave the 4198 error yesterday if inserted in a brand new document. Sorry to have mislead you. Therefore, I prefer you deleting the "points for", as, while I appreciate it, I do not feel having contributed to the solution.
0
 
LVL 1

Author Comment

by:KhrisE
ID: 6206024
you've contributed enough for me - perhaps you might answer something in a while when I have no points left - you honesty is exceptional - thanks

KhrisE
0
 
LVL 13

Expert Comment

by:cri
ID: 6243064
Save these points and you will have them to attract all Office experts instead relying on my memory. And, BTW, I did not even see this comment until called to 'attnchuunn' by amp.
0

Featured Post

Ready to get started with anonymous questions?

It's easy! Check out this step-by-step guide for asking an anonymous question on Experts Exchange.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Outlook Free & Paid Tools
This article describes how you can use Custom Document Properties to store settings and other information in your workbook so that they will be available the next time you open the workbook.
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question