Word VBA - best way to place a flag in a document?

I want to programatically place a flag in a Word document (say at the end) so that when it is next opened I can make the Document_Open skip over some code.

What would be the best way to do this?
A field?
A bookmark?
A variable?

Thanks.
hindersalivaAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

B HaydenCommented:
I'm not particularly familiar with the object model of Word but setting and retrieving the value of a 'CustomProperty' as shown on the following page might help:

https://msdn.microsoft.com/en-us/vba/word-vba/articles/customproperties-object-word
hindersalivaAuthor Commented:
Thanks BH. That helped, although the examples didn't work for me.
I found this
https://support.office.com/en-gb/article/view-or-change-the-properties-for-an-office-file-21d604c2-481e-4379-8e54-1dd4622c6b75

And I did
thisdocument.CustomDocumentProperties("IsALiveSpec") = "Y"

Open in new window

having set the property up as per the link above.
GrahamSkanRetiredCommented:
If you want it to be visible and editable to the user, you can use a custom Document Property. Otherwise use a Document Variable. Here is some code

Sub TestSetVar()
    SetDocumentVariable ActiveDocument, "Flag", "True"
End Sub

Sub TestGetVar()
Dim strValue As String
    strValue = GetDocumentVariableValue(ActiveDocument, "Flag")
    MsgBox "Value is: " & strValue
End Sub


Sub SetDocumentVariable(doc As Document, strVarName As String, strValue As String)
Dim bExists As Boolean
Dim var As Variable

For Each var In doc.Variables
    If var.Name = strVarName Then
        bExists = True
        Exit For
    End If
Next var
If bExists Then
    var.Value = strValue
Else
    ActiveDocument.Variables.Add "Flag", strValue
End If
End Sub

Function GetDocumentVariableValue(doc As Document, strVarName As String) As String
Dim bExists As Boolean
Dim var As Variable

For Each var In doc.Variables
    If var.Name = strVarName Then
        bExists = True
        Exit For
    End If
Next var
If bExists Then
    GetDocumentVariableValue = var.Value
Else
    GetDocumentVariableValue = ""
End If
End Function

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

Paul SauvéRetiredCommented:
Shift-F5 When you open a document, go to the place you were working when you last closed it


go to last 2 pages for this info:
More useful keyboard shortcuts in Word 2010
Key combination           Action

Shift-F5                            When you open a document, go to the place you were working when you last closed it

and here's another one:
Shortcut Key Reference
The following table lists the default shortcut keys (which are also called keyboard shortcuts or key combinations) that are available in Word. For the Microsoft help topic about the keyboard shortcuts available in Word 2016 and Word 2013, see Keyboard Shortcuts for Microsoft Word 2016 for Windows. For the Microsoft help topic about the keyboard shortcuts available in Word 2010, see Keyboard Shortcuts for Microsoft Word. For the Microsoft help topic about the keyboard shortcuts available in Word 2007, see Keyboard Shortcuts for Microsoft Office Word.

Note. Some of these shortcut keys work only in outline view. For more information about outline view, see How to Save Yourself Hours by Using Outline View Properly by Dave Rado.
hindersalivaAuthor Commented:
Thanks BH and Graham.

Paul Sauvé?
Paul SauvéRetiredCommented:
Paul Sauvé?
Shift-F5: When you open a document, go to the place you were working when you last closed it
open any previously opened Word document & you will see that the doc 'remembers' the last position of the cursor.
my point was that you don't really need VBA to accomplish what you want to do
hindersalivaAuthor Commented:
my point was that you don't really need VBA to accomplish what you want to do
Sorry Paul. My question was not about 'going back to the last place' which I think you're thinking. It is about a block of VBA code that I want the program execution to skip when the document it opened second and subsequent times. So, the solution I was after is, to place a flag that can be saved with the document so when opened subsequently the code looks at that and skips the block of code I need to skip.

I can't see how that can be done without code. Can you?
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VBA

From novice to tech pro — start learning today.