View Single Post
 
Old 09-30-2014, 02:49 AM
Benble Benble is offline Windows 7 32bit Office 2010 32bit
Novice
 
Join Date: Apr 2014
Posts: 12
Benble is on a distinguished road
Default Find a tag and replace that with a Caption in a Word doc using VBA .InsertCaption

I have a problem with .InsertCaption. I have been trying to look for solution on this site but I am not able to find a solution to my problem. I wonder if you could help me (again...).

The thing is that my code works sometimes and sometimes not. I have a tool that generate RTF documents and the generated documents need a lot of enhancement. I have therefore (with help from macropod earlier) created a VBA scripts that goes thru the whole document and fix TOC and TOF , fonts, labels etc. It also try to fix the caption for each figure that is in the document for TOF. The generated document contains tags that I am looking for ("CaptionFigureStart(*)CaptionFigureEnd") to replace them other tags in order later create the final result, see below. It always fails on the instruction “ .InsertCaption Label:="Figure", Title:=StrTxt, ExcludeLabel:=False” and the error message is sometimes “Command Failed” or “Runtime Error 4198 Command Failed”.

The thing is that this code work perfect when I am running the WORD 2010 from an terminal server where I am logged into. But if I am using a Citrix Portal the scripts will be run by another Word installation which is also Word 2010 with English as the default language and also the very same settings regarding the “Trust Center Settings…”. On the Citrix Portal the code below will fail. I know it might be very hard to give me an answer why but my question is if the code below is very bad/poor and should be rewritten in order to run no matter where is called from. I have tried to create this script by looking at other similar scripts for finding, replacing and fixing the caption…

Please see the attached file where you can see the progress of the script.

Code:
Sub ConvertToCaption() 
     '
     ' Convert CaptionFigureStart and -End cotag to Cpation
     '
    Dim RngTmp As Range, StrTmp As String, StrTxt As String, StrSreenTip As String 
    With ActiveDocument.Range 
         'Convert CaptionFigureStart and -End codes to {CAPTIONLINK}\1 formatWith .Find
        .Text = "CaptionFigureStart(*)CaptionFigureEnd" 
        .ClearFormatting 
        .Replacement.ClearFormatting 
        .Replacement.Text = "CAPTIONtempLINK: \1" 
        .Forward = True 
        .MatchWildcards = True 
        .Wrap = wdFindStop 
        .Execute Replace:=wdReplaceAll 
    End With 
    With .Find 
        .Text = "CAPTIONtempLINK" 
        .Replacement.Text = "" 
        .Wrap = wdFindStop 
        .Execute 
    End With 
     'Convert Caption fields
    Do While .Find.Found = True 
        Set RngTmp = .Duplicate 
        .Text = Replace(.Text, "CAPTIONtempLINK", vbNullString, 1, -1, vbTextCompare) 
        StrTxt = "" 
        .InsertCaption Label:="Figure", Title:=StrTxt, ExcludeLabel:=False 
        .Find.Execute 
    Loop 
End With 
Set RngTmp = Nothing 
End Sub
Regards / Benble
Attached Files
File Type: docx caption sample.docx (19.8 KB, 13 views)

Last edited by macropod; 09-30-2014 at 08:11 PM. Reason: Added code tags & formatting
Reply With Quote