Showing posts with label VBA Code Snippet. Show all posts
Showing posts with label VBA Code Snippet. Show all posts

Friday, March 14, 2014

How to switch from main view to Endnote view

Leave a Comment


As I mentioned in the previous article, to make vba code work with endnote or footnote we need to switch the view to endnote or footnote view. In this article I show the code for switching view of word document.

How to switch the view to endnote view
First I use the function EndnotesExist to check whether there is endnote exists in the word document and then use ActiveWindow.ActivePane.View.Type to switch the view


≫ Detect Endnote
FunctionEndnotesExist() As Boolean
    DimStoryRange As Range
    ForEach StoryRange In ActiveDocument.StoryRanges
        IfStoryRange.StoryType = wdEndnotesStory Then
            EndnotesExist = True
            ExitFor
        End If
    Next StoryRange
EndFunction


Full code
PublicSub ChangeFormatInEndnote()
   
    IfEndnotesExist = TrueThen
        ' switch body to endnotes
        IfActiveWindow.ActivePane.View.Type = wdPrintView Or ActiveWindow. _
            ActivePane.View.Type = wdWebView Or ActiveWindow.ActivePane.View.Type = _
            wdPrintPreview Then
                ActiveWindow.View.SeekView = wdSeekEndnotes
        Else
            ActiveWindow.View.SplitSpecial = wdPaneEndnotes
        End If
       
        ' Macro in endnote
        Selection.WholeStory
        'Put your Change Function Here
       
        IfActiveWindow.ActivePane.View.Type = wdPrintView Or ActiveWindow. _
            ActivePane.View.Type = wdWebView Or ActiveWindow.ActivePane.View.Type = _
            wdPrintPreview Then
                ActiveWindow.View.SeekView = wdSeekMainDocument
        Else
            ActiveWindow.Panes(2).Close
        End If
    End If
   
    Selection.WholeStory
    'Put your Function Here
   
EndSub

Read More...

How to detect footnote and endnote whether they exist in word document

Leave a Comment


In this article, it is a bit strange that why we need to check whether the footnote or endnote exists or not with word document. The reason is that word document separates the document into these views, main document view and footnote or endnote view. When you run your macro code, it work only in the main view, so in the footnote or endnote views it doesn't work.

How to make macro work in footnote or endnote view
We need to detect the footnote or endnote and switch from main view to footnote or endnote view with ActiveWindow.ActivePane.View.Type.


Detect Endnote
FunctionEndnotesExist() As Boolean
    DimmyStoryRange As Range
    ForEach myStoryRange In ActiveDocument.StoryRanges
        IfmyStoryRange.StoryType = wdEndnotesStory Then
            EndnotesExist = True
            ExitFor
        End If
    Next myStoryRange
EndFunction


Detect Footnote
FunctionFootnotesExist() As Boolean
    DimmyStoryRange As Range
    ForEach myStoryRange In ActiveDocument.StoryRanges
        IfmyStoryRange.StoryType = wdFootnotesStory Then
            FootnotesExist = True
            ExitFor
        End If
    Next myStoryRange
EndFunction

Conclusion, detect footnote or endnote when using vba programming with word document is important because when you using vba code to perform action with word document, it work only with the main document, not work with endnote, so to make it work with footnote or endnote your need to detect it and switch the document view. In the next article, I will show how to switch the document views in word document.
Read More...

How to convert footnote to endnote in word document

Leave a Comment


In this section, I discuss about footnote and endnote within word document and how convert from footnote to endnote

What is footnote?
A term used to describe additional information found at the bottom of a page call footnote

What is endnote?
Endnote is additional information or credits given at the end of the document instead of at the end of each page


How to convert from footnote to endnote
ActiveDocument.Footnotes.Convert

Here is the code for converting from footnote to endnote
SubConvertFootnotesEndnotes()
' Convert
    ActiveDocument.Footnotes.Convert
    WithActiveDocument.Range(Start:=ActiveDocument.Content.Start, End:= _
        ActiveDocument.Content.End).FootnoteOptions
        .Location = wdBottomOfPage
        .NumberingRule = wdRestartContinuous
        .StartingNumber = 1
        .NumberStyle = wdNoteNumberStyleArabic
    EndWith
' Renumbering
    WithActiveDocument.Range(Start:=ActiveDocument.Content.Start, End:= _
        ActiveDocument.Content.End).EndnoteOptions
        .Location = wdEndOfDocument
        .NumberingRule = wdRestartContinuous
        .StartingNumber = 1
        .NumberStyle = wdNoteNumberStyleArabic
    EndWith
EndSub

How to test this Code
1. Open Ms Word
2. Open VB Editor or Alt+F11
3. Create new module
4. Copy and paste the Code
5. Go back to you word and go to Macro Dialog then select the Macro Name (ConvertFootnotesEndnotes) and Click on Run
Read More...