Hi, I am using vb6 to manipulate my word 2003 template .dot and save it into .doc.

My current method is:
I am creating I word class, called W.

then I use:
W.activedocument.SaveAs "documentname.doc", 0, 0, "". 1, "", 0, 0, 0, 0, 0

It can be used perfectly for word 2003.

But when I use it in word 2007, I got a problem.
It seems that all the manipulation in .dot file can be done.
But when I try to save that new document as "documentname.doc", I cannot do it.

I think, it is because word 2007 has .dotx and .docx as a default format.
I tried to save my .dot template as .dotx. Then my program opened it, manipulated it and saved my document as "documentname.docx"
But I still could not save it.

What should I do?
thanks a lot

Try to use

Format:=wdOpenFormatAuto

before your

W.activedocument.SaveAs "documentname.doc", 0, 0, "". 1, "", 0, 0, 0, 0, 0

Hi thanks for your help :) you are the best :)

Are you sure that Format:=wdOpenFormatAuto is suitable for vb6?
It seems that it doesn't recognize it, it keeps on saying "compile error: expected expression".

and when I used fileformat = wdopenformatauto. the error says, the variable is undefined refers to wdopenformatauto.

what should I do? at this moment, I build my vb6 program with the laptop which the 2003 microsoft office installed in it (but not 2007). is it the source of the problem? should I install 2007 in that laptop, so it can recognize "Format:=wdOpenFormatAuto"?

thanks a lot

No, the wdOpenFormatAuto automatically recognizes which version of Office is installed and will then save the file as either .doc or .docx.:)

Paste the code you have before AND after the wdOpenFormatAuto. I think the problem lies in there somewhere. It is most definitely not a problem within vb6, I have used it before with full on success.:)

This is what inside my class called word.

I opens it anywhere in my forms using W.showme or W.savedocument etc.

Option Explicit

Private W As Object

Private Sub Class_Initialize()

  On Error GoTo eInit
  If gnWordVersion = 95 Then
    'Word 95 format
      Set W = CreateObject("word.basic")
  Else
    'Word 97 format
      Set W = CreateObject("word.application")
  End If
  
  Exit Sub
    
eInit:
  
    MsgBox "Could not initialise word.  " & Error

End Sub

Public Sub ShowMe()
  On Error Resume Next
  DoEvents
  If gnWordVersion = 95 Then
    W.appshow
  Else
    'W.Application.Visible = True
    W.wordbasic.appshow
  End If
End Sub

Public Sub SaveDocument(DocName As String)
On Error GoTo WError
    
  If gnWordVersion = 95 Then
    'Word 95 format eg
      W.FileSaveAs App.path & "\" & DocName, 0, 0, "", 1, "", 0, 0, 0, 0, 0
  Else
    'Word 97 format
      W.Activedocument.SaveAs App.path & "\" & DocName, 0, 0, "", 1, "", 0, 0, 0, 0, 0
  End If
  Exit Sub
WError:
    MsgBox DocName & " is currently active, please close it and run the same function once again." 'save the newer one with the same name to overwrite it."

End Sub




Private Sub Class_Terminate()
  On Error GoTo eTerm
  'Quit
  Set W = Nothing
  Exit Sub

eTerm:

  MsgBox "Could not terminate very well.  " & Error
    
End Sub

the main problem is caused by the program reference, once I put ms word object reference. I can include it. so my current code for open and save is:

Public Sub OpenDocument(DocName)
  If gnWordVersion = 95 Then
    'Word 95 format
      W.FileOpen (App.path & "\" & DocName)  ', 0, 0, 0, "", "", 0, "", ""
  Else
    'Word 97 format
        W.Documents.Open (App.path & "\" & DocName), ConfirmConversions:=False, ReadOnly:=False, AddToRecentFiles:=False, PasswordDocument:="", PasswordTemplate:="", Revert:=False, WritePasswordDocument:="", WritePasswordTemplate:="", format:=wdOpenFormatAuto
  End If
End Sub

Public Sub SaveDocument(DocName As String)
On Error GoTo WError
    
  If gnWordVersion = 95 Then
    'Word 95 format eg
      W.FileSaveAs App.path & "\" & DocName, 0, 0, "", 1, "", 0, 0, 0, 0, 0
  Else
    'Word 97 format
      W.ActiveDocument.SaveAs App.path & "\" & DocName, FileFormat:=wdOpenFormatAuto, LockComments:=False, Password:="", AddToRecentFiles:=True, WritePassword:="", ReadOnlyRecommended:=False, EmbedTrueTypeFonts:=False, SaveNativePictureFormat:=False, SaveFormsData:=False, SaveAsAOCELetter:=False
  End If
  Exit Sub
WError:
    MsgBox DocName & " is currently active, please close it and run the same function once again." 'save the newer one with the same name to overwrite it."
    'inuseW = True
    'don't do anything
End Sub

But, even after I use that code. I still cannot save .doc in word 2007 yet.
I haven't tried to save .docx yet though.. maybe it will work.
thanks a lot

I tried to save it as docx... and use dotx template but still not working :(

just to make it clear... the docname that I use for is:

W.ActiveDocument.SaveAs App.path & "\" & "tester.docx", FileFormat:=wdOpenFormatAuto, LockComments:=False, Password:="", AddToRecentFiles:=True, WritePassword:="", ReadOnlyRecommended:=False, EmbedTrueTypeFonts:=False, SaveNativePictureFormat:=False, SaveFormsData:=False, SaveAsAOCELetter:=False

Yay.. finally.. it is done...
The main reason of its failure is.. since I put the entire program in the program files folder, that's why it has a write protection on it. When I put it in desktop, it works fine :) thanks a lot.

commented: Nicely done! +6

Sorry I could not help you to the end, had to shoot home.:) Well done on solving this yourself though, I gave you some kudos...;)

My next question, after reading your code, would have been "Where do you save the file?" This would have brought up the problem, all about user privileges to the folder and the like.:)

lol.. :) yup.. that's the source of the problem... thanks a lot :)
You did help me a lot, without your replies, I might have given up on this :p

It was only a pleasure.:)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.