Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 09-30-2014, 02:49 AM
Benble Benble is offline Find a tag and replace that with a Caption in a Word doc using VBA .InsertCaption Windows 7 32bit Find a tag and replace that with a Caption in a Word doc using VBA .InsertCaption Office 2010 32bit
Novice
Find a tag and replace that with a Caption in a Word doc using VBA .InsertCaption
 
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, 11 views)

Last edited by macropod; 09-30-2014 at 08:11 PM. Reason: Added code tags & formatting
Reply With Quote
  #2  
Old 09-30-2014, 08:17 PM
macropod's Avatar
macropod macropod is offline Find a tag and replace that with a Caption in a Word doc using VBA .InsertCaption Windows 7 64bit Find a tag and replace that with a Caption in a Word doc using VBA .InsertCaption Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

There is nothing about the code, per se, that would cause it to work when running WORD 2010 from a terminal server but not from a Citrix Portal. The problem is more likely to be something to do with the way the Citrix Portal is configured.

PS: When posting code, please use the code tags. They're on the 'Go Advanced' tab at the bottom of this screen.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #3  
Old 09-30-2014, 09:43 PM
Benble Benble is offline Find a tag and replace that with a Caption in a Word doc using VBA .InsertCaption Windows 7 32bit Find a tag and replace that with a Caption in a Word doc using VBA .InsertCaption Office 2010 32bit
Novice
Find a tag and replace that with a Caption in a Word doc using VBA .InsertCaption
 
Join Date: Apr 2014
Posts: 12
Benble is on a distinguished road
Default

Hi Paul.
Thanks for your reply. Now I now at least that it is not because of the code. I will run more investigations around the configuration of the Citrix portal.
Kind Regards, Benble.
Reply With Quote
  #4  
Old 12-02-2014, 03:42 AM
Benble Benble is offline Find a tag and replace that with a Caption in a Word doc using VBA .InsertCaption Windows 7 32bit Find a tag and replace that with a Caption in a Word doc using VBA .InsertCaption Office 2010 32bit
Novice
Find a tag and replace that with a Caption in a Word doc using VBA .InsertCaption
 
Join Date: Apr 2014
Posts: 12
Benble is on a distinguished road
Default

Quote:
Originally Posted by Benble View Post
Hi Paul.
Thanks for your reply. Now I now at least that it is not because of the code. I will run more investigations around the configuration of the Citrix portal.
Kind Regards, Benble.
Hi Paul. I think I found a solution to the problem. It is a language issue. The label "Figure" was not found in the list of lables. I had a mixture of English and Swedish in the template file and for some reason the Lable text/entry "Figure" was not found when running thru the Citrix Portal. I added this code snippet before inserting the caption:
CaptionLabels.Add Name:="Figure" ' always make sure we have we this label created before we use it

Now it works from both the Terminal Server I am logged in to and also thru the Citrix portal.
Kind Regards /Benble
Reply With Quote
Reply

Tags
insertcaption



Similar Threads
Thread Thread Starter Forum Replies Last Post
Find a tag and replace that with a Caption in a Word doc using VBA .InsertCaption How do I find/replace the same word in multiple documents? Ineedhelp! Word 3 03-04-2014 03:50 PM
wildcards in find & replace to reverse word order jeffk Word 3 11-11-2012 01:47 PM
MS Word Find and Replace not working allenglishboy Word 10 07-25-2012 08:05 AM
Find a tag and replace that with a Caption in a Word doc using VBA .InsertCaption Word Find and Replace Query bthart Word 1 12-29-2011 12:45 AM
Find a tag and replace that with a Caption in a Word doc using VBA .InsertCaption Bad view when using Find and Find & Replace - Word places found string on top line paulkaye Word 4 12-06-2011 11:05 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 08:30 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft