|
|
Thread Tools | Display Modes |
#1
|
|||
|
|||
I got error for Macro script when I used for split large file and convert as HTML
Dear all,
I have a large file around 1300 pages and wanted to split by pages and also change file formate as HTML but getting error. My Macro script is, Sub BreakOnPage() ' Used to set criteria for moving through the document by page. Application.Browser.Target = wdBrowsePage For i = 1 To ActiveDocument.BuiltInDocumentProperties("Number of Pages") 'Select and copy the text to the clipboard ActiveDocument.Bookmarks("\page").Range.Copy ' Open new document to paste the content of the clipboard into. Documents.Add Selection.Paste ' Removes the break that is copied at the end of the page, if any. Selection.TypeBackspace ChangeFileOpenDirectory "C:" DocNum = DocNum + 1 ActiveDocument.SaveAs FileName:="page_" & DocNum & ".html ActiveDocument.Close ' Move the selection to the next page in the document Application.Browser.Next Next i ActiveDocument.Close savechanges:=wdDoNotSaveChanges End Sub Aprreciated for your help. |
#2
|
||||
|
||||
There are a couple of issues with your code, but the likely problem issues are that you have not defined the file type and you have not set the save path. Saving to the root of the C:\ drive would almost certainly be blocked by security issues.
It is better to declare your variables and name the documents e.g. Code:
Sub BreakOnPage() Dim i As Integer Dim oSource As Document, oTarget As Document Dim oRng As Range Dim strPath As String strPath = "C:\Path\" 'enter the path here ' Used to set criteria for moving through the document by page. Application.Browser.Target = wdBrowsePage Set oSource = ActiveDocument For i = 1 To oSource.BuiltInDocumentProperties("Number of Pages") 'Set a range to the page Set oRng = oSource.Bookmarks("\page").Range ' Remove the break that is copied at the end of the page, if any. oRng.End = oRng.End - 1 'Create new document to copy the range to. Set oTarget = Documents.Add oTarget.Range.FormattedText = oRng.FormattedText oTarget.SaveAs FileName:=strPath & "page_" & i & ".html", FileFormat:=wdFormatFilteredHTML oTarget.Close ' Move the selection to the next page in the document Application.Browser.Next Next i oSource.Close savechanges:=wdDoNotSaveChanges Set oSource = Nothing Set oTarget = Nothing Set oRng = Nothing End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
#3
|
|||
|
|||
Again error for file type
Dear Gmayor,
Thanks for your suggestion, I did same as you told, but it showing same error. see attached image, |
#4
|
||||
|
||||
Does the path (strPath) exist?
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
Tags |
error 5156, macro assign word, split and convert html |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
DOCX TO HTML - script writing | adikrak | Word VBA | 7 | 01-02-2020 02:52 PM |
Help Please: New VBA user trying to use a macro to split Mail Merge documents. Two Run-Time Error | zipit189 | Word VBA | 7 | 03-18-2015 01:13 PM |
Ongoing Nightmare - Opening a .txt. or .csv containing HTML script | freshfrost | Excel | 4 | 09-28-2014 12:23 AM |
Convert word files to html including hyperlink file types | MikS | Word | 3 | 06-12-2012 05:39 AM |
Convert a file from HTML to WORD format weblayout view | gtselvam | Word | 0 | 12-02-2008 03:53 AM |