У меня есть несколько макросов для применения стилей в текстовом документе, затем найдите и замените указанные стили на теги 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
См. Мое редактирование для решения второй проблемы. –