copy contents of clipboard into field code

A

andreas

Dear experts:

All the paragraphs of my document have the following make-up:

Quotation Marks { Noteref fn_ \h } text text text, number (1 to 3
digits), full stop, Quotation marks. end of paragraph (paragraph
mark).

This translates into:
"{ Noteref fn_ \h } this is a sample text 124."
"{ Noteref fn_ \h } this is a sample text 56."
and so forth

As I already mentioned, all paragraphs of the document have got the
above make-up.

I now would like the macro to achieve the following:


.......copying the number at the end of the first paragraph (124) and
insert this number into the field code of the first paragraph right
after the underscore character (_), i.e. the first field code should
look like this
{NOTEREF fn_124 \h} after the macro has done the first loop.

....... looping thru all the paragraphs copying the respective number
at the end of the paragraph and inserting the number into the
respective field code.

Your professional help is much appreciated.

Regards, Andreas
 
D

Doug Robbins - Word MVP

Forget the clipboard.

Use:

Dim i As Long
Dim num As String
With ActiveDocument
For i = 1 To .Paragraphs.Count
With .Paragraphs(i).Range
num = .Words(.Words.Count - 2)
.Text = Left(.Text, 15) & num & Mid(.Text, 16)
End With
Next i
End With


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
 
L

Lene Fredborg

I think the following macro will do what you want – provided if have read
your description correctly. It does not matter whether or not field codes are
shown when running the macro. See the comments in the code.


Sub InsertLastNumberFromParaInFieldCode()
Dim oRange As Range
Dim strFieldCode As String
Dim oPara As Paragraph

Application.ScreenUpdating = False

For Each oPara In ActiveDocument.Paragraphs
With oPara
'Set range to para text
Set oRange = .Range
With oRange
'Reduce to 1 digit + period + para mark + quote at end
.Start = .End - 4
'Exclude period, quote and para mark
.End = .End - 3
'Move start to the left as long as previous char is numeric
Do Until IsNumeric(.Characters(1).Previous) = False
.Start = .Start - 1
Loop
End With

'Create the field code to use in the Noteref field
strFieldCode = Replace(.Range.Fields(1).Code.Text, "fn_", "fn_"
& oRange.Text)
'Insert changed field code
.Range.Fields(1).Code.Text = strFieldCode
'Update field
.Range.Fields(1).Update
End With

Next oPara

'Clean up
Set oRange = Nothing
Application.ScreenUpdating = True

End Sub

--
Regards
Lene Fredborg - Microsoft MVP (Word)
DocTools - Denmark
www.thedoctools.com
Document automation - add-ins, macros and templates for Microsoft Word
 
L

Lene Fredborg

Doug,

Definitely shorter than my version...

I started using .Words (plus .Last and .Previous) – and I did not succeed
because I forgot the wdWord parameter (can I see now). When testing your
version, I found that it destroys the NoteRef fields – even if field codes
are shown when running the macro. Do you see something else?

--
Regards
Lene Fredborg - Microsoft MVP (Word)
DocTools - Denmark
www.thedoctools.com
Document automation - add-ins, macros and templates for Microsoft Word
 
D

Doug Robbins - Word MVP

Hi Lene,

I guess I should have realised that it was a field code. The following
should do it however:

Dim i As Long
Dim num As String
With ActiveDocument
For i = 1 To .Paragraphs.Count
With .Paragraphs(i).Range
num = .Words(.Words.Count - 2)
.Fields(1).Code.Text = "Noteref fn_" & num & " \h"
End With
Next i
End With


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
 
L

Lene Fredborg

Hi Doug,

I agree.
My version also works but is more clumsy (maybe I should have gone to bed in
time <g>).

--
Regards
Lene Fredborg - Microsoft MVP (Word)
DocTools - Denmark
www.thedoctools.com
Document automation - add-ins, macros and templates for Microsoft Word
 
A

andreas

Hi Lene,

I guess I should have realised that it was a field code.  The following
should do it however:

Dim i As Long
Dim num As String
With ActiveDocument
    For i = 1 To .Paragraphs.Count
        With .Paragraphs(i).Range
            num = .Words(.Words.Count - 2)
            .Fields(1).Code.Text = "Noteref  fn_" & num &" \h"
        End With
    Next i
End With

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.










- Show quoted text -

Hi Doug,

both versions (Lene's and yours) work just fine. Thank you very much
for your terrific help. Regards, Andreas
 
A

andreas

I think the following macro will do what you want – provided if have read
your description correctly. It does not matter whether or not field codesare
shown when running the macro. See the comments in the code.

Sub InsertLastNumberFromParaInFieldCode()
    Dim oRange As Range
    Dim strFieldCode As String
    Dim oPara As Paragraph

    Application.ScreenUpdating = False

    For Each oPara In ActiveDocument.Paragraphs
        With oPara
            'Set range to para text
            Set oRange = .Range
            With oRange
                'Reduce to 1 digit + period + para mark +quote at end
                .Start = .End - 4
                'Exclude period, quote and para mark
                .End = .End - 3
                'Move start to the left as long as previous char is numeric
                Do Until IsNumeric(.Characters(1).Previous) = False
                    .Start = .Start - 1
                Loop
            End With

            'Create the field code to use in the Noteref field
            strFieldCode = Replace(.Range.Fields(1).Code.Text, "fn_", "fn_"
& oRange.Text)
            'Insert changed field code
            .Range.Fields(1).Code.Text = strFieldCode
            'Update field
            .Range.Fields(1).Update
        End With

    Next oPara

    'Clean up
    Set oRange = Nothing
    Application.ScreenUpdating = True

End Sub

--
Regards
Lene Fredborg - Microsoft MVP (Word)
DocTools - Denmarkwww.thedoctools.com
Document automation - add-ins, macros and templates for Microsoft Word














- Show quoted text -


Dear Lene,

as always, a very good coding job. Terrific help. Regards, Andreas
 

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