Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / September 2005

Tip: Looking for answers? Try searching our database.

Create a double field through VBA

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
frogman - 07 Sep 2005 21:39 GMT
I am trying to create a field code that looks like this:
{Macrobutton NoMacro {Quote "Insert Number" \* CharFormat}}

but if I just add the text the second Quote is just the text that is
added to the field.  so I attempted to create a double field creation
but it failed.

Thank you for your help

Sub TextToFieldClear()
Dim strSelection As String
Dim intWordCount As Integer

   Selection.Font.Color = wdColorRed
   strSelection = Selection.Text
   'intWordCount =
   'intWordCount = Selection.Information(wdEndOfRangeRowNumber)

   Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty,
Text:="Macrobutton NoMacro " & strSelection & "",
PreserveFormatting:=False
   'ActiveDocument.Words.Item(intWordCount).Select
   Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty,
Text:="" & strSelection & "" & " \* CharFormat",
PreserveFormatting:=False

Selection.Fields(1).Update

End Sub
Greg Maxey - 07 Sep 2005 22:55 GMT
Try this:

Sub ScratchMacoro()
Dim myRng As Range
Set myRng = Selection.Range
With myRng
 .Collapse wdCollapseEnd
 .Fields.Add Range:=myRng, Type:=wdFieldMacroButton,
PreserveFormatting:=False
 .Move Unit:=wdWord, Count:=2
 .InsertAfter "NoMacro "
 .Move Unit:=wdWord, Count:=1
 .Fields.Add Range:=myRng, Type:=wdFieldQuote, PreserveFormatting:=False
 .Move Unit:=wdWord, Count:=2
 .InsertAfter """Insert Number"" \* CharFormat"
End With
End Sub

Signature

Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.

> I am trying to create a field code that looks like this:
> {Macrobutton NoMacro {Quote "Insert Number" \* CharFormat}}
[quoted text clipped - 25 lines]
>
> End Sub
frogman - 08 Sep 2005 15:35 GMT
Thank you it helped alot here is your code that I modified to work for
me.

Sub TextToFieldClear()
Dim strSelection As String
Dim myRng As Range
Set myRng = Selection.Range
   ActiveWindow.View.ShowFieldCodes = True
   strSelection = Selection.Text

   With myRng
       .Delete
       .Collapse wdCollapseEnd
       .Fields.Add Range:=myRng, Type:=wdFieldMacroButton,
PreserveFormatting:=False
       .Move Unit:=wdWord, count:=2
       .InsertAfter "NoMacro "
       .Move Unit:=wdWord, count:=1
       .Fields.Add Range:=myRng, Type:=wdFieldQuote,
PreserveFormatting:=False
       .Move Unit:=wdWord, count:=2
       .InsertAfter ("""" & strSelection & """ \* CharFormat")
   End With

   With Selection
       .find.ClearFormatting
       .find.Forward = False
       .find.MatchCase = True
       .find.Text = "Q"
       .find.Execute
       .Font.Color = wdColorRed
       .Fields.Update
   End With
   
   ActiveWindow.View.ShowFieldCodes = False
End Sub
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.