Accessing standard VBA word enumerations - vba

I'm new to VBA. Some tutorials tell me to use enumeration constants such as wdOrientLandscape and wdRowHeightExactly (to change page orientation and make row height 'exact' respectively). However, when debugging, I can see that the value of these constants is Empty and they don't 'work' as expected.
Do I need to do something special to access these constants e.g. add a reference or something?
I'm creating a Word document from an Excel macro.
Thanks in advance

Within Excel (or any automation client) the enumerations belonging to the Word object model are not exposed if you use Late Binding (CreateObject("Word.Application")).
If you early bind by adding a reference they become visible.
If you add Option Explicit to the top of you code modules (or select Require Variable Declarations from the VBA editor options) you will receive a compile time warning if you attempt to use something that's not declared/unavailable
In your particular case, you would want to add the Microsoft Word Object Library to your references. If you choose to early bind. This can be done by going to Tools>>References and checking the appropriate box.

Related

Access VBA control reference issue?

I'm trying to loop through all the controls on an Access form but for some reason the controls object is not giving me the correct properties.
When I select and right click on "Control" (in 'As Control') it says "identifier under cursor is not recognized".
I'm thinking it's a reference issue but I can't figure what's missing or incorrect. I don't get any syntax error so Control is a valid object.
I tried decompiling, repair and compacting just to be sure.
The generic control object doesn't have too many useful properties or methods.
It covers also things like lines or subform containers or tab cards, which don't have much in common.
The specific controls like Textboxes have all the properties you are probably looking for.
You still can loop over all controls, and set the usual properties that most controls have, like .Visible or .Locked. Even if Intellisense doesn't provide them.
ctl.Locked = bLocked will work.
But you should handle (On Error Goto) or ignore (On Error Resume Next) runtime errors that will occur, if one control doesn't have the property you want to set.
Bonus question to everyone: why does the generic control object have the .Dropdown method that applies only to combo boxes? Very strange.

How Can I Comprehensively Manage my VBA UserForm's Properties?

I am writing my first VBA Add-In under Microsoft Office Word 2007 with Windows 7 Pro/64. Part of this Add-In is a UserForm. Using the Visual Basic editor that runs from Word, I find there are two ways of viewing, and two ways of modifying a UserForm's properties:
View all properties from Object Browser (F2)
View some properties and edit them from Properties Window (F4)
Manually enter and edit any property from the Code window (F7)
Here is a screenshot of my Properties and Code windows:
A problem I find is that the Properties Window contains only a subset of the UserForm's properties (notice that CanPaste, CanRedo, and CanUndo don't appear in Properties), and changes made in the Properties Window are overridden by changes made in the Code window (e.g., at runtime, Me.Caption from the Code window above overrides the Caption field in Properties).
I suppose I should avoid using Properties at all then, and enter all settings via UserForm_Initialize as shown above. But (a) for some settings, Properties makes several settings at once. For example, selecting Verdana Bold from Properties equals Font = Verdana and Font.Bold = True in Code. And (b) it seems Properties sets the subset of properties it controls to defaults of its choosing, and if I change them I can't see what they started out as.
I therefore desire unified and comprehensive access to all my UserForm's properties at once, including the aforementioned default settings. Does anyone know how to reveal a UserForm's default settings as code, or to automatically open all its current settings in the Code window? Is there an umbrella mechanism I'm not aware of?
I'm not a veteran VBA programmer, but I can't believe my experience is unique. I've searched the 'net in vain for a solution. How do you with more experience manage this dilemma?
You use the Properties window to set appearance-related properties at design time. Those property values will then always apply unless you explicitly change them at run-time with VBA code.
Properties that don't relate to appearance, such as CanPaste and CanRedo relate to the state of the form at run-time, so it doesn't make sense to have them configurable at design-time.
You can change nearly all of the properties at run-time, whether it is in the Initialize event or elsewhere. You can even add controls at run-time, but your changes won't be persisted once the instance of the form terminates.

How to fill word template with picture controls from vb.net

I need to make an application that opens a template made by customer, with special names/titles/tags, whatever we will agree upon, and then fill it from the database based on the customer's search.
However, if the customer made a template like this one
Now we have two ContentControls (wdContentControlRichText & wdContentControlPicture)
Questions are:
1- How to make multiple pages in the same document from the same chosen template.
2- (Most Important) How to set the PictureContentControl Image from vb code.
Notes:
I tried my best to search and look for similar questions, yet no one seems to solve my problem. What I have reached is that I can loop through the content controls, but I can't cast the PictureContentControl from Microsoft.Office.Interop.Word Object to Microsoft.Office.Tools Object.
Also all solutions I found on the net, includes "Globals.Factory" which doesn't seem to ever be valid for my code, as my Globals doesn't contain the Factory Object.
I'm using the references:
Microsoft.Office.Core
Microsoft.Office.Interop.Word
Microsoft.Office.Tools
Microsoft.Office.Common
Microsoft.Office.Common.v4.0.Utilities
Microsoft.Office.Tools.Word
Microsoft.Office.Tools.Word.v4.0.Utilities
Microsoft.VisualStudio.Tools.Applications.Runtime

How to identify a specific ActiveX Interface based on limited information

I am in the process of developing an ActiveX control for embedding in Rockwell Automation's Factory Talk View. (See ActiveX DLL Error: Solved to see how great a start I got off to!)
At the moment I have an ActiveX control that seems to embed into the host system OK, but is not fully integrated into the VBA process. I believe that this is because I have not implemented some specific COM interface that is required, but I have no documentation as to what that is. I do know that standard MS controls like the Microsoft Forms 2.0 controls do implement the required interface and fully integrate.
The key functionality I am missing relates to a property that I need to set in order for my VBA code to actually see the ActiveX object. This property can be seen when opening the property panel for any activeX control placed on a Factory Talk screen, but NOT when looking at the properties of ActiveX controls embedded in (for example) Excel. Thus I am probably trying to chase down some custom Rockwell implementation. I am hoping that SO might be able to give me some aid into how to track down what I want based on recognizing the functionality it implements.
Now to the actual property. In Factory Talk the property is called "ExposeToVBA", and has a type "long". You have a choice of 3 values selected from a drop down list:
Not Exposed - When set to this, the controls name is not even visible to VBA code
Type Info Extension - Not sure what this does
VBA Control - VBA code can see and interact with the control.
When I select anything other than "Not Exposed" for my control, I get a warning dialog box with no text on it. For other controls I need to set it to "VBA Control" in order to interact with it in the VBA code (d'oh)
I suspect that the value of this property is translated into some calls to an ActiveX interface on the target control.
The Question
What I want to do is identify what interfaces something like a "Forms 2.0 Label" implements that would match the above functionality. And also perhaps how I could translate this into my C# based control (based on C# ActiveX control (CSActiveX))
I know its a long shot, but I'd appreciate any and all suggestions
This became moot when I found a canned Active-x control that had the functionality that I needed.
FWIW This is what I believe that I used: DMGraph

Is there a way to get full IntelliSense for VBA in Access and Excel 2007?

In VB.NET, the IntelliSense pops up as soon as you start typing which gives you a pretty full list of things you can use at that moment. The IntelliSense in VBA however, doesn't kick on for me until a period is put after the part you're using. For example, I go into the VBA editor in Excel 2007 and start typing the word "Range" but the IntelliSense doesn't come up until I type "Range." after which it will give me a list of things I can use at that point.
Is there any way to make IntelliSense come up sooner in the VBA editor, so that I'm able to see a list of things that can be used like Davg, DCount, etc?
Just press Ctrl + Space at any time in the editor.
No, but you can type Excel first. Put a period after that, and you'll find out more than you ever wanted to know about Excel.
Same with Access. Type Access and a period, and you will find DCount there.
Unfortunately in Excel some objects are declared 'As Object' and Object doesn't have any methods so Intellisense won't be able to display any. The main culprit being Excel.ActiveSheet.
If you know what the type actually is then you can declare it explicitly and assign it to the value you want. EG:
Dim mySheet As Worksheet
Set mySheet = ActiveSheet 'This line would cause a type mismatch error if mySheet was declared as something other than a WorkSheet
mysheet.[All The Intellisense For a Worksheet Pops Up]
VBA is a different beast than .NET so I'm not sure how to bring up the IntelliSense quicker. I find I have the same problem you're having in 2003.
I would suggest checking out the Object Browser though. In 2003, it's View > Object Browser, or F2, in the VB Editor. I find it's a great way to explore the class libraries available. It will show you everything that you currently have referenced and once you reference more libraries, they will also show up in the Object Browser.