PDA

View Full Version : Convert from .docx to RTF ?



HTSCF Fareha
03-24-2021, 02:11 PM
I was wondering if there is a way to add a final line of VBA to a macro that will effectively convert the UserForm output to RTF?

The reason that I ask is that the documents I produce have to be copied / pasted into a bespoke programme that accepts RTF. On the whole most things paste okay, the only exception being underline. For some reason it completely ignores these.

gmayor
03-24-2021, 10:37 PM
Do you mean like

ActiveDocument.SaveAs2 FileName:="c:\path\filename.rtf", FileFormat:=wdFormatRTF

HTSCF Fareha
03-25-2021, 10:28 AM
Sorry, Graham, I didn't explain this well enough.

When pressing the 'Enter' button on the UserForm, the Word document will populate as a Word document (obviously). This document will then be copied / pasted into the third party product that accepts anything as if it were in RTF already. Thus the formatting of anything with an underline (specifically) is "stripped out" leaving just the text part when pasted across from the Word formatted document.

What I'd like to achieve is when pressing the 'Enter' button on the UserForm, the document is "formatted / coded" as a RTF document. I'm hoping then that the underlines will stay in place and will hopefully be accepted into the third party programme when copied / pasted.

I think that makes sense to me????

gmayor
03-25-2021, 09:56 PM
I am not sure that is possible, however the following may work. It saves the document, then saves it as RTF, copies the RTF file content to the clipboard, closes the RTF file and re-opens the DOCX file and deletes the temporary RTF file. Note that the code will have to be in the document template and not the document.


Sub SaveandCopyForm()Const strPath As String = "c:\path\"
Dim strFname As String
Dim oDoc As Document
strFname = "Filename"
Set oDoc = ActiveDocument
oDoc.SaveAs2 FileName:=strPath & strFname & ".docx", FileFormat:=wdFormatXMLDocument
oDoc.SaveAs2 FileName:=strPath & strFname & ".rtf", FileFormat:=wdFormatRTF
oDoc.Range.Copy
MsgBox "Document copied to clipboard"
oDoc.Close 0
Documents.Open FileName:=strPath & strFname & ".docx", AddtoRecentFiles:=False
Kill "c:\path\filename.rtf"
Set oDoc = Nothing
End Sub

HTSCF Fareha
03-26-2021, 12:10 PM
Many thanks, Graham.

I have a more pressing issue with one of my other VBA templates, but will certainly give this a try very soon.

Steve

HTSCF Fareha
03-27-2021, 07:18 AM
I have given this a try and the code does what it says on the tin, but pasting into the bespoke programme ignores the underline formatting.

I've asked some questions to see why this might be by those who administer the programme. Thankfully this is a small detail, but would be nice to have implemented.

Thanks to Graham!