2015-04-16 2 views
3

У меня есть несколько макросов для применения стилей в текстовом документе, затем найдите и замените указанные стили на теги HTML. Я получаю сообщение об ошибке при запуске макроса:Как пропустить и выполнить макросы VBA без использования неприменимых стилей?

"Run time error:5941 The requested member of collection does not exist."

Например: В коде ниже, я применил все стили, кроме Book_Title стиля.

Как пропустить недостающие стили при запуске макросов?

Sub HTML_Conversion() 
    Selection.Find.ClearFormatting 
    Selection.Find.Style = ActiveDocument.Styles("Image") 
    Selection.Find.Replacement.ClearFormatting 
    With Selection.Find 
     .Text = "" 
     .Replacement.Text = "<p align=""center""><img src=""images\chapter_img.jpg"" alt=""""/></p>^p" 
     .Forward = True 
     .Wrap = wdFindContinue 
     .Format = True 
     .MatchCase = False 
     .MatchWholeWord = False 
     .MatchWildcards = False 
     .MatchSoundsLike = False 
     .MatchAllWordForms = False 
    End With 
    Selection.Find.Execute Replace:=wdReplaceAll 

    Selection.Find.ClearFormatting 
    Selection.Find.Style = ActiveDocument.Styles("Book_Title") 
    Selection.Find.Replacement.ClearFormatting 
    With Selection.Find 
     .Text = "" 
     .Replacement.Text = _ 
      "<h1 class=""book-title"">^&</h1>^p" 
     .Forward = True 
     .Wrap = wdFindContinue 
     .Format = True 
     .MatchCase = False 
     .MatchWholeWord = False 
     .MatchWildcards = False 
     .MatchSoundsLike = False 
     .MatchAllWordForms = False 
    End With 
    Selection.Find.Execute Replace:=wdReplaceAll 



    Selection.Find.ClearFormatting 
    Selection.Find.Style = ActiveDocument.Styles("Half_Title") 
    Selection.Find.Replacement.ClearFormatting 
    With Selection.Find 
     .Text = "*^13" 
     .Replacement.Text = "<p class=""halftitle"">^&</p>^p" 
     .Forward = True 
     .Wrap = wdFindContinue 
     .Format = True 
     .MatchCase = False 
     .MatchWholeWord = False 
     .MatchAllWordForms = False 
     .MatchSoundsLike = False 
     .MatchWildcards = True 
    End With 
    Selection.Find.Execute Replace:=wdReplaceAll 



    Selection.Find.ClearFormatting 
    Selection.Find.Style = ActiveDocument.Styles("Indent_Para") 
    Selection.Find.Replacement.ClearFormatting 
    With Selection.Find 
     .Text = "*^13" 
     .Replacement.Text = "<p class=""indent"">^&</p>^p" 
     .Forward = True 
     .Wrap = wdFindContinue 
     .Format = True 
     .MatchCase = False 
     .MatchWholeWord = False 
     .MatchAllWordForms = False 
     .MatchSoundsLike = False 
     .MatchWildcards = True 
    End With 
    Selection.Find.Execute Replace:=wdReplaceAll 



    Selection.Find.ClearFormatting 
    Selection.Find.Style = ActiveDocument.Styles("NonIndent_Para") 
    Selection.Find.Replacement.ClearFormatting 
    With Selection.Find 
     .Text = "*^13" 
     .Replacement.Text = "<p class=""noindent"">^&</p>^p" 
     .Forward = True 
     .Wrap = wdFindContinue 
     .Format = True 
     .MatchCase = False 
     .MatchWholeWord = False 
     .MatchAllWordForms = False 
     .MatchSoundsLike = False 
     .MatchWildcards = True 
    End With 
    Selection.Find.Execute Replace:=wdReplaceAll 
End Sub 

Как вы сказали, я пробовал в своих макросах, но я получаю ту же ошибку.

Мои полные коды ниже:

Sub HTML_Conversion() 

    Dim sStyleName As String 

    sStyleName = "HTML_Start" 
    If IsValidDocumentStyle(ActiveDocument, sStyleName) Then 

    Selection.Find.ClearFormatting 
    Selection.Find.Style = ActiveDocument.Styles("sStyleName") 
    Selection.Find.Replacement.ClearFormatting 
    With Selection.Find 
     .Text = "" 
     .Replacement.Text = _ 
      "<?xml version=""1.0"" encoding=""UTF-8"" standalone=""no""?>^p<!DOCTYPE html>^p<html xml:lang=""en-US"" xmlns=""http://www.w3.org/1999/xhtml"">^p<head>^p<title></title>^p<link rel=""stylesheet"" type=""text/css"" href=""../css/epub.css""/>^p</head>^p<body>^p" 
     .Forward = True 
     .Wrap = wdFindContinue 
     .Format = True 
     .MatchCase = False 
     .MatchWholeWord = False 
     .MatchWildcards = False 
     .MatchSoundsLike = False 
     .MatchAllWordForms = False 
    End With 
    Selection.Find.Execute Replace:=wdReplaceAll 
    End If 

    sStyleName = "Ack_title" 
    If IsValidDocumentStyle(ActiveDocument, sStyleName) Then 

    Selection.Find.ClearFormatting 
    Selection.Find.Style = ActiveDocument.Styles("sStyleName") 
    Selection.Find.Replacement.ClearFormatting 
    With Selection.Find 
     .Text = "" 
     .Replacement.Text = "<p class=""act-title"">^&</p>" 
     .Forward = True 
     .Wrap = wdFindContinue 
     .Format = True 
     .MatchCase = False 
     .MatchWholeWord = False 
     .MatchWildcards = False 
     .MatchSoundsLike = False 
     .MatchAllWordForms = False 
    End With 
    Selection.Find.Execute Replace:=wdReplaceAll 
    End If 
    sStyleName = "FigCaption" 
    If IsValidDocumentStyle(ActiveDocument, sStyleName) Then 

    Selection.Find.ClearFormatting 
    Selection.Find.Style = ActiveDocument.Styles("sStyleName") 
    Selection.Find.Replacement.ClearFormatting 
    With Selection.Find 
     .Text = "" 
     .Replacement.Text = "^p<figure>^p<img src=""images\chapter_img.jpg"" alt=""""/>^p<figcaption>^&</figcaption></figure>^p" 
     .Forward = True 
     .Wrap = wdFindContinue 
     .Format = True 
     .MatchCase = False 
     .MatchWholeWord = False 
     .MatchWildcards = False 
     .MatchSoundsLike = False 
     .MatchAllWordForms = False 
    End With 
    Selection.Find.Execute Replace:=wdReplaceAll 

    End If 
    sStyleName = "TabCaption" 
    If IsValidDocumentStyle(ActiveDocument, sStyleName) Then 

    Selection.Find.ClearFormatting 
    Selection.Find.Style = ActiveDocument.Styles("sStyleName") 
    Selection.Find.Replacement.ClearFormatting 
    With Selection.Find 
     .Text = "" 
     .Replacement.Text = "<p class=""tabcaption"">^&</p>" 
     .Forward = True 
     .Wrap = wdFindContinue 
     .Format = True 
     .MatchCase = False 
     .MatchWholeWord = False 
     .MatchWildcards = False 
     .MatchSoundsLike = False 
     .MatchAllWordForms = False 
    End With 
    Selection.Find.Execute Replace:=wdReplaceAll 

    End If 
    sStyleName = "ListItem" 
    If IsValidDocumentStyle(ActiveDocument, sStyleName) Then 

    Selection.Find.ClearFormatting 
    Selection.Find.Style = ActiveDocument.Styles("sStyleName") 
    Selection.Find.Replacement.ClearFormatting 

    With Selection.Find 
     .Text = "*^13" 
     .Replacement.Text = "<li>^&</li>^p" 
     .Forward = True 
     .Wrap = wdFindContinue 
     .Format = True 
     .MatchCase = False 
     .MatchWholeWord = False 
     .MatchWildcards = True 
     .MatchSoundsLike = False 
     .MatchAllWordForms = False 
    End With 
    Selection.Find.Execute Replace:=wdReplaceAll 

    End If 
    sStyleName = "OL_Start" 
    If IsValidDocumentStyle(ActiveDocument, sStyleName) Then 

    Selection.Find.ClearFormatting 
    Selection.Find.Style = ActiveDocument.Styles("sStyleName") 
    Selection.Find.Replacement.ClearFormatting 

    With Selection.Find 
     .Text = "" 
     .Replacement.Text = "<ol>^p" 
     .Forward = True 
     .Wrap = wdFindContinue 
     .Format = True 
     .MatchCase = False 
     .MatchWholeWord = False 
     .MatchWildcards = False 
     .MatchSoundsLike = False 
     .MatchAllWordForms = False 
    End With 
    Selection.Find.Execute Replace:=wdReplaceAll 

    End If 
    sStyleName = "OL_End" 
    If IsValidDocumentStyle(ActiveDocument, sStyleName) Then 

    Selection.Find.ClearFormatting 
    Selection.Find.Style = ActiveDocument.Styles("sStyleName") 
    Selection.Find.Replacement.ClearFormatting 

    With Selection.Find 
     .Text = "" 
     .Replacement.Text = "</ol>^p" 
     .Forward = True 
     .Wrap = wdFindContinue 
     .Format = True 
     .MatchCase = False 
     .MatchWholeWord = False 
     .MatchWildcards = False 
     .MatchSoundsLike = False 
     .MatchAllWordForms = False 
    End With 
    Selection.Find.Execute Replace:=wdReplaceAll 

    End If 
    sStyleName = "UL_Start" 
    If IsValidDocumentStyle(ActiveDocument, sStyleName) Then 

    Selection.Find.ClearFormatting 
    Selection.Find.Style = ActiveDocument.Styles("sStyleName") 
    Selection.Find.Replacement.ClearFormatting 

    With Selection.Find 
     .Text = "" 
     .Replacement.Text = "<ul>^p" 
     .Forward = True 
     .Wrap = wdFindContinue 
     .Format = True 
     .MatchCase = False 
     .MatchWholeWord = False 
     .MatchWildcards = False 
     .MatchSoundsLike = False 
     .MatchAllWordForms = False 
    End With 
    Selection.Find.Execute Replace:=wdReplaceAll 


    End If 
    sStyleName = "UL_End" 
    If IsValidDocumentStyle(ActiveDocument, sStyleName) Then 

    Selection.Find.ClearFormatting 
    Selection.Find.Style = ActiveDocument.Styles("sStyleName") 
    Selection.Find.Replacement.ClearFormatting 

    With Selection.Find 
     .Text = "" 
     .Replacement.Text = "</ul>^p" 
     .Forward = True 
     .Wrap = wdFindContinue 
     .Format = True 
     .MatchCase = False 
     .MatchWholeWord = False 
     .MatchWildcards = False 
     .MatchSoundsLike = False 
     .MatchAllWordForms = False 
    End With 
    Selection.Find.Execute Replace:=wdReplaceAll 


    End If 
    sStyleName = "Image" 
    If IsValidDocumentStyle(ActiveDocument, sStyleName) Then 

    Selection.Find.ClearFormatting 
    Selection.Find.Style = ActiveDocument.Styles("sStyleName") 
    Selection.Find.Replacement.ClearFormatting 

    With Selection.Find 
     .Text = "" 
     .Replacement.Text = "<p align=""center""><img src=""images\chapter_img.jpg"" alt=""""/></p>^p" 
     .Forward = True 
     .Wrap = wdFindContinue 
     .Format = True 
     .MatchCase = False 
     .MatchWholeWord = False 
     .MatchWildcards = False 
     .MatchSoundsLike = False 
     .MatchAllWordForms = False 
    End With 
    Selection.Find.Execute Replace:=wdReplaceAll 

    End If 
    sStyleName = "Book_Title" 
    If IsValidDocumentStyle(ActiveDocument, sStyleName) Then 

    Selection.Find.ClearFormatting 
    Selection.Find.Style = ActiveDocument.Styles("sStyleName") 
    Selection.Find.Replacement.ClearFormatting 

    With Selection.Find 
     .Text = "" 
     .Replacement.Text = _ 
      "<h1 class=""book-title"">^&</h1>^p" 
     .Forward = True 
     .Wrap = wdFindContinue 
     .Format = True 
     .MatchCase = False 
     .MatchWholeWord = False 
     .MatchWildcards = False 
     .MatchSoundsLike = False 
     .MatchAllWordForms = False 
    End With 
    Selection.Find.Execute Replace:=wdReplaceAll 


    End If 
    sStyleName = "Half_Title" 
    If IsValidDocumentStyle(ActiveDocument, sStyleName) Then 

    Selection.Find.ClearFormatting 
    Selection.Find.Style = ActiveDocument.Styles("sStyleName") 
    Selection.Find.Replacement.ClearFormatting 

    With Selection.Find 
     .Text = "*^13" 
     .Replacement.Text = "<p class=""halftitle"">^&</p>^p" 
     .Forward = True 
     .Wrap = wdFindContinue 
     .Format = True 
     .MatchCase = False 
     .MatchWholeWord = False 
     .MatchAllWordForms = False 
     .MatchSoundsLike = False 
     .MatchWildcards = True 
    End With 
    Selection.Find.Execute Replace:=wdReplaceAll 


     End If 
    sStyleName = "Indent_Para" 
    If IsValidDocumentStyle(ActiveDocument, sStyleName) Then 

    Selection.Find.ClearFormatting 
    Selection.Find.Style = ActiveDocument.Styles("sStyleName") 
    Selection.Find.Replacement.ClearFormatting 

    With Selection.Find 
     .Text = "*^13" 
     .Replacement.Text = "<p class=""indent"">^&</p>^p" 
     .Forward = True 
     .Wrap = wdFindContinue 
     .Format = True 
     .MatchCase = False 
     .MatchWholeWord = False 
     .MatchAllWordForms = False 
     .MatchSoundsLike = False 
     .MatchWildcards = True 
    End With 
    Selection.Find.Execute Replace:=wdReplaceAll 


    End If 
    sStyleName = "NonIndent_Para" 
    If IsValidDocumentStyle(ActiveDocument, sStyleName) Then 

    Selection.Find.ClearFormatting 
    Selection.Find.Style = ActiveDocument.Styles("sStyleName") 
    Selection.Find.Replacement.ClearFormatting 

    With Selection.Find 
     .Text = "*^13" 
     .Replacement.Text = "<p class=""noindent"">^&</p>^p" 
     .Forward = True 
     .Wrap = wdFindContinue 
     .Format = True 
     .MatchCase = False 
     .MatchWholeWord = False 
     .MatchAllWordForms = False 
     .MatchSoundsLike = False 
     .MatchWildcards = True 
    End With 
    Selection.Find.Execute Replace:=wdReplaceAll 



    End If 
    sStyleName = "Book_Author" 
    If IsValidDocumentStyle(ActiveDocument, sStyleName) Then 

    Selection.Find.ClearFormatting 
    Selection.Find.Style = ActiveDocument.Styles("sStyleName") 
    Selection.Find.Replacement.ClearFormatting 

    With Selection.Find 
     .Text = "*^13" 
     .Replacement.Text = "<p class=""bookauthor"">^&</p>^p" 
     .Forward = True 
     .Wrap = wdFindContinue 
     .Format = True 
     .MatchCase = False 
     .MatchWholeWord = False 
     .MatchAllWordForms = False 
     .MatchSoundsLike = False 
     .MatchWildcards = True 
    End With 
    Selection.Find.Execute Replace:=wdReplaceAll 


    End If 
    sStyleName = "Pub" 
    If IsValidDocumentStyle(ActiveDocument, sStyleName) Then 

    Selection.Find.ClearFormatting 
    Selection.Find.Style = ActiveDocument.Styles("sStyleName") 
    Selection.Find.Replacement.ClearFormatting 

    With Selection.Find 
     .Text = "*^13" 
     .Replacement.Text = "<p class=""pub"">^&</p>^p" 
     .Forward = True 
     .Wrap = wdFindContinue 
     .Format = True 
     .MatchCase = False 
     .MatchWholeWord = False 
     .MatchAllWordForms = False 
     .MatchSoundsLike = False 
     .MatchWildcards = True 
    End With 
    Selection.Find.Execute Replace:=wdReplaceAll 


    End If 
    sStyleName = "Pub1" 
    If IsValidDocumentStyle(ActiveDocument, sStyleName) Then 

    Selection.Find.ClearFormatting 
    Selection.Find.Style = ActiveDocument.Styles("sStyleName") 
    Selection.Find.Replacement.ClearFormatting 

    With Selection.Find 
     .Text = "*^13" 
     .Replacement.Text = "<p class=""pub1"">^&</p>^p" 
     .Forward = True 
     .Wrap = wdFindContinue 
     .Format = True 
     .MatchCase = False 
     .MatchWholeWord = False 
     .MatchAllWordForms = False 
     .MatchSoundsLike = False 
     .MatchWildcards = True 
    End With 
    Selection.Find.Execute Replace:=wdReplaceAll 


    End If 
    sStyleName = "Copyright" 
    If IsValidDocumentStyle(ActiveDocument, sStyleName) Then 

    Selection.Find.ClearFormatting 
    Selection.Find.Style = ActiveDocument.Styles("sStyleName") 
    Selection.Find.Replacement.ClearFormatting 

    With Selection.Find 
     .Text = "*^13" 
     .Replacement.Text = "<p class=""copyright"">^&</p>^p" 
     .Forward = True 
     .Wrap = wdFindContinue 
     .Format = True 
     .MatchCase = False 
     .MatchWholeWord = False 
     .MatchAllWordForms = False 
     .MatchSoundsLike = False 
     .MatchWildcards = True 
    End With 
    Selection.Find.Execute Replace:=wdReplaceAll 


    End If 
    sStyleName = "Section" 
    If IsValidDocumentStyle(ActiveDocument, sStyleName) Then 

    Selection.Find.ClearFormatting 
    Selection.Find.Style = ActiveDocument.Styles("sStyleName") 
    Selection.Find.Replacement.ClearFormatting 

    With Selection.Find 
     .Text = "*^13" 
     .Replacement.Text = "<p class=""section"">^&</p>^p" 
     .Forward = True 
     .Wrap = wdFindContinue 
     .Format = True 
     .MatchCase = False 
     .MatchWholeWord = False 
     .MatchAllWordForms = False 
     .MatchSoundsLike = False 
     .MatchWildcards = True 
    End With 
    Selection.Find.Execute Replace:=wdReplaceAll 


    End If 
    sStyleName = "Center_Para" 
    If IsValidDocumentStyle(ActiveDocument, sStyleName) Then 

    Selection.Find.ClearFormatting 
    Selection.Find.Style = ActiveDocument.Styles("sStyleName") 
    Selection.Find.Replacement.ClearFormatting 
    With Selection.Find 
     .Text = "*^13" 
     .Replacement.Text = "<p class=""center"">^&</p>^p" 
     .Forward = True 
     .Wrap = wdFindContinue 
     .Format = True 
     .MatchCase = False 
     .MatchWholeWord = False 
     .MatchAllWordForms = False 
     .MatchSoundsLike = False 
     .MatchWildcards = True 
    End With 
    Selection.Find.Execute Replace:=wdReplaceAll 


    End If 
    sStyleName = "Block_Quote" 
    If IsValidDocumentStyle(ActiveDocument, sStyleName) Then 

    Selection.Find.ClearFormatting 
    Selection.Find.Style = ActiveDocument.Styles("sStyleName") 
    Selection.Find.Replacement.ClearFormatting 
     With Selection.Find 
     .Text = "*^13" 
     .Replacement.Text = "<p class=""blockquote"">^&</p>^p" 
     .Forward = True 
     .Wrap = wdFindContinue 
     .Format = True 
     .MatchCase = False 
     .MatchWholeWord = False 
     .MatchAllWordForms = False 
     .MatchSoundsLike = False 
     .MatchWildcards = True 
    End With 
    Selection.Find.Execute Replace:=wdReplaceAll 


    End If 
    sStyleName = "Poem" 
    If IsValidDocumentStyle(ActiveDocument, sStyleName) Then 

    Selection.Find.ClearFormatting 
    Selection.Find.Style = ActiveDocument.Styles("sStyleName") 
    Selection.Find.Replacement.ClearFormatting 
    With Selection.Find 
     .Text = "*^13" 
     .Replacement.Text = "<p class=""poem"">^&</p>^p" 
     .Forward = True 
     .Wrap = wdFindContinue 
     .Format = True 
     .MatchCase = False 
     .MatchWholeWord = False 
     .MatchAllWordForms = False 
     .MatchSoundsLike = False 
     .MatchWildcards = True 
    End With 
    Selection.Find.Execute Replace:=wdReplaceAll 


    End If 
    sStyleName = "Poem1" 
    If IsValidDocumentStyle(ActiveDocument, sStyleName) Then 

    Selection.Find.ClearFormatting 
    Selection.Find.Style = ActiveDocument.Styles("sStyleName") 
    Selection.Find.Replacement.ClearFormatting 
    With Selection.Find 
     .Text = "*^13" 
     .Replacement.Text = "<p class=""poem1"">^&</p>^p" 
     .Forward = True 
     .Wrap = wdFindContinue 
     .Format = True 
     .MatchCase = False 
     .MatchWholeWord = False 
     .MatchAllWordForms = False 
     .MatchSoundsLike = False 
     .MatchWildcards = True 
    End With 
    Selection.Find.Execute Replace:=wdReplaceAll 


    End If 
    sStyleName = "Bibliography" 
    If IsValidDocumentStyle(ActiveDocument, sStyleName) Then 

    Selection.Find.ClearFormatting 
    Selection.Find.Style = ActiveDocument.Styles("sStyleName") 
    Selection.Find.Replacement.ClearFormatting 
    With Selection.Find 
     .Text = "*^13" 
     .Replacement.Text = "<p class=""bib"">^&</p>^p" 
     .Forward = True 
     .Wrap = wdFindContinue 
     .Format = True 
     .MatchCase = False 
     .MatchWholeWord = False 
     .MatchAllWordForms = False 
     .MatchSoundsLike = False 
     .MatchWildcards = True 
    End With 
    Selection.Find.Execute Replace:=wdReplaceAll 


    End If 
    sStyleName = "Index" 
    If IsValidDocumentStyle(ActiveDocument, sStyleName) Then 

    Selection.Find.ClearFormatting 
    Selection.Find.Style = ActiveDocument.Styles("sStyleName") 
    Selection.Find.Replacement.ClearFormatting 
    With Selection.Find 
     .Text = "*^13" 
     .Replacement.Text = "<p class=""index"">^&</p>^p" 
     .Forward = True 
     .Wrap = wdFindContinue 
     .Format = True 
     .MatchCase = False 
     .MatchWholeWord = False 
     .MatchAllWordForms = False 
     .MatchSoundsLike = False 
     .MatchWildcards = True 
    End With 
    Selection.Find.Execute Replace:=wdReplaceAll 


    End If 
    sStyleName = "Notes_Titles" 
    If IsValidDocumentStyle(ActiveDocument, sStyleName) Then 

    Selection.Find.ClearFormatting 
    Selection.Find.Style = ActiveDocument.Styles("sStyleName") 
    Selection.Find.Replacement.ClearFormatting 
    With Selection.Find 
     .Text = "*^13" 
     .Replacement.Text = "<p class=""nt"">^&</p>^p" 
     .Forward = True 
     .Wrap = wdFindContinue 
     .Format = True 
     .MatchCase = False 
     .MatchWholeWord = False 
     .MatchAllWordForms = False 
     .MatchSoundsLike = False 
     .MatchWildcards = True 
    End With 
    Selection.Find.Execute Replace:=wdReplaceAll 


    End If 
    sStyleName = "Notes" 
    If IsValidDocumentStyle(ActiveDocument, sStyleName) Then 

    Selection.Find.ClearFormatting 
    Selection.Find.Style = ActiveDocument.Styles("sStyleName") 
    Selection.Find.Replacement.ClearFormatting 
    With Selection.Find 
     .Text = "*^13" 
     .Replacement.Text = "<p class=""notes"">^&</p>^p" 
     .Forward = True 
     .Wrap = wdFindContinue 
     .Format = True 
     .MatchCase = False 
     .MatchWholeWord = False 
     .MatchAllWordForms = False 
     .MatchSoundsLike = False 
     .MatchWildcards = True 
    End With 
    Selection.Find.Execute Replace:=wdReplaceAll 



    End If 
    sStyleName = "Right_Para" 
    If IsValidDocumentStyle(ActiveDocument, sStyleName) Then 

    Selection.Find.ClearFormatting 
    Selection.Find.Style = ActiveDocument.Styles("sStyleName") 
    Selection.Find.Replacement.ClearFormatting 
    With Selection.Find 
     .Text = "*^13" 
     .Replacement.Text = "<p class=""right"">^&</p>^p" 
     .Forward = True 
     .Wrap = wdFindContinue 
     .Format = True 
     .MatchCase = False 
     .MatchWholeWord = False 
     .MatchAllWordForms = False 
     .MatchSoundsLike = False 
     .MatchWildcards = True 
    End With 
    Selection.Find.Execute Replace:=wdReplaceAll 



    End If 
    sStyleName = "Chapter_Title" 
    If IsValidDocumentStyle(ActiveDocument, sStyleName) Then 

    Selection.Find.ClearFormatting 
    Selection.Find.Style = ActiveDocument.Styles("sStyleName") 
    Selection.Find.Replacement.ClearFormatting 
    With Selection.Find 
     .Text = "*^13" 
     .Replacement.Text = "<p class=""chtitle"">^&</p>^p" 
     .Forward = True 
     .Wrap = wdFindContinue 
     .Format = True 
     .MatchCase = False 
     .MatchWholeWord = False 
     .MatchAllWordForms = False 
     .MatchSoundsLike = False 
     .MatchWildcards = True 
    End With 
    Selection.Find.Execute Replace:=wdReplaceAll 



    End If 
    sStyleName = "H1" 
    If IsValidDocumentStyle(ActiveDocument, sStyleName) Then 

    Selection.Find.ClearFormatting 
    Selection.Find.Style = ActiveDocument.Styles("sStyleName") 
    Selection.Find.Replacement.ClearFormatting 
    With Selection.Find 
     .Text = "" 
     .Replacement.Text = "<h1>^&</h1>^p" 
     .Forward = True 
     .Wrap = wdFindContinue 
     .Format = True 
     .MatchCase = False 
     .MatchWholeWord = False 
     .MatchWildcards = False 
     .MatchSoundsLike = False 
     .MatchAllWordForms = False 
    End With 
    Selection.Find.Execute Replace:=wdReplaceAll 


    End If 

    MsgBox "HTML CONVERSION COMPLETED SUCCESSFULLY" 

     End Sub 
+0

См. Мое редактирование для решения второй проблемы. –

ответ

1

Основная обработка ошибок: проверьте, если стиль действует, прежде чем использовать его!

Существует несколько способов сделать это, каждый из них - это цикл всех стилей, чтобы увидеть, совпадает ли имя с одним из доступных стилей. Кроме того, поместите его в функцию, чтобы вы могли ее повторно использовать. В моем примере, функция называется IsValidDocumentStyle:

Option Explicit 

Sub HTML_Conversion() 

    Dim sStyleName As String 

    sStyleName = "Book_Title" 

    If IsValidDocumentStyle(ActiveDocument, sStyleName) Then 
     Selection.Find.ClearFormatting 
     Selection.Find.Style = ActiveDocument.Styles(sStyleName) 
     Selection.Find.Replacement.ClearFormatting 
     With Selection.Find 
      .Text = "" 
      .Replacement.Text = _ 
       "<h1 class=""book-title"">^&</h1>^p" 
      .Forward = True 
      .Wrap = wdFindContinue 
      .Format = True 
      .MatchCase = False 
      .MatchWholeWord = False 
      .MatchWildcards = False 
      .MatchSoundsLike = False 
      .MatchAllWordForms = False 
     End With 
     Selection.Find.Execute Replace:=wdReplaceAll 

    End If 

End Sub 

Function IsValidDocumentStyle(poDoc As Document, psStyleName As String) As Boolean 
    Dim oStyle As Style 
    Dim bReturn As Boolean 

    bReturn = False 

    For Each oStyle In poDoc.Styles 
     If oStyle.NameLocal = psStyleName Then 
      bReturn = True 
      Exit For 
     End If 
    Next 

    IsValidDocumentStyle = bReturn 

    Set oStyle = Nothing 
End Function 

EDIT:

При попытке использовать мой пример, вы передаете имя "HTML_Start" стиль для IsValidDocumentStyle, но затем использовать буквенную строку "sStyleName" как название стиля в строках Selection.Find.Style = ActiveDocument.Styles("sStyleName").

Смежные вопросы