Solved

Insert Cross Reference by vba and add  \* Charformat  to it

Posted on 2010-09-18
3
1,895 Views
Last Modified: 2012-05-10
Hi experts

I'm inserting cross-references into a document from a userform, eg of the insert method below.
 Selection.InsertCrossReference ReferenceType:=strLabel, _
        ReferenceKind:=wdOnlyLabelAndNumber, ReferenceItem:=n

When it puts in a Table Caption for instance it inserts the formatting of the caption which is bolded. I've tried a few ways of getting around this (like applying unbold) but on update it reverts back to bold even with unbold character style applied. Then I stumbled across something tonight which I can add into the cross reference field that has been created. And that is \* Charformat . But I can't work out how to get it to automatically come in like this. At the moment I have to right-click on the cross-reference and then click Edit Field > Field Codes > Options > Field Specific Switches > then I paste \* Charformat  down in the field code text box. It is working when I do it this way but I need to set that step by code.

Experts how can I do this through VBA code as this is a very long-winded step?
0
Comment
Question by:Fi69
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 76

Accepted Solution

by:
GrahamSkan earned 500 total points
ID: 33707687
The method does not return the field, nor does the selction contin it, so we have to look backwards for the field and edit it.


Sub InsertXRef()
Dim fld As Field
Dim rng As Range
Selection.InsertCrossReference ReferenceType:="Figure", _
        ReferenceKind:=wdOnlyLabelAndNumber, ReferenceItem:="1"
        Selection.MoveStart wdCharacter, -3
Set rng = Selection.Range
Do Until rng.Fields.Count = 1
    rng.MoveStart wdCharacter, -1
Loop
Set fld = rng.Fields(1)
fld.Code.Text = fld.Code.Text & " \* Charformat"
End Sub

Open in new window

0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 33707695
Why are typos invisible before submission, and obvious afterwards?

"selction contin it," should be "selection contain it,"
0
 

Author Comment

by:Fi69
ID: 33709500
I know exactly what you mean about the typos!

That code works brilliantly. I just had to add fld.update at the end so that it appeared correctly in my document.

Thank you so much Graham!
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This tutorial provides instructions on how to properly format your Word document using the inbuilt tools provided. The benefits of using these tools means your documents are more accessible and easily portable to other applications an…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
This video walks the viewer through the process of creating Hyperlinks for the web and other documents. Select the "Insert" tab: Click "Hyperlink":  Type "http://" followed by a web address to reference a website or navigate to a document to ref…
Office 365 is currently available in five editions. Three of them are for business use: Office 365 Business Essentials, Office 365 Business, and Office 365 Business Premium. Two of them are for home/personal use: Office 365 Home and Office 365 Perso…

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question