I am trying to add the path and file name of files that throw exceptions (see image attached). My code works but Visual Studio Community Edition 2015 (VB.NET 4.6) reports "Variable 'FileName' is used before it is assigned a value...". Can anyone suggest how I can remove this issue, or whether this is ok to ignore? I am new to VB.NET and would be happy to reformat my code if there is a better way to do this.
Regards
George
use this:
Dim FileName as String = Nothing 'String.Empty 'C#: null
Related
I am trying to create a macro that will export a Microsoft Project file into an excel file. Through the use of macro recording I have got a line of code that accomplishes this using the export wizard, but I want the file path and file name to be dynamic so I can use this macro on different projects. I have been searching many other threads and the Microsoft website with no luck. Is this possible?
Here is what I have:
sub formatAndSave ()
FileSaveAs Name:="C:\Users\XXXXXX\SharePoint\Projects\ProjectType\HxH\myProject.xlsx",_
FormatID:="MSProject.ACE", map:="myMap"
end sub
One idea I tried was:
Active.Workbook.SaveAs FileName:=Title
Any help would be very much appreciated!
For the sake of simplicity, let's assume for all answers below your project is located at c:\projects\myProj.mpp
I think you're after the string replace function. Something like:
Dim excelFilePath As String
excelFilePath = Replace(ActiveProject.FullName, ".mpp", ".xlsx")
Debug.Print excelFilePath
'the output would be c:\projects\myProj.xlsx
If you're unfamiliar with string manipulation in VB/VBA, just search the web for "VBA string manipulation". Microsoft has a decent article here: https://msdn.microsoft.com/en-us/library/aa903372(v=vs.71).aspx
A few other things that may be handy for you are these variables:
ActiveProject.FullName 'shows full path & name, so you'd get "c:\projects\myProj.mpp"
ActiveProject.Path 'shows just the path, so you'd get "c:\projects\"
ActiveProject.Name 'shows just the file name, so you'd get "myProj.mpp"
Finally, one caveat I've seen is that the ActiveProject.FullName and ActiveProject.Name variables may or may not provide the file extension depending on your local windows environment settings. I've observed that if Windows Explorer is configured to hide file extensions, then these variables also withhold the extension; if Explorer is configured to show them, then they are provided in the variables. Make sure your code is robust to both cases, or make sure you have control over the environment where you code will run.
I have a VB.net program that I wrote and have used hundreds of times. Whilst using Windows 7 I "upgraded" to Office 2010 and IIRC had to make a few small changes to get it to work. I have now (and again I put it in quotes as I fail to see the benefits of calling it an upgrade !) "upgraded" to Windows 10 but went back to Office 2007 as I much prefer it. I am also using Visual Studio Community 2015. All of that may or may not be of help !!!
So, I run the program and it fails with the following error :
An unhandled exception of type
'System.Runtime.InteropServices.COMException' occurred in
KA_Newsletter.exe
Additional information: Word cannot open this document template.
(L:...\Customize Ribbon Example 2.dotm)
I have looked up the error and there is a Microsoft page ...
MS Support Bug
... that suggests this may be a Bug, it explains why it may be happening and gives a resolution but I program for fun, I'm not an expert in VB at all and it may as well be written in Russian for all it helps me !!!
I also have no idea why Word should be trying to open that Example Template either, I copy a Template of my own to create a new Word document, this is pretty basic stuff !!! This is the relevant code, any help would be very much appreciated ...
Dim myNewsLetter As String
.
.
.
If File.Exists(myNewsLetter) Then
'do nothing
Else
myTemplate = myTempFolder & "KA_Newsletter.doc"
File.Copy(myTemplate, myNewsLetter)
Create_Blank_Newsletter()
End If
.
.
.
Private Sub Create_Blank_Newsletter()
myMSWord = New Word.Application
myMSDoc = myMSWord.Documents.Open(myNewsLetter) << <Error occurs on this line
myMSWord.WindowState= Word.WdWindowState.wdWindowStateNormal
myMSWord.Visible= False
UPDATE :
Olaf, I updated the code as follows ...
myMSWord = New Word.Application
Dim inval As Object
'Marshal the object before passing it to the method.
inval = New System.Runtime.InteropServices.DispatchWrapper(myNewsLetter)
myMSDoc = myMSWord.Documents.Open(inval)
'myMSDoc = myMSWord.Documents.Open(myNewsLetter)
... but I am getting a similar error on the Open statement ...
An unhandled exception of type 'System.Runtime.InteropServices.COMException' occurred in KA_Newsletter.exe
Additional information: Type mismatch. (Exception from HRESULT: 0x80020005 (DISP_E_TYPEMISMATCH))
Any ideas ?
The MS page says you should try something like
Dim inval As Object
'Marshal the object before passing it to the method.
inVal = New System.Runtime.InteropServices.DispatchWrapper(myNewsLetter)
myMSDoc = myMSWord.Documents.Open(inval)
I took out part of my code, and I've reversed this text. Then I saved the file in the Resource as txt.
I want to read the text from the text file and reverse the text again, and then execute the text as codes.
I used this code to reverse the text:
Codes
Dim TestString As String = My.Resources.rever1
Dim revString As String = StrReverse(TestString)
Dim malk1221 As Assembly = Assembly.Load(revString)
Codes
But the program crashed and nothing happened.
Note that there are codes before and after codes.
How do I fix this?
Assembly.Load expects an assembly name as argument, not VB source code!
See How to programmatically compile code by using the Visual Basic .NET or Visual Basic 2005 compiler on MSDN.
Here is another example: Compile and execute code at RunTime.
`Dim medium As String = TextBox1.Text
Dim data_xml = XElement.Load("Assets\Manager.xml")
'next quaries the xml for desired attributes
Dim query = From DataTable1 In data_xml.Descendants("DataTable1")
Where (DataTable1.Attribute("Medium").Value = medium)
Select Uname = DataTable1.Attribute("Username").Value
For Each result In query
'displays results to textbox
TextBox2.Text = result
Next
I try to use this code to read from an xml file in the assets but the file does not seem to exist
At the declaration of the xelement.load("Assets\Manager.xml") I get this error
An exception of type 'System.IO.FileNotFoundException' occurred in mscorlib.ni.dll but was not handled in user code
Additional information: Could not find file 'C:\Data\SharedData\PhoneTools\AppxLayouts\d5d3a1e7-56d7-477c-bcd2-f949f3374de1VS.Debug_AnyCPU.NAME\Assets\Manager.xml'.
If there is a handler for this exception, the program may be safely continued.
Any ideas?
Steps to make it work:
Change Manager.xml Build action to Content
Change Copy to output directory to Copy if newer
Load file using XElement.Load("Assets/Manager.xml");
Edit:
Ok, since you are not willing to share more of the code even though you ask for help, there's nothing else I can do but put up an example :)
"Software" below, implemented in Visual Basic, reads and displays XML file named Manager.xml, that is set as Content in WP8 project. Working example can be found at https://github.com/mikkoviitala/read-content-xml
I wrote a VSMacro (in VS2010) that parses a solution and adds try and catch statements to cpp files. It's been working fine up until now. I had to reformat my pc and reinstall VS2010 and now it crashes. Here's a snippet from the macro.
Sub Foo(ByVal file As EnvDTE.ProjectItem)
Dim fileCM As EnvDTE.FileCodeModel
fileCM = file.FileCodeModel
End Sub
I've checked to see if that file is a valid object, and it does point to a cpp file. But for some reason file.FileCodeModel = Nothing. Why would FileCodeModel be Nothing?
There are several scenarios in Visual Studio where a ProjectItem will not return a FileCodeModel object. For example if the ProjectItem points to a folder or a non-code file item in solution explorer.
Can you verify what the ProjectItem represents in this scenario. In particular
Is it pointing to a file?
What type of project is this in (Web, console app, etc ...)?
Usually, it happens when the project is not compilable. You need to try to build the solution first.