Macro to SaveAs .rtf

D

Douglas McKibbin

How can I place the contents of the clipboard in the name of the
document that I am saving as an rtf file in a macro. If I record a
macro to do this and paste the contents of the scrapbook into the
document name in the SavAs dialog, the macro does not contain the paste
command. Instead only the current scrapbook text appears ( PW52-1) in
the document name in the macro. I think I probably have to assign a
variable to the scapbook contents and place the variable in the
document name ...."Macintosh HD:VARIABLE.rtf". Unfortunately I don't
know how to do this in Visual Basic.


Sub SaveAsRTF()
'
' SaveAsRTF Macro
' Macro recorded 5/28/05 by Douglas McKibbin


ActiveDocument.SaveAs FileName:="Macintosh HD:pW52-1.rtf",
FileFormat:= _
wdFormatRTF, LockComments:=False, Password:="",
AddToRecentFiles:=True, _
WritePassword:="", ReadOnlyRecommended:=False,
EmbedTrueTypeFonts:=False, _
SaveNativePictureFormat:=False, SaveFormsData:=False,
SaveAsAOCELetter:= _
False, HTMLDisplayOnlyOutput:=False
End Sub
 
J

John McGhie [MVP - Word and Word Macintosh]

Hi Doug:

Really, you can't do this reliably. That's because the clipboard can have
multiple contents, and unless you programmatically load the clipboard within
your macro, you cannot know for sure what is going to come out of it :)

I you insist on using the clipboard, it's quite difficult. You have to Dim
a variable as a Data object, then use the GetFromClipboard method to load
the content of the clipboard into the variable as text, then use the
variable to put the text where you want it (because you cannot "paste"
internally in VBA).

Far better to to load a variable and simply use that.

The following example changes your macro to load a variable with the text
you have selected when you run the macro:

Sub SaveAsRTF()
'
' SaveAsRTF Macro
' Macro recorded 5/28/05 by Douglas McKibbin
Dim MyName as String
MyName = Selection.Range.Text & ".rtf"

ActiveDocument.SaveAs FileName:=MyName, FileFormat:= _
wdFormatRTF

End Sub

Hope this helps

How can I place the contents of the clipboard in the name of the
document that I am saving as an rtf file in a macro. If I record a
macro to do this and paste the contents of the scrapbook into the
document name in the SavAs dialog, the macro does not contain the paste
command. Instead only the current scrapbook text appears ( PW52-1) in
the document name in the macro. I think I probably have to assign a
variable to the scapbook contents and place the variable in the
document name ...."Macintosh HD:VARIABLE.rtf". Unfortunately I don't
know how to do this in Visual Basic.


Sub SaveAsRTF()
'
' SaveAsRTF Macro
' Macro recorded 5/28/05 by Douglas McKibbin


ActiveDocument.SaveAs FileName:="Macintosh HD:pW52-1.rtf",
FileFormat:= _
wdFormatRTF, LockComments:=False, Password:="",
AddToRecentFiles:=True, _
WritePassword:="", ReadOnlyRecommended:=False,
EmbedTrueTypeFonts:=False, _
SaveNativePictureFormat:=False, SaveFormsData:=False,
SaveAsAOCELetter:= _
False, HTMLDisplayOnlyOutput:=False
End Sub

--

Please reply to the newsgroup to maintain the thread. Please do not email
me unless I ask you to.

John McGhie <[email protected]>
Microsoft MVP, Word and Word for Macintosh. Consultant Technical Writer
Sydney, Australia +61 4 1209 1410
 
D

Douglas McKibbin

John,
Thanks from up and over. Your suggestion worked perfectly and Thank
You. I did elect to use GetFromClipboard ... the only minor problem I
have is that the code below does not append ".rtf" to the file name. I
appreciate your help
Douglas McKibbin

Sub SaveAsRTF()
'
' SaveAsRTF Macro
' Macro recorded 5/28/05 by Douglas McKibbin
'
Dim MyData As DataObject
Set MyData = New DataObject
Dim sClipText As String
Dim MyName As String
MyData.GetFromClipboard
sClipText = MyData.GetText(1)
MyName = sClipText & ".rtf"
ActiveDocument.SaveAs FileName:=MyName, FileFormat:= _
wdFormatRTF
End Sub
 
J

John McGhie [MVP - Word and Word Macintosh]

Hi Doug:

Now you know "why" I never use this method...

Nasty... In fact, it's amazingly ugly!!

It appears to be one of those implementations from Hell. Looks to me like
the GetText function uses a Null string delimiter they forget to tell us
about, which then prevents anything else that attempts to read the string
from reading beyond the null.

Here's how I worked it out:

Sub SaveAsRTF()
'
' SaveAsRTF Macro
' Macro recorded 5/28/05 by Douglas McKibbin
'
Dim MyData As DataObject
Set MyData = New DataObject
Dim sClipText As String
Dim MyName As String
MyData.GetFromClipboard
sClipText = Left(MyData.GetText(1), Len(MyData.GetText(1)) - 2)
x = Len(sClipText)
MyName = sClipText & ".rtf"
MsgBox MyName & Str(x)
ActiveDocument.SaveAs FileName:=MyName, FileFormat:= _
wdFormatRTF
End Sub

Play around with that and you will discover that Len(MyData.GetText(1))
returns a length two characters longer than the actual string. I assume
this is the binary value of the clipboard data type. Whatever it is, it's
unprintable: you can't see it.

Somewhere in there I am guessing there's a null the other functins can't
read past.

It's my bedtime, so you can do the rest of the work ... Still to do:

1) Is the length always two characters longer than the string? I suspect
not.

2) If the clipboard is loaded by a user COPY, is the length always two
characters greater than the text string? It seems to be.

3) If the clipboard is loaded from another application, what happens then?

4) Is the character we are trying to avoid a Null? If it is, can we find
it with the Pos() method?

5) Are we utterly sure Doug can't use the nice simple macro I sent him
yesterday that loaded the text of a selection into a variable? :)

Cheers


John,
Thanks from up and over. Your suggestion worked perfectly and Thank
You. I did elect to use GetFromClipboard ... the only minor problem I
have is that the code below does not append ".rtf" to the file name. I
appreciate your help
Douglas McKibbin

Sub SaveAsRTF()
'
' SaveAsRTF Macro
' Macro recorded 5/28/05 by Douglas McKibbin
'
Dim MyData As DataObject
Set MyData = New DataObject
Dim sClipText As String
Dim MyName As String
MyData.GetFromClipboard
sClipText = MyData.GetText(1)
MyName = sClipText & ".rtf"
ActiveDocument.SaveAs FileName:=MyName, FileFormat:= _
wdFormatRTF
End Sub

--

Please reply to the newsgroup to maintain the thread. Please do not email
me unless I ask you to.

John McGhie <[email protected]>
Microsoft MVP, Word and Word for Macintosh. Consultant Technical Writer
Sydney, Australia +61 4 1209 1410
 
D

Douglas McKibbin

Hi John,
The clipboard contents are always loaded from another application. So
far, GetText(1) returns a string 3 characters longer than the length of
the clipboard string (one of the characters in an umlaut capital A and
two more invisible ones). So if I truncate the last three characters,
voila, problem solved. Yes I probably could have gone about this by
selecting text from the document to assign to a variable, but I am also
using the clipboard contents to open the right document as ......

Sub OpenRTF()
'
' OpenRTF Macro
' Macro recorded 6/5/05 by Douglas McKibbin
Dim MyData As DataObject
Set MyData = New DataObject
Dim sClipText As String
Dim MyName As String
MyData.GetFromClipboard
sClipText = Left(MyData.GetText(1), Len(MyData.GetText(1)) - 3)
x = Len(sClipText)
MsgBox MyName & Str(x)
MyName = sClipText
Documents.Open FileName:=MyName, _
ConfirmConversions:=False, ReadOnly:=False,
AddToRecentFiles:=False, _
PasswordDocument:="", WritePasswordTemplate:="", Format:= _
wdOpenFormatAuto

End Sub

I admit this is ugly but it has been many years since I programmed
anything in a non object oriented language so if it works .....

Thanks John for all of your good help
Doug
 
J

John McGhie [MVP - Word and Word Macintosh]

Hi Doug:

Yep. VBA was *designed* to be quick, not pretty. Rule 1 is "If it works,
it's good."

However, VBA is actually very object-oriented. Not perfect: there's some
kludges in there where it was too difficult to remain pure. But it
generously rewards a study of the Word object model, and all the good coding
techniques we can use.

I'm curious as to why you used wdOpenFormatAuto instead of wdOpenFormatRTF.
If your aim is to ensure that you open the RTF version of the file, I think
that is less safe. If you specifically ask for RTF, Word will error if the
target file is not actually in RTF, which is the way I would want it.

On the other hand, wdOpenFormatAuto ensures that *whatever* the file is, you
will get it open, as raw Unicode text if nothing else will work. That might
be what you want.

Cheers



Hi John,
The clipboard contents are always loaded from another application. So
far, GetText(1) returns a string 3 characters longer than the length of
the clipboard string (one of the characters in an umlaut capital A and
two more invisible ones). So if I truncate the last three characters,
voila, problem solved. Yes I probably could have gone about this by
selecting text from the document to assign to a variable, but I am also
using the clipboard contents to open the right document as ......

Sub OpenRTF()
'
' OpenRTF Macro
' Macro recorded 6/5/05 by Douglas McKibbin
Dim MyData As DataObject
Set MyData = New DataObject
Dim sClipText As String
Dim MyName As String
MyData.GetFromClipboard
sClipText = Left(MyData.GetText(1), Len(MyData.GetText(1)) - 3)
x = Len(sClipText)
MsgBox MyName & Str(x)
MyName = sClipText
Documents.Open FileName:=MyName, _
ConfirmConversions:=False, ReadOnly:=False,
AddToRecentFiles:=False, _
PasswordDocument:="", WritePasswordTemplate:="", Format:= _
wdOpenFormatAuto

End Sub

I admit this is ugly but it has been many years since I programmed
anything in a non object oriented language so if it works .....

Thanks John for all of your good help
Doug

--

Please reply to the newsgroup to maintain the thread. Please do not email
me unless I ask you to.

John McGhie <[email protected]>
Microsoft MVP, Word and Word for Macintosh. Consultant Technical Writer
Sydney, Australia +61 4 1209 1410
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top