PDA

View Full Version : Corrupt Word Document after VBA Macros



plifshits
08-21-2024, 12:52 AM
Hello,

After using following code in my .docm file I can't open .docx file (see error below)


Dim FolderPath As String
Dim DocName As String


DocName = TName & "" & RevName & " " & sCustomer & " " & Title_txt & " _ 5CU"
FolderPath = Environ("HOMEDRIVE") & Environ("HOMEPATH") & "\Downloads" & "\" & DocName & ".docx"


With ActiveDocument
.Fields.Update
.SaveAs FileName:=FolderPath
MsgBox "Proposal Documents saved as <" & DocName & "> in your Downloads folder"
' .Close SaveChanges:=wdSaveChanges
End With


31761

jdelano
08-21-2024, 02:11 AM
Is the document empty?
Have you used the debugging tools to view the contents of the document before the save occurs?

Dave
08-21-2024, 05:33 AM
I think U need to use SaveAs2 when changing the file extension. HTH. Dave

Paul_Hossler
08-21-2024, 07:40 AM
Is the "" supposed to be "<space>" ?



DocName = TName & "" & RevName & " " & sCustomer & " " & Title_txt & " _ 5CU"

elenagilbert
08-21-2024, 07:01 PM
I suggest repairing the file, checking macro settings, converting the format, and temporarily disabling security software.

plifshits
08-22-2024, 12:52 AM
Hi all,

Document is not empty, macro is pasting some text and values from excel to word.
The thing is, when I SaveAs manually .docm to .docx (after macro run), it saves correctly and .docx opening without any trouble.
When I use code from first message to auto save and the end of macro work, it gives me corrupt error.




I think U need to use SaveAs2 when changing the file extension. HTH. Dave

I tried SaveAs2 instead of SaveAs but corruption still remains.


Is the "" supposed to be "<space>" ?

Yes, it is space.

Chas Kenyon
08-22-2024, 07:13 PM
Why is this a .docm and not a .dotm?
When a new document is created based on the template it can simply be saved as a docx file.